Chapter 14. Temporary Queues and Runtime Queues When designing a request-reply pattern where a client sends a request and waits for a reply, you must consider whether each runtime instance of the client requires a dedicated queue for its replies, or whether the runtime instances can access a shared queue, selecting their specific reply messages based on an appropriate attribute. If multiple queues are required, then clients need the ability to create a queue dynamically. Jakarta Messaging provides this facility using the concept of temporary queues. A TemporaryQueue is created on request by the Session . It exists for the life of the Connection , for example until the connection is closed, or until the temporary queue is deleted. This means that although the temporary queue is created by a specific session, it can be reused by any other sessions created from the same connection. The trade-off between using a shared queue and individual temporary queues for replies is influenced by the potential number of active client instances. With a shared-queue approach, at some provider-specific threshold, contention for access to the queue can become a concern. This has to be contrasted against the additional overhead associated with the provider creating queue storage at runtime and the impact on machine memory of hosting a potentially large number of temporary queues. The following example creates a temporary queue and consumer for each client on startup. It sets the JMSReplyTo property on each message to the temporary queue, and then sets a correlation ID on each message to correlate request messages to response messages. This avoids the overhead of creating and closing a consumer for each request, which is expensive. The same producer and consumer can be shared or pooled across many threads. Any messages that have been received, but not yet acknowledged when the session terminates, are retained and redelivered when a consumer accesses the queue. Example: Temporary Queue Code ... // Create a temporary queue, one per client Destination temporaryQueue = session.createTemporaryQueue(); MessageConsumer responseConsumer = session.createConsumer(temporaryQueue); // This class handles messages to the temporary queue responseConsumer.setMessageListener(this); // Create the message to send TextMessage textMessage = session.createTextMessage(); textMessage.setText("My new message!"); // Set the reply to field and correlation ID textMessage.setJMSReplyTo(temporaryQueue); textMessage.setJMSCorrelationID(myCorrelationID); producer.send(textMessage); ... In a similar manner, temporary topics are created using the Session.createTemporaryTopic() method.

Commands:
// Create a temporary queue, one per client Destination temporaryQueue = session.createTemporaryQueue(); MessageConsumer responseConsumer = session.createConsumer(temporaryQueue); // This class handles messages to the temporary queue responseConsumer.setMessageListener(this); // Create the message to send TextMessage textMessage = session.createTextMessage(); textMessage.setText("My new message!"); // Set the reply to field and correlation ID textMessage.setJMSReplyTo(temporaryQueue); textMessage.setJMSCorrelationID(myCorrelationID); producer.send(textMessage);