問題描述
RabbitMQ 的 Channel#basicConsume
方法為我們提供了以下參數(shù):
RabbitMQ's Channel#basicConsume
method gives us the following arguments:
channel.basicConsume(queueName, autoAck, consumerTag, noLocal,
exclusive, arguments, callback);
讓我們能夠準(zhǔn)確地告訴 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);
為什么我不能在這里指定要發(fā)布到的隊列?!?如何讓 Channel
發(fā)布到名為 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.
假設(shè)您有 3 個不同的發(fā)布商.
Publisher1發(fā)送消息與routingKey事件"進行交換
Publisher2 發(fā)送消息與 routingKey tasks"進行交換
Publisher3 發(fā)送消息與 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. 消息將發(fā)布到交易所.
2. 隊列會根據(jù)routingKeys綁定交換.
3. RabbitMQ 會將路由鍵匹配的消息轉(zhuǎn)發(fā)到對應(yīng)的隊列中.
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 中消息傳遞模型的核心思想是生產(chǎn)者永遠不會將任何消息直接發(fā)送到隊列.實際上,生產(chǎn)者通常根本不知道消息是否會被傳遞到任何隊列.相反,生產(chǎn)者只能向交換器發(fā)送消息
這篇關(guān)于RabbitMQ:如何指定要發(fā)布到的隊列?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!