具有多个消费者的 JMS 队列
我有一个带有 HornetQ 的 JBoss-6 服务器和一个队列:
<queue name="my.queue">
<entry name="/queue/test"/>
</queue>
有不同的消费者(在不同的机器上)连接到该队列,但一次只有一个消费者处于活动状态。如果我关闭该消费者,消息将立即由其他消费者之一处理。
由于我的消息有一些耗时的处理,我希望多个消费者同时处理他们的唯一消息。
我记得 JBoss 的早期版本中也有类似的情况,该设置运行没有问题。在 Jboss-6 中,消息传递系统运行良好——除了上述问题。这个问题类似于Are multiple client Consumer possible in hornetq?,但场景与我的不相似。
更新 1:如果我关闭 (STRG+C) 一个消费者,则会出现短暂的超时(直到服务器识别出丢失的消费者),直到下一个消费者收到消息。
更新 2:代码片段
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();
和 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) {}
}
}
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) {}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当队列中有多个消费者时,消息在消费者之间进行负载平衡。
由于您需要花费一些时间来消耗消息,因此您应该通过设置 Consumer-window-size 来禁用缓冲。
在 hornetQ 上有一个关于发行版的示例,介绍如何禁用客户端缓冲并为慢速消费者提供更好的支持。 (慢速消费者是指需要一些时间处理消息的消费者)
消息系统会将消息预取/预读到客户端缓冲区,以加快处理速度并避免网络延迟。如果您有快速处理队列和单个消费者,这不是问题。
JBoss Messaging 在连接工厂提供慢速消费者选项,hornetq 提供消费者窗口大小。
大多数消息系统都会为您提供一种启用或禁用客户端预取的方法。
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.
很抱歉,但我无法理解问题到底是什么。我们在2.0.0.GA版本和2.2.2.Final中使用了hornetq。在这两种情况下,基于队列的负载平衡都可以正常工作。如果您为一个队列定义多个消费者并且所有消费者都处于活动状态,则消息将自动在它们之间分发。第一条消息发送给消费者 A,第二条消息发送给消费者 B,第三条消息发送给消费者 C,依此类推。这就是具有多个消费者的队列的工作方式 - 这是免费的负载平衡:) 当您关闭一个消费者时,其他消费者会收到更多消息,这是正常的。
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.