JMS queue with multiple consumers

17,906

Solution 1

With multiple consumers on a queue, messages are load balanced between the consumers.

As you have some time consuming the message, you should disable buffering by setting consumer-window-size.

On hornetQ there's an example on the distribution, about how to disable client buffering and give a better support for slow consumers. (a slow consumer is a consumer that will have some time processing the message)

message systems will pre-fetch/read-ahead messages to the client buffer to speed up processing and avoid network latency. This is not an issue if you have fast processing queues and a single consumer.

JBoss Messaging offered the slow-consumer option at the connection factory and hornetq offers the consumer window size.

Most Message systems will provide you a way to enable or disable client pre-fetching.

Solution 2

I am sorry but I cannot understand what exactly the problem is. We've used hornetq in 2.0.0.GA version and 2.2.2.Final. In both cases, queue-based load balancing works fine. If you will define multiple consumers for one queue and all of them are active, messages will be distributed between them automatically. First message to consumer A, second to consumer B, third to consumer C and so on. This is how queues with multiple consumers works - it's free load balancing :) That's normal that when you shut down one consumer, others would receive more messages.

Share:
17,906
Thor
Author by

Thor

Updated on June 11, 2022

Comments

  • Thor
    Thor almost 2 years

    I have a JBoss-6 server with HornetQ and a single queue:

    <queue name="my.queue">  
        <entry name="/queue/test"/>  
    </queue>
    

    There a different consumers (on different machines) connected to this queue, but only a single consumer is active at a time. If I shut down this consumer, the messages are immediately processed by one of the other consumers.

    Since my messages have some time consuming processing, I want multiple consumer process their unique messages concurrently.

    I remember a similar in earlier versions of JBoss where this setup worked without problems. Here in Jboss-6 the messaging system is working well -- except of the issue described above. This question is similar to Are multiple client consumers possible in hornetq?, but the scenario is not similar to mine.

    Update 1: If I close (STRG+C) one consumer there is a short timeout (until the server recognized the lost consumer) until the next consumer gets the message.

    Update 2: Code Snippet

    VoidListener ml = new VoidListener();
    QueueConnectionFactory qcf = (QueueConnectionFactory)
                                 ctx.lookup("ConnectionFactory");
    QueueConnection conn = qcf.createQueueConnection();
    Queue queue = (Queue) ctx.lookup(queueName);
    QueueSession session = conn.createQueueSession(false,
                                                   QueueSession.AUTO_ACKNOWLEDGE);
    
    QueueReceiver recv = session.createReceiver(queue,"");
    recv.setMessageListener(ml);
    conn.start();
    

    And the MessageListerner:

    public class OlVoidListener implements MessageListener
    {
      public void onMessage(Message msg)
      {
        counter++;
        logger.debug("Message ("+counter+") received");
        try {Thread.sleep(15*1000);} catch (InterruptedException e) {}
      }
    }
    
  • Thor
    Thor almost 13 years
    Well, what you describe is exactly what I expected. But in my setup even with multiple connected consumers (and lots of messages in the queue) only one consumer receives messages.
  • omnomnom
    omnomnom almost 13 years
    Hmm... I have the same queue configuration as you and no other configuration changes. If you have such issue, I'd expect that it's something wrong with your consumers. Do you use MDBs?
  • Thor
    Thor almost 13 years
    The consumers are no MDBs but stand-alone java programs.
  • omnomnom
    omnomnom almost 13 years
    So I would definitely recommend to start issue investigation from consumers code. Can you paste a snippet?
  • Thor
    Thor almost 13 years
    But does this explain why Consumer A is processing a message while Consumer B is idle and there are additional elements waiting in the queue?
  • Clebert Suconic
    Clebert Suconic almost 13 years
    Yes, message systems will pre-fetch/read-ahead messages to the client buffer to speed up processing and avoid network latency. This is not an issue if you have fast processing queues and a single consumer. It will be an issue with slow processing also. JBoss Messaging offered the slow-consumer option at the connection factory and hornetq offers the consumer window size. Most Message systems will provide you a way to enable disable client pre-fetching. I'm placing this to my original answer
  • Thor
    Thor almost 13 years
    This seems to be the right answer, but unfortunately I cannot activate the <consumer-window-size>0</consumer-window-size>, see: stackoverflow.com/questions/7021485
  • Clebert Suconic
    Clebert Suconic almost 13 years
    I have answered your other question. you have used the wrong connector. Look at the right one at your hornetq-configuration.xml