Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading ActiveMQ messages without deletion

Tags:

c#

activemq

nms

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. I use NMS API with the following code:

        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? }

like image 973
Scarlaxx Avatar asked Dec 22 '22 06:12

Scarlaxx


1 Answers

Although it was not easy to write a working code for that, thanks to the ARSs answer I have now this working solution:

        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));
                }
            }
        }
like image 66
Scarlaxx Avatar answered Jan 05 '23 12:01

Scarlaxx