Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有C#和Apache NMS的ActiveMQ-对队列中的消息进行计数_C#_Message Queue_Activemq_Apache Nms - Fatal编程技术网

带有C#和Apache NMS的ActiveMQ-对队列中的消息进行计数

带有C#和Apache NMS的ActiveMQ-对队列中的消息进行计数,c#,message-queue,activemq,apache-nms,C#,Message Queue,Activemq,Apache Nms,我正在使用ActiveMQ使用C#应用程序发送和接收消息。但是,我在获取队列中消息的计数时遇到了一些困难。。这是我的密码: public int GetMessageCount() { int messageCount = 0; Uri connecturi = new Uri(this.ActiveMQUri); IConnectionFactory factory = new NMSConnectionFactory(conn

我正在使用ActiveMQ使用C#应用程序发送和接收消息。但是,我在获取队列中消息的计数时遇到了一些困难。。这是我的密码:

    public int GetMessageCount()
    {
        int messageCount = 0;
        Uri connecturi = new Uri(this.ActiveMQUri);

        IConnectionFactory factory = new NMSConnectionFactory(connecturi);

        using (IConnection connection = factory.CreateConnection())
        using (ISession session = connection.CreateSession())
        {
            IDestination requestDestination = SessionUtil.GetDestination(session, this.QueueRequestUri);

            IQueueBrowser queueBrowser = session.CreateBrowser((IQueue)requestDestination);
            IEnumerator messages = queueBrowser.GetEnumerator();

            while(messages.MoveNext())
            {
                messageCount++;
            }

            connection.Close();
            session.Close();
            connection.Close();
        }

        return messageCount;
    }
我想我可以使用QueueBrowser获取计数,但它返回的IEnumerator总是空的。我从这个页面上得到了使用的想法,但也许还有其他方法可以使用

更新:

我在遍历枚举器时发现的“无限循环”问题的解决方案是通过访问当前消息来解决的。它现在只经过一次循环(这是正确的,因为队列中只有一条消息)

新的while循环是:

while(messages.MoveNext())
{
    IMessage message = (IMessage)messages.Current;
    messageCount++;
}

我现在没有带ActiveMq,所以无法尝试 但我认为问题是你没有开始连接。试着这样做:

using (IConnection connection = factory.CreateConnection())
{
    connection.start ();

     using (ISession session = connection.CreateSession())
     {
      //Whatever...
     }

}

谢谢,这将填充QueueBrowser的枚举器,但是尽管队列中只有一条消息,
while(messages.MoveNext())
现在似乎永远在无限循环中运行。从无到有!我修复了循环问题(如上面更新中提到的),尽管我不太确定实际问题是什么。但是,最初的问题是通过启动连接来回答的,因此感谢您的帮助!您可以分享:此代码>SessionUtil.GetDestination |请