如何创建一个临时JMS队列,并通过名称连接到它?(How to create a temporary

2019-08-20 05:06发布

我需要创建应答临时队列,但我需要知道是否有可能连接到临时队列,不通过消息的setJMSReplyTo方法发送响应队列对象,因为答复线程没有得到该对象的。

Answer 1:

我绑定我的临时队列使用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);    


Answer 2:

asadmin> create-jms-resource --restype javax.jms.ConnectionFactory --description "connection factory for XXX" jms/ConnectionFactory



asadmin> create-jms-resource --restype javax.jms.ConnectionFactory --description "connection factory for durable subscriptions"
 --property ClientId=MyID jms/DurableConnectionFactory

命令创建的JMS资源成功执行。

如GlassFish中server.it将成功创建。



文章来源: How to create a temporary jms queue and connect to it by name?
标签: java queue jms