rabbitMq自动创建队列
生活随笔
收集整理的這篇文章主要介紹了
rabbitMq自动创建队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們在rabbitmqConfig中配置隊列,交換機,綁定關系的時候,啟動后,我們會發現rabbitmq中并沒有幫我們自動創建這些信息,那么什么時候會創建呢?就是我們連上監聽rabbitmq的時候,rabbitmq發現沒有這些組件就會自動創建,所以,我們可以在rabbitmqConfig中,編寫一個監聽方法,讓rabbitmq在應用一啟動的時候就可以直接創建。
@Configuration public class RabbitmqConfig {//用于自動創建組件的方法@RabbitListener(queues = "stock.release.stock.queue")public void handle(){}/*** 使用JSON序列化機制,進行消息轉換* @return*/@Beanpublic MessageConverter messageConverter() {return new Jackson2JsonMessageConverter();}/*** 庫存服務默認的交換機* @return*/@Beanpublic Exchange stockEventExchange() {//String name, boolean durable, boolean autoDelete, Map<String, Object> argumentsTopicExchange topicExchange = new TopicExchange("stock-event-exchange", true, false);return topicExchange;}/*** 普通隊列* @return*/@Beanpublic Queue stockReleaseStockQueue() {//String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> argumentsQueue queue = new Queue("stock.release.stock.queue", true, false, false);return queue;}/*** 延遲隊列* @return*/@Beanpublic Queue stockDelay() {HashMap<String, Object> arguments = new HashMap<>();arguments.put("x-dead-letter-exchange", "stock-event-exchange");arguments.put("x-dead-letter-routing-key", "stock.release");// 消息過期時間 2分鐘arguments.put("x-message-ttl", 120000);Queue queue = new Queue("stock.delay.queue", true, false, false,arguments);return queue;}/*** 交換機與普通隊列綁定* @return*/@Beanpublic Binding stockLocked() {//String destination, DestinationType destinationType, String exchange, String routingKey,// Map<String, Object> argumentsBinding binding = new Binding("stock.release.stock.queue",Binding.DestinationType.QUEUE,"stock-event-exchange","stock.release.#",null);return binding;}/*** 交換機與延遲隊列綁定* @return*/@Beanpublic Binding stockLockedBinding() {return new Binding("stock.delay.queue",Binding.DestinationType.QUEUE,"stock-event-exchange","stock.locked",null);}}總結
以上是生活随笔為你收集整理的rabbitMq自动创建队列的全部內容,希望文章能夠幫你解決所遇到的問題。