RabbitMQ configuration for multiple queues

Apparently I had configured a single queue for receiving user's posts in my application. Below is the configuration.

<!-- Creates a queue for consumers to retrieve messages -->
<rabbit:queue name="UserPostpublishQueue" durable="true">
</rabbit:queue>

<!-- queue for sending notifications to users -->
<rabbit:queue name="notificationQueue" durable="true"/>


<!-- Fanout exchange for a pubsub bound to UserPostpublishQueue -->
<fanout-exchange name="broadcastEvents" durable="true" xmlns="http://www.springframework.org/schema/rabbit">
    <bindings>
        <binding queue="UserPostpublishQueue"/>
    </bindings>
</fanout-exchange>

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" retry-template="retryTemplate" 
exchange="broadcastEvents" channel-transacted="true"/>

In the code I just autowired the AMQP template

@Autowired
    AmqpTemplate amqpTemplate;
    amqpTemplate.convertAndSend(post);

Now I have to introduce another notification queue. I am not sure how to do that. Do I bind the queue with the same fanout exchange, on doing that the exchange is pushing the user's post into the notification queue.

Or do I create another fanout exchange & then bind the notification queue with that exchange but then how do I register this new exchange with the amqp template?

Or do I create a direct exchange for both user post & notification queue?

I am not sure. Any help would be appreciated.


You should start from the business requirements description.

All binding variants mentioned by are valid.

  • You really can add one more queue to the fanout exchange - and the same message will be placed to all queues. Topic behavior.

  • You can create another exchange and bind that queue there. In this case the direct exchange would be enough. To send the message exactly to that queue (maybe something different than post), you should use different AmqpTemplate method:

    void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
    
  • Maybe for you it would be better to go to the RabbitMQ tutorials and study all possible configurations.

    We recently also added Spring AMQP implementation for those tutorials to the Spring AMQP Samples project.

    链接地址: http://www.djcxy.com/p/34216.html

    上一篇: 最佳实践

    下一篇: 用于多个队列的RabbitMQ配置