[rabbitmq-discuss] Channel thread safety in Java client

Jiri Krutil jiri at krutil.com
Tue Jul 5 09:42:00 BST 2011


Hi

I'm trying to understand the limitations of using a Channel in a
multi-threaded application using the Java client. Sorry for this longish
post...

I have read http://www.rabbitmq.com/api-guide.html#channel-threads but
did not find any other, more detailed info.

I want to receive messages by subscription. I create a Channel and
invoke basicConsume() on it, passing a QueueingConsumer.

The moment I call basicConsume(), I would imagine the Channel becomes
"owned" by the client lib's connection thread, which feeds messages to
QueueingConsumer.

My app is retrieving those messages from the QueueingConsumer in the
main thread. I would like to know to which extent can I keep using the
Channel passed to basicConsume() from the main thread.

I guess I can use it to ack messages, because ack would not work on
another channel, correct? Can I also use the channel for other things,
such as sending messages, using transactions, or starting another
subscription?

JMS specs seem to be quite explicit on this. It says a session with an
active subscription is owned by the connection thread and client app
threads cannot use the session; they can only shut it down. That's why
if I want multiple subscriptiopns on one session, I must set them all up
before I call Connection.start(), which passes the session ownership to
the client library. Afterwards I can no longer use the session from my
threads except closing it.

Could you please elaborate on how exactly this works in the rabbit Java
client?

Cheers
Jiri



More information about the rabbitmq-discuss mailing list