摘要:環(huán)境依賴創(chuàng)建一個新的工程,在其文件加入依賴創(chuàng)建一個消息接收者類,它是一個普通的類,需要注入到中。
這篇文章主要講述如何在springboot中用reids實現(xiàn)消息隊列。
準備階段安裝redis,可參考我的另一篇文章,5分鐘帶你入門Redis。 java 1.8 maven 3.0 idea環(huán)境依賴
創(chuàng)建一個新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依賴:
創(chuàng)建一個消息接收者org.springframework.boot spring-boot-starter-data-redis
REcevier類,它是一個普通的類,需要注入到springboot中。
public class Receiver { private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class); private CountDownLatch latch; @Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } public void receiveMessage(String message) { LOGGER.info("Received <" + message + ">"); latch.countDown(); } }注入消息接收者
@Bean Receiver receiver(CountDownLatch latch) { return new Receiver(latch); } @Bean CountDownLatch latch() { return new CountDownLatch(1); } @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); }注入消息監(jiān)聽容器
在spring data redis中,利用redis發(fā)送一條消息和接受一條消息,需要三樣東西:
一個連接工廠 一個消息監(jiān)聽容器 Redis template
上述1、3步已經(jīng)完成,所以只需注入消息監(jiān)聽容器即可:
@Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic("chat")); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); }測試
在springboot入口的main方法:
public static void main(String[] args) throws Exception{ ApplicationContext ctx = SpringApplication.run(SpringbootRedisApplication.class, args); StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class); CountDownLatch latch = ctx.getBean(CountDownLatch.class); LOGGER.info("Sending message..."); template.convertAndSend("chat", "Hello from Redis!"); latch.await(); System.exit(0); }
先用redisTemplate發(fā)送一條消息,接收者接收到后,打印出來。啟動springboot程序,控制臺打?。?/p>
2017-04-20 17:25:15.536 INFO 39148 — [ main] com.forezp.SpringbootRedisApplication : Sending message… 2017-04-20 17:25:15.544 INFO 39148 — [ container-2] com.forezp.message.Receiver : 》Received
源碼下載:
https://github.com/forezp/Spr...
參考資料messaging-redis
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://systransis.cn/yun/67814.html
摘要:是一個開源的應用容器引擎,基于語言并遵從協(xié)議開源。準備工作環(huán)境環(huán)境或不要用對一無所知的看教程。創(chuàng)建一個工程引入的起步依賴,創(chuàng)建一個將工程容器化有一個簡單的文件作為指定鏡像的圖層。說明的工程已部署。停止鏡像刪除鏡像參考資料源碼下載 這篇文篇介紹,怎么為 springboot程序構建一個docker鏡像。docker 是一個開源的應用容器引擎,基于 Go 語言 并遵從Apache2.0協(xié)議...
摘要:創(chuàng)建消息監(jiān)聽,并發(fā)送一條消息在程序中,提供了發(fā)送消息和接收消息的所有方法。 這篇文章帶你了解怎么整合RabbitMQ服務器,并且通過它怎么去發(fā)送和接收消息。我將構建一個springboot工程,通過RabbitTemplate去通過MessageListenerAdapter去訂閱一個POJO類型的消息。 準備工作 15min IDEA maven 3.0 在開始構建項目之前,機器需...
摘要:本文介紹如何在中使用默認的聲明式緩存定義和接口用來統(tǒng)一不同的緩存技術。在使用集成的時候,我們需要注冊實現(xiàn)的的。默認使用在我們不使用其他第三方緩存依賴的時候,自動采用作為緩存管理器。源碼下載參考資料揭秘與實戰(zhàn)二數(shù)據(jù)緩存篇快速入門 本文介紹如何在springboot中使用默認的spring cache 聲明式緩存 Spring 定義 CacheManager 和 Cache 接口用來統(tǒng)一不...
摘要:用于控制活動人數(shù),將超過此一定閥值的訂單直接丟棄。緩解短時間的高流量壓垮應用。目前比較推薦的就是我們手動然后將消費錯誤的消息轉移到其它的消息隊列中,做補償處理消費者該方案是默認的方式不太推薦。 SpringBoot 是為了簡化 Spring 應用的創(chuàng)建、運行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動裝配的特性讓我們可以更好的關注業(yè)務本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相...
閱讀 977·2021-10-13 09:48
閱讀 4012·2021-09-22 10:53
閱讀 3179·2021-08-30 09:41
閱讀 1988·2019-08-30 15:55
閱讀 2966·2019-08-30 15:55
閱讀 1899·2019-08-30 14:11
閱讀 2244·2019-08-29 13:44
閱讀 810·2019-08-26 12:23