How to check if a JMS queue exists using Java?

How to check if a queue exists on a JMS server using the Java API? At the moment I do not want to send or receive any data to the queue, just make sure that the queue exists. In addition, the queue may be empty.

Here is my sample code. I just deleted the error handling.

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 hope for a solution that works on versions 5-7.

Decision

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

 QueueBrowser browser = session.createBrowser(queue); 
+6
source share
2 answers

Try creating a user or producer from a session in the newly created queue object:

 session.createConsumer(queue); 

This should throw an InvalidDestinationException if the queue (or topic) does not exist.

+3
source

It depends on the provider, but in most cases you will not know until you create a session type, such as session.createConsumer. Simply creating a consumer in this way will not consume any messages until you receive the receipt. And this is where the behavior can change from provider to provider and server configuration.

For example, using ActiveMQ, if there are no permissions blocking the user you are connecting to, a queue is created automatically when creating a session type.

Using WebSphere MQ, the queue must be defined by the administrator. If it does not exist, the queue manager returns an exception with reason code 2085 (UNKNOWN_OBJECT_NAME).

In addition, you will need to find out if a particular provider has access to the queue list. Using the examples above, ActiveMQ you can get the list of queues using JMX using WebSphere MQ, you can do this if you have permission to send PCF commands to the queue manager.

+4
source

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


All Articles