How to Create Temporary Queues or Topics
It is possible to create a temporary queue or topic that will exist as long as the current connection is open or until you call delete. Only the connection that created the queue or topic can receive messages from it, but other connections may send messages. A common use of temporary queues is to use them for receiving responses to a request message. For this you pass the temporary queue in the JMSReplyTo header field.
Queue queue = ...;
Session session = ...;
// Create temporary queue for response
Queue tmpQueue = session.createTemporaryQueue();
MessageProducer producer = session.createProducer(queue);
Message msg = session.createTextMessage("how are you?");
msg.setJMSReplyTo(tmpQueue); // use temporary queue as response channel
producer.send(msg);
The message's receiver must take the destination from the JMSReplyTo property and send the reply to this channel. Meanwhile sender of the original message needs to listen to the temporary queue, waiting for the response. When the temporary queue is not needed anymore, it should be deleted using delete, unless the connection is closed anyway.
Note: temporary queues can not be used with message-driven beans, as they don't support configuring the queue after deployment.