简体   繁体   English

如何检查 Activemq 上是否存在队列

[英]how can I check if a queue exists on Activemq

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

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.默认情况下,ActiveMQ 5.x 按需创建队列,因此您可能已更改默认配置以禁止这种情况,在这种情况下,如果您遇到不存在的队列,应该会发生错误,您应该检查并处理它。 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.如果您需要确定,那么代理会提供一个JMX接口来查询有关代理统计信息等的信息。还有其他监控方式,例如通过 Jolokia 管理接口使用 Rest 样式调用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM