Spring RabbitTemplate - How to create queues automatically upon send

只愿长相守 提交于 2020-01-03 10:52:47

问题


I am using RabbitMQ together with Spring's RabbitTemplate.

When sending messages to queues using the template send methods, I want the queue to automatically be created/declared if it is not already exists.

It is very important since according to our business logic queue names are generated on run-time and I cannot declare them in advance.

Previously we have used JmsTemplate and any call to send or receive automatically created the queue.


回答1:


Yes, you can use a RabbitAdmin and admin.getQueueProperties() to see if the queue exists and admin.declareQueue(new Queue(...)) to add a queue. You should probably keep track of which one's you've already checked/created in order to avoid the overhead on every send.

You can also add exchanges and bind queues to them with the admin.




回答2:


You can use a RabbitAdmin to automatically declare the exchange, queue, and binding. Check out this thread for more detail. This forum also bit related to your scenario. I have not tried spring with AMQP though, but I believe this would do it.

/**
 * Required for executing adminstration functions against an AMQP Broker
 */
@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory());
}

Keep coding !



来源:https://stackoverflow.com/questions/46872274/spring-rabbittemplate-how-to-create-queues-automatically-upon-send

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!