如何创建一个临时 jms 队列并通过名称连接到它?

2022-01-21 00:00:00 queue java jms

我需要为响应创建一个临时队列,但我需要知道是否可以在不通过消息的 setJMSReplyTo 方法发送响应队列对象的情况下连接到临时队列,因为回复线程根本没有得到该对象.

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.

推荐答案

我使用 InitialContext 对象将我的临时队列绑定到 jndi,这样我就可以从需要使用我的临时队列的线程中查找我的临时队列.

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();
connection.start();
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);    

相关文章