CptPicard
CptPicard

Reputation: 325

RabbitMQ channels and threads in Java client

I'd like a quick confirmation of what I suspect this part of the RabbitMQ documentation says:

Callbacks to Consumers are dispatched on a thread separate from the thread managed by the Connection. This means that Consumers can safely call blocking methods on the Connection or Channel, such as queueDeclare, txCommit, basicCancel or basicPublish.

Each Channel has its own dispatch thread. For the most common use case of one Consumer per Channel, this means Consumers do not hold up other Consumers. If you have multiple Consumers per Channel be aware that a long-running Consumer may hold up dispatch of callbacks to other Consumers on that Channel.

I have various commands (messages) coming in through a single inbound queue and channel which has a DefaultConsumer attached to it. Is it correct to assume that there is a threadpool in DefaultConsumer that lets me run application logic straight off the consumer callback method, and I'm not blocking the processing of later commands? And that if it seems like there's a bottleneck, I can just give RMQ a bigger threadpool?

In addition, occasionally there is a basicPublish to the same channel from other threads. I take it that this does hold up the consumers? I guess I should make use of a new channel when doing this?

Upvotes: 2

Views: 6490

Answers (1)

wheleph
wheleph

Reputation: 8354

The thread pool you mentioned is not a part of DefaultConsumer but rather a part of Connection that is shared between its Channels and DefaultConsumers. It allows different consumers be invoked in parallel. See this part of the guide.

So you would expect that by increasing size of the thread pool you can reach higher level of parallelism. However that's not the only factor that influences it.

There's a big caveat: incoming messages flowing though a single channel are processed serially no matter how many threads you have in the thread pool. It's just the way how ConsumerWorkService is implemented.

So to be able to consume incoming messages concurrently you have either to manage multiple channels or to put those messages into a separate thread pool.

Publishes do not use threads from the Connections's thread pool so they do not hold up consumers.

For more details you may check this post.

Upvotes: 7

Related Questions