简体   繁体   English

读取ActiveMQ消息而不删除

[英]Reading ActiveMQ messages without deletion

I am trying to read through ActiveMQ messages and according some filters to process some of them or leave the other messages in the queue. 我试图通过ActiveMQ消息读取并根据一些过滤器来处理其中一些消息或将其他消息留在队列中。 I use NMS API with the following code: 我使用NMS API和以下代码:

        Uri connecturi = new Uri("activemq:tcp://model.net:61616");
        IConnectionFactory factory = new NMSConnectionFactory(connecturi);
        List<ModelBuilderBase> result = new List<ModelBuilderBase>();
        using (IConnection connection = factory.CreateConnection())
        using (ISession session = connection.CreateSession())
        {
            IDestination destination = SessionUtil.GetDestination(session, "queue://cidModelbuilderQ");
            using (IMessageConsumer consumer = session.CreateConsumer(destination))
            {
                connection.Start();
                ITextMessage message;
                while ((message = consumer.ReceiveNoWait() as ITextMessage) != null)
                {
                    if (message.Properties[MANDATOR] == null || message.Properties[REFCODE] == null)
                        continue;
                    var mandator = message.Properties[MANDATOR].ToString();
                    var refCode = message.Properties[REFCODE].ToString();
                    result.Add(ModelBuilderFactory.Instance.GetInstance(refCode, mandator));
                }
            }

Problem is that after a message is received the message is deleted. 问题是收到消息后消息被删除。 Can I somehow change this behavior and delete the messages only manually after a successful processing? 我可以以某种方式更改此行为并仅在成功处理后手动删除消息吗? } }

Creates a QueueBrowser object to peek at the messages on the specified queue. 创建QueueBrowser对象以查看指定队列上的消息。 Perform you logic on the message and then creates a QueueReceiver object to receive messages from the specified queue. 对消息执行逻辑,然后创建QueueReceiver对象以从指定的队列接收消息。

Although it was not easy to write a working code for that, thanks to the ARSs answer I have now this working solution: 虽然为此编写一个工作代码并不容易,但由于ARS的答案,我现在有了这个有效的解决方案:

        Uri connecturi = new Uri("activemq:tcp://model.net:61616");
        IConnectionFactory factory = new NMSConnectionFactory(connecturi);
        List<ModelBuilderBase> result = new List<ModelBuilderBase>();
        using (IConnection connection = factory.CreateConnection())
        using (ISession session = connection.CreateSession())
        {

            IDestination destination = SessionUtil.GetDestination(session, "queue://cidModelbuilderQ");
            using (IMessageConsumer consumer = session.CreateConsumer(destination))
            {
                connection.Start();
                var q = session.GetQueue("cidModelbuilderQ");
                var b = session.CreateBrowser(q);
                var msgs = b.GetEnumerator();
                while (msgs.MoveNext())
                {
                    ITextMessage message = msgs.Current as ITextMessage;
                    if (message.Properties[MANDATOR] == null || message.Properties[REFCODE] == null)
                        continue;
                    var mandator = message.Properties[MANDATOR].ToString();
                    var refCode = message.Properties[REFCODE].ToString();
                    result.Add(ModelBuilderFactory.Instance.GetInstance(refCode, mandator));
                }
            }
        }

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

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