ActiveMQ delete specific message

2019-08-06 02:19发布

问题:

I use the following code to recieve messages without deletion. Now I need to add some filtering and delete some of the messages. My question is how to delete a specific message while other messages should stay in the queue?

    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));
            }
        }
    }

回答1:

Use selectors and get these messages.

When creating the consumer, create it by IMessageConsumer consumer = session.CreateConsumer(destination,"REFCODE > 200") or similar depending what you need to look for. Then just call consumer.receive(timeout_val); until you pulled all selected messages of the queue. Info about AMQ selectors



标签: c# activemq