繁体   English   中英

如何获取现有JMS队列?

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

我觉得这可能是一个非常简单的问题,但这是我第一次涉足JMS,所以我有点不确定。

我正在尝试写入现有的JMS队列(然后从另一个队列中读取),我知道队列名称,主机,队列管理器和通道。 如何以javax.jms.Destination对象的形式获取对此队列的引用?

我发现的所有示例都涉及调用javax.jms.Session.createQueue(String) ,但由于此队列已经存在,我不想创建另一个,对吧? 或者我误解了发生了什么?

如果重要,我使用的是com.ibm.msg.client.jms驱动程序。

谢谢!

通常,运行应用程序的容器将在其命名服务中绑定Queue 容器中的应用程序可以使用JNDI查找并使用它。

要在上面添加erickson的答案:

这是获取和浏览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());
 }
...

确保使用这些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