RabbitMQ:RabbitAdmin 與 RabbitTempla
RabbitAdmin
該類封裝了對 RabbitMQ 的管理操作
@Beanpublic RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){ return new RabbitAdmin(connectionFactory); }@Autowiredprivate RabbitAdmin rabbitAdmin;
Exchange 操作
//建立四種型別的 Exchange,均為持久化,不自動刪除rabbitAdmin.declareExchange(new DirectExchange("direct.exchange",true,false)); rabbitAdmin.declareExchange(new TopicExchange("topic.exchange",true,false)); rabbitAdmin.declareExchange(new FanoutExchange("fanout.exchange",true,false)); rabbitAdmin.declareExchange(new HeadersExchange("header.exchange",true,false));//刪除 ExchangerabbitAdmin.deleteExchange("header.exchange");
Queue 操作
//定義佇列,均為持久化rabbitAdmin.declareQueue(new Queue("debug",true)); rabbitAdmin.declareQueue(new Queue("info",true)); rabbitAdmin.declareQueue(new Queue("error",true));//刪除佇列 rabbitAdmin.deleteQueue("debug");//將佇列中的訊息全消費掉rabbitAdmin.purgeQueue("info",false);
Binding 繫結
//繫結佇列到交換器,透過路由鍵rabbitAdmin.declareBinding(new Binding("debug",Binding.DestinationType.QUEUE, "direct.exchange","key.1",new HashMap())); rabbitAdmin.declareBinding(new Binding("info",Binding.DestinationType.QUEUE, "direct.exchange","key.2",new HashMap())); rabbitAdmin.declareBinding(new Binding("error",Binding.DestinationType.QUEUE, "direct.exchange","key.3",new HashMap()));//進行解綁rabbitAdmin.removeBinding(BindingBuilder.bind(new Queue("info")). to(new TopicExchange("direct.exchange")).with("key.2"));//使用BindingBuilder進行繫結rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("info")). to(new TopicExchange("topic.exchange")).with("key.#"));//宣告topic型別的exchangerabbitAdmin.declareExchange(new TopicExchange("exchange1",true,false)); rabbitAdmin.declareExchange(new TopicExchange("exchange2",true,false));//exchange與exchange繫結rabbitAdmin.declareBinding(new Binding("exchange1",Binding.DestinationType.EXCHANGE, "exchange2","key.4",new HashMap()));
RabbitTemplate
Spring AMQP 提供了 RabbitTemplate 來簡化 RabbitMQ 傳送和接收訊息操作
@Autowiredprivate RabbitAdmin rabbitAdmin;
RabbitTemplate 初始化
設定 RabbitTemplate 的預設交換器、預設路由鍵、預設佇列
傳送訊息
send (自定義訊息 Message)
Message message = new Message("hello".getBytes(),new MessageProperties());// 傳送訊息到預設的交換器,預設的路由鍵rabbitTemplate.send(message);// 傳送訊息到指定的交換器,指定的路由鍵rabbitTemplate.send("direct.exchange","key.1",message);// 傳送訊息到指定的交換器,指定的路由鍵rabbitTemplate.send("direct.exchange","key.1",message,new CorrelationData(UUID.randomUUID().toString()));
convertAndSend(自動 Java 物件包裝成 Message 物件,Java 物件需要實現 Serializable 序列化介面)
User user = new User("linyuan");// 傳送訊息到預設的交換器,預設的路由鍵rabbitTemplate.convertAndSend(user);// 傳送訊息到指定的交換器,指定的路由鍵,設定訊息 IDrabbitTemplate.convertAndSend("direct.exchange","key.1",user,new CorrelationData(UUID.randomUUID().toString()));// 傳送訊息到指定的交換器,指定的路由鍵,在訊息轉換完成後,透過 MessagePostProcessor 來新增屬性rabbitTemplate.convertAndSend("direct.exchange","key.1",user,mes -> { mes.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT); return mes; });
接收訊息
receive(返回 Message 物件)
// 接收來自指定佇列的訊息,並設定超時時間Message msg = rabbitTemplate.receive("debug",2000l);
receiveAndConvert(將返回 Message 轉換成 Java 物件)
User user = (User) rabbitTemplate.receiveAndConvert();
------- 待完善 -------
作者:林塬
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/200/viewspace-2820328/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【RabbitMQ】RabbitMQ與Spring整合MQSpring
- RabbitMQ(三):RabbitMQ與Spring Boot簡單整合MQSpring Boot
- Rabbitmq安裝與配置MQ
- redis與rabbitmq對比RedisMQ
- RabbitMQ系列(三)RabbitMQ交換器Exchange介紹與實踐MQ
- 初識Java與RabbitMQ(三)JavaMQ
- rabbitmq學習與分享(4)MQ
- RabbitMQ 安裝與介面管理MQ
- RabbitMQ與Kafka選型對比MQKafka
- [Open Source] RabbitMQ 安裝與使用MQ
- RabbitMq--與SpringBoot整合MQSpring Boot
- RabbitMQ與AMQP協議詳解MQ協議
- rabbitmq 學習與實踐分享(2)MQ
- rabbitmq 學習與實踐分享(3)MQ
- RabbitMQ 安裝部署與配置總結MQ
- 【RabbitMQ】走進RabbitMQMQ
- 安裝rabbitmq遇到各種問題,多數是erlang與rabbitmq版本不匹配MQ
- RabbitMQ與Erlang的版本對應關係MQ
- Kafka 與 RabbitMQ 如何選擇使用哪個?KafkaMQ
- Spring Boot(十三)RabbitMQ安裝與整合Spring BootMQ
- RabbitMQ如何備份與恢復資料MQ
- Java訊息佇列:RabbitMQ與Kafka的整合與應用Java佇列MQKafka
- RabbitMQ(一):RabbitMQ快速入門MQ
- RabbitMQ(二):RabbitMQ高階特性MQ
- RabbitMQ系列之---初識RabbitMQMQ
- RabbitMQ-Spring整合RabbitMQMQSpring
- RabbitMQ -springboot整合rabbitmqMQSpring Boot
- 【RabbitMQ】Linux安裝RabbitMQMQLinux
- 關於 RabbitMQ 的安裝與簡單使用MQ
- RabbitMQ簡介以及與SpringBoot整合示例MQSpring Boot
- rabbitmq 實踐與學習心得分享(1)MQ
- 與RabbitMQ有關的一些知識MQ
- RabbitMq中的訊息應答與持久化MQ持久化
- rabbitmqMQ
- RabbitMQ學習之(一)_初步瞭解RabbitMQ、RabbitMQ的使用流程、為什麼要使用RabbitMQ、RabbitMQ的應用場景MQ
- 從消費者角度比較Kafka 與 RabbitMQ - OpenCredoKafkaMQ
- CentOS7下RabbitMQ3.7安裝與配置CentOSMQ
- rabbitmq單機多例項叢集與負載均衡MQ負載