我需要为响应创建一个临时队列,但我需要知道是否可以连接到临时队列而不通过setJMSReplyTo消息方法发送响应队列对象,因为回复线程根本没有获得该对象.
解决方法:
我通过使用InitialContext对象将我的临时队列绑定到jndi,以便我可以从需要使用我的临时队列的线程中查找我的临时队列.
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());
要获得临时队列,您只需在需要使用它的代码中查找它:
Context jndiContext = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("ConnectionFactory");
queue = (Queue) jndiContext.lookup(youTemporaryQueueName);
标签:java,jms,queue
来源: https://codeday.me/bug/20191002/1840936.html