JMS queue with multiple consumers

Thor picture Thor · Aug 10, 2011 · Viewed 17.2k times · Source

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

Answer

Clebert Suconic picture Clebert Suconic · Aug 10, 2011

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.