問題描述
我將 RabbitMQ 與 Spring 的 RabbitTemplate 一起使用.
I am using RabbitMQ together with Spring's RabbitTemplate.
當(dāng)使用模板發(fā)送方法向隊列發(fā)送消息時,我希望隊列不存在時自動創(chuàng)建/聲明.
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.
這非常重要,因為根據(jù)我們的業(yè)務(wù)邏輯,隊列名稱是在運(yùn)行時生成的,我無法提前聲明它們.
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 并且任何發(fā)送或接收的調(diào)用都會自動創(chuàng)建隊列.
Previously we have used JmsTemplate and any call to send or receive automatically created the queue.
推薦答案
是的,你可以使用 RabbitAdmin
和 admin.getQueueProperties()
來查看隊列是否存在和 admin.declareQueue(new Queue(...))
添加隊列.您可能應(yīng)該跟蹤您已經(jīng)檢查/創(chuàng)建了哪些,以避免每次發(fā)送的開銷.
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.
這篇關(guān)于Spring RabbitTemplate - 如何在發(fā)送時自動創(chuàng)建隊列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!