简体   繁体   English

获取 ActiveMQ 中的队列名称列表

[英]Get list of queue names in ActiveMQ

I have tried below code to get list of queues in ActiveMQ.我尝试了下面的代码来获取 ActiveMQ 中的队列列表。 But its not working.但它不起作用。 I have 4 queues in my 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();
}

Please let me know any correction in my code or some new code get it done.请让我知道我的代码中的任何更正或一些新代码完成它。

The Destination Source functionality is not a guaranteed way to find the destinations on the Broker. Destination Source 功能不是在 Broker 上找到目的地的保证方式。 The functionality can fail to provide any results in a number of cases such as when the advisory feature on the Broker is disabled or the client has been configured not to watch for advisories.在许多情况下,该功能可能无法提供任何结果,例如当 Broker 上的建议功能被禁用或客户端已配置为不监视建议时。 You also query for destination immediately which doesn't necessarily allow for the time needed for the advisories to be dispatched to the client from the Broker.您还可以立即查询目的地,这不一定允许将建议从 Broker 发送到客户所需的时间。

A more reliable mechanism is the JMX support on the Broker which provides methods for obtaining lists of destinations along with a host of other information about the running broker instance.更可靠的机制是 Broker 上的JMX 支持,它提供了获取目的地列表以及有关正在运行的代理实例的大量其他信息的方法。

There are plenty of articles out there showing how to use JMX with ActiveMQ.有很多文章展示了如何在 ActiveMQ 中使用 JMX。

you have to invoke getDestinationSource().getQueues() on the same connection你必须在同一个连接上调用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();
}

this worked for me这对我有用

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

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

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