0

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

The Consumer's code:

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

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

Yolanda
  • 33
  • 1
  • 7

1 Answers1

3

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.

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> 

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.

Jakub Korab
  • 4,974
  • 2
  • 24
  • 34
  • Thank Jake.Your answer helps me a lot.Creating virtual destination and send message there.This will lead to lots of queue.Is there any other way to do this having single queue? Maybe it is the only way to create one queue for every consumer if sending message to the specific consumer.The system would have 1000 consumers to PTP comunicating.I cant imagine what will happen on the ActiveMQ Server. – Yolanda Jan 15 '14 at 08:59
  • In general, it's OK to have lots of destinations on a broker. It sounds though that you might be using the wrong set of features (queues might not be the best way to tackle your particular problem). If you can you say what you are trying to achieve, I might be able to point you in the right direction. – Jakub Korab Jan 15 '14 at 21:07
  • There will be about 1000 users in our system.I want to realize the point to point communication between each other.(Perhaps there might be aoubt 300 users on-line at the same time).And message would be persistent stored in the activemq broker.For example userA sends one message to userB. userB would receive the message once on-line.And One user would have three kinds of cliend-side such as the client of WebPage、Instant Communication Tool and Mobile.That means one message would be sent to all client-sides of useB.Is there any good way to achieve this?Thanks Jake. – Yolanda Jan 16 '14 at 03:59
  • Is it the same message sent to all 3 destinations each time? – Jakub Korab Jan 16 '14 at 15:09