消费者选择器在activeMQ上的问题 [英] Consumer's selector issue on activeMQ

查看:268
本文介绍了消费者选择器在activeMQ上的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我向名为JUST.CN的队列发送了五万条消息。并每1000条消息设置一个消息属性字符串 myfilter ='abc'。现在,我创建具有相同选择器的使用者来使用消息。但是,尤其是在30000条消息之后,使用速度非常慢。我无法在activeMQ中更改默认配置。
核心代码如下:

The scenario:I send fifty thousand messages to the queue named JUST.CN. And seting one message propertyString "myfilter='abc'" every 1000 message.Now I create consumer with the same selctor to consume messages.However the comsuming rate is very slow especialy after 30000 messages.I cant change default configuration in activeMQ. The Core Code is below:

 IDestination destination = SessionUtil.GetDestination(session, "JUST.CN");
                    IMessageProducer producer = session.CreateProducer(destination);
                    string msg = "Hello hello hello world!~~testing~Hello hello hello world!~~testing~";

                    for (int i = 0; i < 50000; i++)
                    {
                        ITextMessage message;

                        if (i % 1000 == 0)
                        {
                            message = session.CreateTextMessage(msg);
                            message.Properties.SetString("myfilter", "abc");
                        }
                        else
                        {
                            message = session.CreateTextMessage(msg);
                        }
                        producer.Send(message, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MinValue);
                    }

消费者代码:

 IDestination destination = SessionUtil.GetDestination(session, "JUST.CN");             
 IMessageConsumer consumer = session.CreateConsumer(destination, "myfilter='abc'", false);

                    int count = 0;
                    DateTime dtstart = DateTime.Now;
                 for (int i = 0; i < 50; i++)
                    {
                        IMessage iMsg = consumer.Receive();
                        ITextMessage msg = (ITextMessage)iMsg;
                        Console.WriteLine(msg.Text);
                        count++;

                    }
                    DateTime dtend = DateTime.Now;
                    TimeSpan time = dtend - dtstart;
                    Console.WriteLine(time);
                    Console.WriteLine(count);

对ActiveMQ的选择器是否需要使用任何特殊设置?
谢谢您的任何输入。

Is there any special setting that I need to use for the selectors to ActiveMQ? Thank you in advance for any inputs.

推荐答案

通常,将消息选择器与队列一起使用是一种反模式。几年前的博客中有一篇很好的文章,为什么写在关于中间件的帮助

In general, using message selectors with queues is an anti-pattern. There's a good article on why this is on a blog from a few years back at Ade on Middleware.

如果您正在队列中使用消息选择器,则用例通常是某些消费者只对一些消息。您可以通过使用配置在复合目标上的方法来更好地解决此用例。应用筛选器(通过 filteredDestination )的筛选器,该筛选器等效于选择逻辑:

If you are looking at using message selectors on queues, the use case is generally that certain consumers are interested in only some messages. You can address this use case in a much nicer way through the use of composite destinations configured on the broker that apply a filter (via filteredDestination) that is the equivalent of the selection logic:

<broker xmlns="http://activemq.apache.org/schema/core">
  <destinationInterceptors>
    <virtualDestinationInterceptor>
      <virtualDestinations>
        <compositeQueue name="myapp.in" forwardOnly="true">
          <forwardTo>
            <filteredDestination selector="myHeader > 5" queue="myapp.out.high"/>
            <filteredDestination selector="myHeader <= 5" queue="myapp.out.low"/>
          </forwardTo>
        </compositeQueue>
      </virtualDestinations>
    </virtualDestinationInterceptor>
  </destinationInterceptors>
</broker> 

这里发生的是,当消息到达<$ c时,对消息运行SQL92筛选器$ c> myapp.in 队列,然后对消息进行适当排序。想要只消费高消息的订阅者订阅 myapp.out.high

What happens here is that the SQL92 filter is run against the message when it arrives in the myapp.in queue, and the messages are sorted appropriately. A subscriber that wants to consume only the high messages subscribes to myapp.out.high.

通过执行此操作,可以有效地将问题彻底解决,并消除了在使用消息时进行复杂处理的需要。

By doing this you are effectively turning the problem upside down and removing the need for complicated processing when consuming messages.

这篇关于消费者选择器在activeMQ上的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆