【问题标题】:how can I check if a queue exists on Activemq如何检查 Activemq 上是否存在队列
【发布时间】:2020-02-19 03:40:35
【问题描述】:

当数据队列不存在但不存在时,我有这个方法会抛出异常。你有其他方法解决这个问题吗?

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

【问题讨论】:

    标签: java spring activemq


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      谢谢蒂姆,我用这些方法解决了。

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

      【讨论】:

      • 有几个原因首先不依赖此机制,最重要的是如果任何禁用代理的咨询支持,它就会失败。此外,您需要多次检查,然后才能假设此处不存在队列,因为源的人口不是即时的。
      猜你喜欢
      • 2014-10-15
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 2016-12-29
      • 2018-11-23
      相关资源
      最近更新 更多