简体   繁体   English

如何获取现有JMS队列?

[英]How can I get an existing JMS queue?

I feel like this is probably a pretty simple question, but this is my first foray into JMS, so I am a little unsure. 我觉得这可能是一个非常简单的问题,但这是我第一次涉足JMS,所以我有点不确定。

I am trying to write to an existing JMS queue (and then read from another queue), for which I know the queue name, host, queue manager, and channel. 我正在尝试写入现有的JMS队列(然后从另一个队列中读取),我知道队列名称,主机,队列管理器和通道。 How do I get a reference to this queue in the form of a javax.jms.Destination object? 如何以javax.jms.Destination对象的形式获取对此队列的引用?

All of the examples I have found involve calling javax.jms.Session.createQueue(String) , but since this queue already exists, I don't want to create another one, right? 我发现的所有示例都涉及调用javax.jms.Session.createQueue(String) ,但由于此队列已经存在,我不想创建另一个,对吧? Or am I misunderstanding what is going on? 或者我误解了发生了什么?

If it matters, I am using the com.ibm.msg.client.jms driver. 如果重要,我使用的是com.ibm.msg.client.jms驱动程序。

Thanks! 谢谢!

Normally, the container in which your application runs will bind the Queue in its naming service. 通常,运行应用程序的容器将在其命名服务中绑定Queue An application in the container can look it up with JNDI and use it. 容器中的应用程序可以使用JNDI查找并使用它。

To add erickson's answer above: 要在上面添加erickson的答案:

This is an example of getting and browsing a JMS Queue: (using javax.jms-api 2.x) 这是获取和浏览JMS队列的示例:( 使用javax.jms-api 2.x)

 // Set up the connection to the queue:
 Properties env = new Properties();
 env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
 env.put(Context.PROVIDER_URL, "http-remoting://<host>:<port>");
 Context namingContext = new InitialContext(env);
 ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup("jms/RemoteConnectionFactory");
 JMSContext context = connectionFactory.createContext("jms_user", "pwd");

 // Get the JMS Queue:
 Queue queue = (Queue) namingContext.lookup("jms/queue/exampleQueue");
 // Create the JMS Browser:
 QueueBrowser browser = context.createBrowser(queue);
 // Browse the messages:
 Enumeration<Message> e = browser.getEnumeration();
 while (e.hasMoreElements()) {
     Message message = (Message) e.nextElement();
     log.debug(message.getBody(String.class) + " with priority: " + message.getJMSPriority());
 }
...

Make sure you use these Maven dependencies: 确保使用这些Maven依赖项:

<dependency>
    <groupId>javax.jms</groupId>
    <artifactId>javax.jms-api</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-jms-client-bom</artifactId>
    <version>10.0.0.Final</version>
    <type>pom</type>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM