簡體   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