7
votes

I'm implementing the request/reply pattern in RabbitMQ using Java. I know that channels are not thread-safe, so use 1 channel per consumer/thread.

I wonder if there are any problems or inefficienies when a single channel is used both to consume and publish messages, or receive requests and return responses in my case, like the code below, it's from here:

channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());

Should I use two different channels for consuming and publishing messages?

2

2 Answers

0
votes

As per Rabbitmq java api guide - https://www.rabbitmq.com/api-guide.html

Consuming in one thread and publishing in another thread on a shared channel can be safe.

Mentioned in the Channels and Concurrency Considerations (Thread Safety) section.

-1
votes

You should use different connections for publishing and consuming, since publishing connections can be throttled down by RabbitMQ. If you have your consumers in the same connection they might be blocked as well.

https://www.rabbitmq.com/blog/2015/10/06/new-credit-flow-settings-on-rabbitmq-3-5-5/