【问题标题】:Get list of queue names in ActiveMQ获取 ActiveMQ 中的队列名称列表
【发布时间】:2014-08-14 16:11:52
【问题描述】:

我尝试了下面的代码来获取 ActiveMQ 中的队列列表。但它不工作。我的 ActiveMQ 中有 4 个队列。

try {
ActiveMQConnection.makeConnection(URL).start();
Set<ActiveMQQueue> allque= ActiveMQConnection.makeConnection().getDestinationSource().getQueues();

Iterator<ActiveMQQueue> itr= allque.iterator();
while(itr.hasNext()){
ActiveMQQueue q= itr.next();
System.out.println(q.getQueueName());
}
} catch (Exception e) {

e.printStackTrace();
}

请让我知道我的代码中的任何更正或一些新代码来完成它。

【问题讨论】:

  • 您使用该代码会遇到哪些错误和/或 putput?
  • 我没有收到任何错误或异常,也没有收到任何输出。

标签: java activemq


【解决方案1】:

这对我有用

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 {
            System.Out.Println(activeMQQueue.getQueueName());
            
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    connection.close();re

【讨论】:

    【解决方案2】:

    你必须在同一个连接上调用 getDestinationSource().getQueues()

    try {
        ActiveMQConnection conn = ActiveMQConnection.makeConnection(URL);
        conn.start();
    
        Set<ActiveMQQueue> allque= conn.getDestinationSource().getQueues();
        Iterator<ActiveMQQueue> itr= allque.iterator();
        while(itr.hasNext()){
          ActiveMQQueue q= itr.next();
          System.out.println(q.getQueueName());
        }
    } catch (Exception e) {
    
    e.printStackTrace();
    }
    

    【讨论】:

      【解决方案3】:

      Destination Source 功能不能保证在 Broker 上找到目的地。在许多情况下,该功能可能无法提供任何结果,例如代理上的咨询功能被禁用或客户端已配置为不监视咨询。您还可以立即查询目的地,这并不一定允许将建议从代理发送给客户所需的时间。

      更可靠的机制是代理上的JMX support,它提供了获取目的地列表的方法以及有关正在运行的代理实例的大量其他信息。

      有很多 articles 展示了如何将 JMX 与 ActiveMQ 结合使用。

      【讨论】:

      • 如果你能用一些代码例子来解释一下就好了。
      • 尝试谷歌搜索,已经有大量关于这个主题的文章。您还可以查看 ActiveMQ 单元测试以获取使用 JMX API 的示例。
      猜你喜欢
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2013-02-18
      • 2016-11-09
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多