1

我使用以下代码在不删除的情况下接收消息。现在我需要添加一些过滤并删除一些消息。我的问题是如何删除特定消息而其他消息应保留在队列中?

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

1 回答 1

3

使用选择器并获取这些消息。

创建消费者时,通过 IMessageConsumer consumer = session.CreateConsumer(destination,"REFCODE > 200") 或类似内容创建它,具体取决于您需要查找的内容。然后只需调用 consumer.receive(timeout_val); 直到您拉出队列中所有选定的消息。 关于 AMQ 选择器的信息

于 2012-07-10T12:23:16.280 回答