pkaeding
pkaeding

Reputation: 37633

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!

Upvotes: 3

Views: 6703

Answers (2)

Naor Bar
Naor Bar

Reputation: 2209

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>

Upvotes: 0

erickson
erickson

Reputation: 269657

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.

Upvotes: 4

Related Questions