RabbitMQ: How to specify the queue to publish to?

前端 未结 3 1122
逝去的感伤
逝去的感伤 2021-02-08 01:43

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

channel.basicConsume(queueName, autoAck, consumerTag, noLocal,
    exclusi         


        
3条回答
  •  忘掉有多难
    2021-02-08 02:11

    To expand on @Tien Nguyen's answer, there is a "cheat" in RabbitMQ that effectively lets you publish directly to a queue. Each queue is automatically bound to the AMQP default exchange, with the queue's name as the routing key. The default exchange is also known as the "nameless exchange" - ie its name is the empty string. So if you publish to the exchange named "" with routing key equal to your queue's name, the message will go to just that queue. It is going through an exchange as @John said, it's just not one that you need to declare or bind yourself.

    I don't have the Java client handy to try this code, but it should work.

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

    That said, this is mostly contrary to the spirit of how RabbitMQ works. For normal application flow you should declare and bind exchanges. But for exceptional cases the "cheat" can be useful. For example, I believe this is how the Rabbit Admin Console allows you to manually publish messages to a queue without all the ceremony of creating and binding exchanges.

提交回复
热议问题