Karenloh
Karenloh

Reputation: 27

How can I check if a queue exists on ActivemQ "Classic"?

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);
}

Upvotes: 1

Views: 2099

Answers (2)

Karenloh
Karenloh

Reputation: 27

I solved it with this:

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;
}

Upvotes: 0

Tim Bish
Tim Bish

Reputation: 18376

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.

Upvotes: 2

Related Questions