java - Java 客户端中的 RabbitMQ channel 和线程

标签 java multithreading rabbitmq

我想快速确认一下我怀疑 RabbitMQ 文档的这一部分所说的内容:

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.

我有各种命令(消息)通过单个入站队列和 channel 传入,该队列和 channel 附加了一个 DefaultConsumer。假设 DefaultConsumer 中有一个线程池可以让我直接从消费者回调方法运行应用程序逻辑,并且我不会阻止后续命令的处理,这是否正确?如果看起来有瓶颈,我可以给 RMQ 一个更大的线程池吗?

另外,偶尔会有一个basicPublish从其他线程到同一个 channel 。我认为这确实支撑了消费者?我想我应该在执行此操作时使用新 channel 吗?

最佳答案

您提到的线程池不是 DefaultConsumer 的一部分,而是 Connection 的一部分,在其 ChannelDefaultConsumers。它允许并行调用不同的消费者。参见 this part of the guide .

因此您会期望通过增加线程池的大小可以达到更高级别的并行性。然而,这并不是影响它的唯一因素。

有一个很大的警告:无论您在线程池中有多少线程,流经单个 channel 的传入消息都会被串行处理。这就是 ConsumerWorkService 的实现方式。

因此,为了能够同时使用传入的消息,您必须管理多个 channel 或将这些消息放入单独的线程池中。

发布不使用来自 Connections 线程池的线程,因此它们不会阻碍消费者。

更多详情可以查看this post .

关于java - Java 客户端中的 RabbitMQ channel 和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345903/

相关文章:

java - 如何为程序只填充一次HashMap对象?

multithreading - Perl 线程和不安全信号

调用 free 导致程序抛出异常

java - 更改 RabbitMQ 中现有队列的路由键

rabbitmq-plugins 无法以rabbitmq 用户身份运行

erlang - RabbitMQ 插件依赖

Java 8 嵌套流 : return a value in last stream

java - 如何向 String 数据类型的 ArrayList 添加数据

java - Spring Security 3.2.4 中的 ConcurrentSessionControlStrategy

c# - 线程逐渐增加处理器使用率