简体   繁体   中英

Consume and publish messages on the same channel

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?

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM