問題描述
我將 RabbitMQ 與 Spring 的 RabbitTemplate 一起使用.
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.
之前我們使用 JmsTemplate 并且任何發送或接收的調用都會自動創建隊列.
Previously we have used JmsTemplate and any call to send or receive automatically created the queue.
推薦答案
是的,你可以使用 RabbitAdmin
和 admin.getQueueProperties()
來查看隊列是否存在和 admin.declareQueue(new Queue(...))
添加隊列.您可能應該跟蹤您已經檢查/創建了哪些,以避免每次發送的開銷.
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.
這篇關于Spring RabbitTemplate - 如何在發送時自動創建隊列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!