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

How to create a temporary jms queue and connect to it by name?(如何创建一个临时 jms 队列并通过名称连接到它?)

本文介绍了如何创建一个临时 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);    

这篇关于如何创建一个临时 jms 队列并通过名称连接到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何创建一个临时 jms 队列并通过名称连接到它?

基础教程推荐