English 中文(简体)
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.

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. How do I get a reference to this queue in the form of a javax.jms.Destination object?

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? Or am I misunderstanding what is going on?

If it matters, I am using the com.ibm.msg.client.jms driver.

Thanks!

最佳回答

Normally, the container in which your application runs will bind the Queue in its naming service. An application in the container can look it up with JNDI and use it.

问题回答

To add erickson s answer above:

This is an example of getting and browsing a JMS Queue: (using 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:

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签