newbie newbie - 1 year ago 61
Java Question

How to create a temporary jms queue and connect to it by name?

I need to create a temporary queue for responses, but I need to know if it is possible to connect to temporary queue without sending response queue object via setJMSReplyTo method of message, because replying thread doesn't get that object at all.

Answer Source

I binded my temporary queue to jndi by using InitialContext object, so that I can lookup my temporary queue from thread that needs to use my temporary queue.

jndiContext = new InitialContext();
connectionFactory = (QueueConnectionFactory) jndiContext.lookup("ConnectionFactory");
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
temporaryQueue = session.createTemporaryQueue();       
jndiContext.bind(queueJndiName, temporaryQueue);    
destination = temporaryQueue;
responseConsumer = session.createConsumer(destination);
responseConsumer.setMessageListener(new MyListener());

To get temporary queue you just need to lookup it in code where you need to use it:

Context jndiContext = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("ConnectionFactory");
queue = (Queue) jndiContext.lookup(youTemporaryQueueName);