title: How to read and collect response messages from JMS and make them available to the correct servlet thread?
body: <p>We've got a synchronous HTTP request-response front end for an asynchronous JMS messaging system.</p>
  
  <p>A HTTP query servlet for each HTTPRequest creates a corresponding JMS message in a query queue. This query is processed by a back-end, and a couple response messages are created for this query. What is a good way to organize the receipt of the response messages in JMS, and to make sure they reach the proper servlet thread so that it can formulate the HTTPResponse?</p>
  
  <p>The queries and responses are non-transactional and need not be persisted. Most of them are read queries. If no response gets read within 45 seconds, the servlet generates a timeout response. Throughput is important, however. We need to process an ever increasing amount of queries. The system is about ten years old and will have to stay up and running for two more years or so.</p>
  
  <p>We're using SonicMQ. We have created one queue for all responses. The servlet container has one connection to the broker which it uses for both reading and writing. We spawn one listener thread per logged-in user (about 1500 concurrently) This thread has a receiver with a message selector that only selects response messages for this particular user. Once a servlet thread has sent its query message, it waits for the user's listener thread to notify it that it's read a response.</p>
  
  <p>We used to have one single QueueSession shared by all senders and all receivers. This actually worked(!) though the session officially is not thread safe. Creating one QueueSession per thread (the servlet threads and the listener threads) improved the performance somewhat but things still aren't too stable and we'd like to organize things better.</p>
  
  <p>I've tried creating a temp queue per user session instead of one single queue with message selectors but that slowed things down considerably.</p>
  
  <p>What would be a better/the proper way to organize this?</p>