开发者

Are multiple client consumers possible in 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.


By looking at your code the consumer variable is getting reassigned different consumer objects in the for loop, which may cause the references to the earlier consumer objects lost and garbage collected. Only one consumer object will remain alive-the one which was created last in the for loop- whose reference is maintained by the consumer variable and it will consume all the coming messages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜