當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot高级-消息-@RabbitListener@EnableRabbit
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot高级-消息-@RabbitListener@EnableRabbit
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在實際開發中我們需要監聽場景,比如我們之前舉的例子,兩個系統,訂單系統,和我們庫存系統,他們交互都是通過交互通過消息隊列,某一個人下了單以后,將訂單信息放到消息隊列中,庫存系統要實時監聽隊列里的內容,一旦有新的訂單進來,庫存系統就要負責相關的操作,那我們的監聽怎么寫呢,Spring為了簡化我么開發,給我們引入了相關的注解,比如我們來舉一個例子,我來寫一個BookService,就來監聽book里面的內容,我怎么寫呢,我就叫receive,在這方法里面呢,我們要收到book的內容,所以我在方法的參數上,我們這個方法是通過監聽消息隊列,可以來寫一個注解,@RabbitListener,監聽MQ的,那監聽哪個消息隊列呢,我們可以來寫一個queues,這個queues它是一個數組的方式,/*** The queues for this listener.* The entries can be 'queue name', 'property-placeholder keys' or 'expressions'.* Expression must be resolved to the queue name or {@code Queue} object.* Mutually exclusive with {@link #bindings()}* @return the queue names or expressions (SpEL) to listen to from target* {@link org.springframework.amqp.rabbit.listener.MessageListenerContainer}.*/
String[] queues() default {};我們可以監聽多個消息隊列,我們就來監聽我們之前的china.news,我們就來監聽,有內容進來,@EnableRabbit,開啟基于注解的MQ
package com.learn.service;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;import com.learn.bean.Book;@Service
public class BookService {@RabbitListener(queues="china.news")public void receive(Book book) {System.out.println("收到消息" + book);}}
package com.learn;import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 自動配置* @author Leon.Sun**/
@SpringBootApplication
@EnableRabbit
public class SpringBoot02AmqpApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(SpringBoot02AmqpApplication.class,args);}}
有了這兩個注解的配合,讓@RabbitListener來監聽Listener里的內容,監聽消息隊列的內容,我們就來啟動一下這個應用,來測試一下,只要有book信息來,我們就會收到,一啟動就收到消息收到消息Book [bookName=三國演義, author=羅貫中]/*** 廣播*/
@Test
public void sendMsg() {rabbitTemplate.convertAndSend("exchange.fanout","",new Book("紅樓夢","曹雪芹"));
}收到消息Book [bookName=紅樓夢, author=曹雪芹]這就是我們收到消息監聽來收消息,只要消息隊列里有,這個我們直接序列化成Book對象,如果我們有一些定制的消息,還想要消息頭等等,寫成Message類型org.springframework.amqp.core.Message寫上Message類型以后呢,我們來寫一個@RabbitListener,我們來監聽china隊列,我們寫的Message參數,既有getBody消息的內容,還有getMessageProperties,我們消息的頭信息,我們可以重新嘗試一下,我們來到這個主程序,他重新啟動,來看china里的內容,china默認已經有兩個內容了,我們啟動的時候都能收到,[B@4511a7ef
MessageProperties [headers={__TypeId__=com.learn.bean.Book}, timestamp=null, messageId=null,
userId=null,
receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null,
correlationIdString=null,
replyTo=null, contentType=application/json, contentEncoding=UTF-8, contentLength=0,
deliveryMode=null,
receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false,
receivedExchange=exchange.fanout, receivedRoutingKey=, receivedDelay=null, deliveryTag=1,
messageCount=0,
consumerTag=amq.ctag-jmwvhNiVfS6HYqKWi5xuCw, consumerQueue=china]
[B@71e6aae7
MessageProperties [headers={__TypeId__=com.learn.bean.Book}, timestamp=null, messageId=null,
userId=null,
receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null,
correlationIdString=null,
replyTo=null, contentType=application/json, contentEncoding=UTF-8, contentLength=0,
deliveryMode=null,
receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false,
receivedExchange=exchange.fanout, receivedRoutingKey=, receivedDelay=null,
deliveryTag=2,messageCount=0, consumerTag=amq.ctag-jmwvhNiVfS6HYqKWi5xuCw, consumerQueue=china]
package com.learn.service;import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;import com.learn.bean.Book;@Service
public class BookService {@RabbitListener(queues="china.news")public void receive(Book book) {System.out.println("收到消息" + book);}@RabbitListener(queues="china")public void receive02(Message message) {System.out.println(message.getBody());System.out.println(message.getMessageProperties());}
}
package com.learn;import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 自動配置* @author Leon.Sun**/
@SpringBootApplication
@EnableRabbit
public class SpringBoot02AmqpApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(SpringBoot02AmqpApplication.class,args);}}
package com.learn.springboot;import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import com.learn.bean.Book;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootAmqpApplicationTests {@AutowiredRabbitTemplate rabbitTemplate;/*** 1.單播(點對點)*/@Testpublic void contextLoads() {// Message需要自己構造一個,定義消息體內容和消息頭
// rabbitTemplate.send(exchange, routingKey, message);// object默認當成消息體,只需要傳入要發送的對象,自動序列化發送給rabbitmq
// rabbitTemplate.convertAndSend(routingKey, message, messagePostProcessor);Map<String,Object> map = new HashMap<String,Object>();map.put("msg", "這是第一個消息");map.put("data", Arrays.asList("helloworld",1231,true)); // 對象被默認序列化以后發送出去rabbitTemplate.convertAndSend("exchange.direct", "china.news",new Book("西游記","吳承恩"));}@Testpublic void receive() {Object o = rabbitTemplate.receiveAndConvert("china.news");System.out.println(o.getClass());System.out.println(o);}/*** 廣播*/@Testpublic void sendMsg() {rabbitTemplate.convertAndSend("exchange.fanout","",new Book("紅樓夢","曹雪芹"));}
}
?
總結
以上是生活随笔為你收集整理的SpringBoot高级-消息-@RabbitListener@EnableRabbit的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot高级-消息-Rabb
- 下一篇: SpringBoot高级-消息-Amqp