java 具有多个消费者的 JMS 队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7008664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JMS queue with multiple consumers
提问by Thor
I have a JBoss-6 server with HornetQ and a single queue:
我有一个带有 HornetQ 的 JBoss-6 服务器和一个队列:
<queue name="my.queue">
<entry name="/queue/test"/>
</queue>
There a different consumers (on different machines) connected to this queue, but only a singleconsumer 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.
我记得在早期版本的 JBoss 中,这个设置没有问题。在 Jboss-6 中,消息传递系统运行良好——除了上述问题。这个问题类似于在 hornetq 中是否可以有多个客户端消费者?,但场景与我的不相似。
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.
更新 1:如果我关闭 (STRG+C) 一个消费者,则在下一个消费者收到消息之前会出现短暂的超时(直到服务器识别出丢失的消费者)。
Update 2: Code Snippet
更新 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();
And the MessageListerner:
和 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) {}
}
}
采纳答案by Clebert Suconic
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)
在 hornetQ 上有一个关于分发的例子,关于如何禁用客户端缓冲并为慢速消费者提供更好的支持。(慢消费者是将有一些时间处理消息的消费者)
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.
JBoss Messaging 在连接工厂提供慢消费者选项,hornetq 提供消费者窗口大小。
Most Message systems will provide you a way to enable or disable client pre-fetching.
大多数消息系统都会为您提供一种启用或禁用客户端预取的方法。
回答by omnomnom
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.
我很抱歉,但我无法理解问题到底是什么。我们在 2.0.0.GA 版本和 2.2.2.Final 中使用了 hornetq。在这两种情况下,基于队列的负载平衡都可以正常工作。如果您将为一个队列定义多个消费者并且所有消费者都处于活动状态,则消息将在它们之间自动分发。第一条消息给消费者 A,第二条消息给消费者 B,第三条消息给消费者 C,依此类推。这就是具有多个消费者的队列的工作方式 - 它是免费的负载平衡 :) 当您关闭一个消费者时,其他人会收到更多消息是正常的。