RabbitMQ: How to specify the queue for publication?

The RabbitMQ Channel#basicConsume gives us the following arguments:

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

Allowing us to tell RabbitMQ exactly which queue we want to consume.

But Channel#basicPublish does not have this equivalence:

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

Why can not I specify the queue for publication here?!? How to get a Channel publication, for example, in a queue named logging ? Thanks in advance!

+6
source share
3 answers

Basically, queues can be tied to an exchange based on routingKeys.

Suppose you have 3 different publishers.
Publisher1 sends a message to exchange with routingKey "events"
Publisher2 sending message for exchange with routingKey "tasks"
Publisher3 sending message for exchange with routingKey "jobs"

You may have a consumer that consumes only messages with a specific type of routing.
For example, in order to have a consumer for event messages that you announce as follows

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

If you want to use all the messages coming to the exchange, you pass the route as "#"

In short, I can say
1. Messages will be published on the exchange.
2. Queues will be required to exchange based on routingKeys.
3. RabbitMQ forwards messages with the appropriate routing keys to the appropriate queues.

See the tutorial - http://www.rabbitmq.com/tutorials/tutorial-three-java.html

The main idea in the messaging model in RabbitMQ is that the producer never sends messages directly to the queue. Actually, often the manufacturer does not even know whether the message will be delivered to any queue at all. Instead, the manufacturer can send messages only for exchange

+13
source

To extend the answer to @Tien Nguyen, RabbitMQ has a cheat that effectively allows publishing directly to the queue. Each queue is automatically tied to the default exchange AMQP, with the queue name as a routing key. The default exchange is also known as the "nameless exchange", that is, its name is an empty string. Therefore, if you publish in exchange with the name "" with a routing key equal to your queue name, the message is sent only to this queue. It goes through an exchange, as Johnson said, it's just not the one you need to declare or link.

I do not have a Java client that can try this code, but it should work.

 channel.basicPublish("", myQueueName, false, false, null, myMessageAsBytes); 

However, this is largely contrary to the spirit of how RabbitMQ works. For a normal flow of applications, you must declare and link exchanges. But in exceptional cases, a cheat may be useful. For example, I believe that Rabbit Admin Console allows you to manually publish messages in a queue without any ceremony of creating and linking exchanges.

+11
source

try the following:

 channel.basicPublish("", yourQueueName, null, message.getBytes((Charset.forName("UTF-8")))); 

He worked for my project.

0
source

Source: https://habr.com/ru/post/952827/


All Articles