RabbitMQ:如何指定要发布到的队列?

RabbitMQ 的 Channel#basicConsume 方法为我们提供了以下参数:

RabbitMQ's Channel#basicConsume method gives us the following arguments:

channel.basicConsume(queueName, autoAck, consumerTag, noLocal,
    exclusive, arguments, callback);

让我们能够准确地告诉 RabbitMQ 我们想从哪个队列消费.

Giving us the ability to tell RabbitMQ exactly which queue we want to consume from.

但是 Channel#basicPublish 没有这样的等价性:

But Channel#basicPublish has no such equivalency:

channel.basicPublish(exchangeName, routingKey, mandatory, immediateFlag,
    basicProperties, messageAsBytes);

为什么我不能在这里指定要发布到的队列?!?如何让 Channel 发布到名为 logging 的队列?提前致谢!

Why can't I specify the queue to publish to here?!? How do I get a Channel publishing to, say, a queue named logging? Thanks in advance!

推荐答案

队列基本上可以基于routingKeys绑定到一个exchange.

Basically queues can be binded to an exchange based on routingKeys.

假设您有 3 个不同的发布商.
Publisher1发送消息与routingKey事件"进行交换
Publisher2 发送消息与 routingKey tasks"进行交换
Publisher3 发送消息与 routingKey jobs"进行交换

Assume that you have 3 different publishers.
Publisher1 sending message to exchange with routingKey "events"
Publisher2 sending message to exchange with routingKey "tasks"
Publisher3 sending message to exchange with routingKey "jobs"

您可以有一个只使用具有特定 routhingKey 的消息的消费者.
例如,为了让您声明这样的事件"消息的消费者

You can have a consumer that consumes only messages with specific routhingKey.
For example in order to have a consumer for "events" messages you declare like this

 channel.queueBind(queueName, exchangeName, "events");

如果你想消耗所有进入交换的消息,你将路由指定为'#'

If you want to consume all the messages coming to the exchange you give the routing as '#'

总之我能说的是,
1. 消息将发布到交易所.
2. 队列会根据routingKeys绑定交换.
3. RabbitMQ 会将路由键匹配的消息转发到对应的队列中.

So in short what i can say is,
1. Messages will be published to an exchange.
2. Queues will be bound to exchange based on routingKeys.
3. RabbitMQ will forward messages with matching routing keys to the corresponding queues.

请看教程 - http://www.rabbitmq.com/tutorials/tutorial-three-java.html

RabbitMQ 中消息传递模型的核心思想是生产者永远不会将任何消息直接发送到队列.实际上,生产者通常根本不知道消息是否会被传递到任何队列.相反,生产者只能向交换器发送消息

相关文章