Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I verify that a JMS queue exists using Java?

Tags:

java

jms

How can I check whether a queue exists on a JMS server using the Java API? I don't want to send or receive any data to the queue for now, just verify that the queue exists. Also, the queue may be empty.

Here is my code sample. I have removed the error handling for simplicity.

    Connection connection = null;
    Session session = null;
    connection = factory.createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    //I was hoping this next line would throw an exception if the queue does not exist
    Queue queue = session.createQueue(queueName);

My JMS server is TIBCO EMS. I'm hoping for a solution that works on versions 5-7.

Solution

I followed the recommendation in the accepted answer but created a browser instead. The following line threw an exception as desired:

QueueBrowser browser = session.createBrowser(queue);
like image 226
user506069 Avatar asked Oct 08 '14 15:10

user506069


People also ask

What is JMS queue in Java?

JMS supports both messaging models: point-to-point (queuing) and publish-subscribe. JMS was defined to allow Java application to use enterprise messaging systems. More importantly, it provides a common way for Java applications to access such enterprise messaging systems.

How do I get messages from JMS queue?

Use a message selector to receive a subset of messages from a destination. Name or lookup name of the Destination from which you want to receive the message. Specify the lookup name of the Destination object when the JMS connection alias uses JNDI to retrieve administered objects.


1 Answers

This is dependent on the provider, but you wont know in most cases until you create the session type, such as session.createConsumer. Simply creating a consumer this way will not consume any messages until you do a receive. And it is here the behavior may change from provider to provider and configuration of the server.

For example with ActiveMQ, assuming there are no permissions blocking the user you are connecting with, the queue is created automatically when you create the session type.

With WebSphere MQ, the queue has to be defined by an admin. If it does not exist, the queue manager will return an exception with a reason code of 2085 (UNKNOWN_OBJECT_NAME).

Outside of this, you'd need to see if the particular provider had a way to access a list of queues. Using the above examples, ActiveMQ you can get the list of queues using JMX, with WebSphere MQ, you can do this if you have permissions to send PCF commands to the queue manager.

like image 81
Erik Williams Avatar answered Oct 21 '22 22:10

Erik Williams