How can I get an existing JMS queue?

It seems to me that this is probably a pretty simple question, but this is my first foray into JMS, so I'm 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 to get a link to this queue as an object javax.jms.Destination?

All the examples I found include a call javax.jms.Session.createQueue(String), but since this queue already exists, I don’t want to create another one, right? Or I do not understand what is happening?

If that matters, I use the com.ibm.msg.client.jms driver.

Thank!

+3
source share
2 answers

, , Queue . JNDI .

+4

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

Source: https://habr.com/ru/post/1729648/


All Articles