在 C#.net 中列出 ActiveMQ Apache.NMS 中的可用队列

Listing available queues in ActiveMQ Apache.NMS in C#.net

有没有办法列出ActiveMQ 中的所有活动队列。我正在使用 Apache.NMS 从 C# 与 ActiveMQ 进行通信。

我在库中没有看到任何直接方法。

请试试这个。

public void Test(ISession Session)
{    
    String QUEUE_ADVISORY_DESTINATION = "ActiveMQ.Advisory.Queue";
    IDestination dest = Session.GetTopic(QUEUE_ADVISORY_DESTINATION);

    using (IMessageConsumer consumer = Session.CreateConsumer(dest))
    {
        IMessage advisory;

        while ((advisory = consumer.Receive()) != null)
        {
            ActiveMQMessage amqMsg = advisory as ActiveMQMessage;

            if (amqMsg.DataStructure != null)
            {
                DestinationInfo info = amqMsg.DataStructure as DestinationInfo;
                if (info != null)
                {
                    Console.WriteLine("   Queue: " + info.Destination.ToString());
                }
            }
        }
    }
    Console.WriteLine("Listing Complete.");    
}

这不是列出队列的可靠方式。请参阅 this answer

上述解决方案包含一个缺陷。如果您尝试接收这样的消息:

consumer.Receive()

您将等待无限长的时间,而新事件不会发生,队列信息进入 ActiveMQ。

我建议设置超时:

consumer.Receive(TimeSpan.FromMilliseconds(2000))

并查看示例 offitial site