本文介绍了在 hornetq 中是否可以有多个客户端消费者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的客户端应用程序中,我创建了多个消费者,但他们无法同时处理队列.始终只有一个消费者处理队列消息.我不知道为什么.

In my client application, I create several consumers, but they can't concurrently process the queue. Always, only a single consumer processes the queue messages. I don't know why.

Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "192.168.1.111:1099");
InitialContext ctx = new InitialContext(env);

ConnectionFactory cf = (ConnectionFactory) ctx.lookup("/ConnectionFactory");
Queue downQueue = (Queue) ctx.lookup("queue/DownQueue");
Session consumerSession = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageConsumer consumer;
for (int i = 0; i < 2; i++) {
    consumer = consumerSession.createConsumer(downQueue, "proxyId=0");
    consumer.setMessageListener(listener);
}

如何处理具有多个并发消费者的队列?

How do I process the queue with multiple concurrent consumers?

推荐答案

把它想象成线程和会话之间的一对一.(连接是线程安全的,下面"的所有内容都不是).所以简而言之,创建多个线程,让每个线程创建一个会话等等.每个线程都会消耗.

Think of it as 1 to 1 between threads and sessions. (Connections are thread safe, everything "below" is not). So in short, create multiple threads, have each thread create a session etc. And each thread will consume.

这篇关于在 hornetq 中是否可以有多个客户端消费者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:45