Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

java - JMS queue with multiple consumers

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) {}
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...