Home » Java » java – how can I check if a queue exists on Activemq-Exceptionshub

java – how can I check if a queue exists on Activemq-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have this method that throws me an exception when the data queue doesn’t exists, but is not. Do you have other way to solve this?

public void checkDataQueue(String dataQueue) throws JMSException {

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

          Queue queue = session.createQueue(dataQueue);
          QueueBrowser browser = session.createBrowser(queue);
      }
How to&Answers:

ActiveMQ 5.x creates Queues on demand by default so you may have changed the default configuration to disallow this in which case the error should be expected to happen if you are hitting a non-existent queue and you should check for and handle that. If you need to be sure then the broker provides a JMX interface to query for information on broker statistics etc. There are also other ways of monitoring such as using Rest style calls over the Jolokia management interface.

Answer:

thank you Tim, I solved it with these method.

public boolean existDataQueue(String dataQueue) throws JMSException {
            boolean response = false;
            ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory();
            activeMQConnectionFactory.setBrokerURL(brokerUrl);
            ActiveMQConnection connection = (ActiveMQConnection)activeMQConnectionFactory.createConnection();

            connection.start();

            DestinationSource ds = connection.getDestinationSource();

            Set<ActiveMQQueue> queues = ds.getQueues();

            for (ActiveMQQueue activeMQQueue : queues) {
                try {
                    if(activeMQQueue.getQueueName().equalsIgnoreCase(dataQueue)) {
                        response = true;
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            connection.close();
            return response;
      }