Spring Boot 入門(八):集成RabbitMQ消息隊列

本片文章續《Spring Boot 入門(七):集成 swagger2》,關於RabbitMQ的介紹請參考《java基礎(六):RabbitMQ 入門html

1.增長依賴java

1         <!--rabbitMq-->
2         <dependency>
3             <groupId>org.springframework.boot</groupId>
4             <artifactId>spring-boot-starter-amqp</artifactId>
5         </dependency>

2.增長confspring

 1 2 
 3 import lombok.extern.slf4j.Slf4j;  4 import org.springframework.amqp.core.*;  5 import org.springframework.amqp.rabbit.connection.CorrelationData;  6 import org.springframework.amqp.rabbit.core.RabbitTemplate;  7 import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;  8 import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;  9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Value; 11 import org.springframework.context.annotation.Bean; 12 import org.springframework.context.annotation.Configuration; 13 
14 import javax.annotation.PostConstruct; 15 
16 /**
17  * @program: 18  * @description: Rabbit相關配置 19  * @author: DZ 20  * @create: 2019-10-18 17:07 21  **/
22 @Slf4j 23 @Configuration 24 public class RabbitConfig implements ConfirmCallback, ReturnCallback { 25  @Autowired 26     private RabbitTemplate rabbitTemplate; 27 
28     //目前就聲明瞭一個消息隊列 29     // 隊列名稱
30 31     public String queue = "queue"; 32     // 交換機名稱
33    34     public String exchang="exchange"; 35     // 關鍵字
36     37     public String key="key"; 38 
39  @PostConstruct 40     public void init() { 41         rabbitTemplate.setConfirmCallback(this); 42         rabbitTemplate.setReturnCallback(this); 43  } 44 
45     //此主要用於檢查交換機(exChange),當 ack=false,交換機可能錯誤
46  @Override 47     public void confirm(CorrelationData correlationData, boolean ack, String cause) { 48         //在發送消息的時候correlationData傳入的爲進件編號
49         if (ack) { 50             log.info("消息發送成功:correlationData = " + correlationData); 51         } else { 52             //若是有多個交換機,這裏日誌須要優化
53             log.error("消息發送失敗,交換機可能錯誤:correlationData = " + correlationData + ",exchang:" + exchang); 54  } 55  } 56 
57     //次方法用於檢查隊列(queue),當此方法執行時,隊列可能錯誤
58  @Override 59     public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { 60         //若是有多個隊列,這裏日誌須要優化
61         log.error("消息發送失敗,隊列可能錯誤:correlationData = " + message.getMessageProperties().getCorrelationId() + ",queue:" + queue); 62  } 63 
64     // 聲明隊列
65  @Bean 66     public Queue queue() { 67         return new Queue(queue, true);//表示持久化
68  } 69 
70     // 聲明交換機,注意交換機的類別
71  @Bean 72     public FanoutExchange exchange() { 73         return new FanoutExchange(exchang); 74         //return new DirectExchange(exchang); 75         //return new TopicExchange(exchang);
76  } 77 
78     // 綁定交換機和隊列,若是是fanout,就不須要key
79  @Bean 80     public Binding binding() { 81         return BindingBuilder.bind(queue()).to(exchange()); 82         //return BindingBuilder.bind(queue()).to(exchange()).with(key);
83  } 84 }

在實際開發過程當中,mq的相關屬性都配置在application.yml的配置文件中。app

在綁定交換機的過程當中,須要注意綁定方式以及key。ide

3.調用函數

 1 @Autowired  2     private RabbitTemplate rabbitTemplate;  3   6     @RequestMapping(value = "testRabbitMQ", method = RequestMethod.POST)  7     public String testRabbitMQ() {  8         String msg = "{\"id\":\"123\",\"msg\":\"555555\"}";  9         String id = "123456789";
11         CorrelationData correlationId = new CorrelationData(id); 12         log.info("開始發送消息 : correlationId= " + correlationId + ",exchange=" + exchange + ",msg= " + msg); 13         Object response = rabbitTemplate.convertSendAndReceive(exchange, "", msg, correlationId); 14        
15         log.info("開始發送結束 : correlationId= " + correlationId); 16         return "testRabbitMQ"; 17     }

 因爲本文中交換機的綁定方式爲fanout,因此不須要key,這裏在發送消息的時候rabbitTemplate.convertSendAndReceive(exchange, "", msg, correlationId);key直接傳入一個空字符串便可。spring-boot

 

使用swagger測試,發送消息成功:測試

 

 

 

 下面測試一個交換機錯誤的狀況,來講明回調函數的做用於意義優化

相關文章
相關標籤/搜索