SpringBoot利用redis集成消息隊(duì)列的方法
一、pom文件依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
二、創(chuàng)建消息接收者
變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動(dòng)裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。
@Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } public void receiveMessage(String message) { LOGGER.info("收到的消息: <" + message + ">"); latch.countDown(); } }
以上基本條件達(dá)成后,以下是實(shí)現(xiàn)的三要素:
一個(gè)連接工廠
一個(gè)消息監(jiān)聽容器
Redis template
三、在application.java注入消息接收者
@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)聽容器
//必要的redis消息隊(duì)列連接工廠 @Bean Receiver receiver(CountDownLatch latch) { return new Receiver(latch); } //必要的redis消息隊(duì)列連接工廠 @Bean CountDownLatch latch() { return new CountDownLatch(1); } //redis模板 @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); } //注入消息監(jiān)聽器容器 @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic("msg")); return container; } //注入消息監(jiān)聽器容器 @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); }
五、單元測試
import java.util.concurrent.CountDownLatch; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import com.Application; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class MsgQueueTest { @Autowired protected ApplicationContext ctx; private static final Logger logger = LoggerFactory.getLogger(MsgQueueTest.class); @Test public void SendMsg() { StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class); CountDownLatch latch = ctx.getBean(CountDownLatch.class); logger.info("我要發(fā)送消息咯..."); template.convertAndSend("msg", "歡迎使用redis的消息隊(duì)列!"); try { //發(fā)送消息連接等待中 logger.info("消息正在發(fā)送..."); latch.await(); } catch (InterruptedException e) { logger.info("消息發(fā)送失敗..."); } } }
總結(jié)
以上所述是小編給大家介紹的SpringBoot利用redis集成消息隊(duì)列的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Springboot3+Redis實(shí)現(xiàn)消息隊(duì)列的多種方法小結(jié)
- SpringBoot集成Redisson實(shí)現(xiàn)消息隊(duì)列的示例代碼
- SpringBoot使用Redis Stream實(shí)現(xiàn)輕量消息隊(duì)列的示例代碼
- SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)
- springboot整合redis之消息隊(duì)列
- SpringBoot集成Redis實(shí)現(xiàn)消息隊(duì)列的方法
- SpringBoot集成Redis消息隊(duì)列的實(shí)現(xiàn)示例
相關(guān)文章
java?常規(guī)輪詢長輪詢Long?polling實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了java?常規(guī)輪詢長輪詢Long?polling實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12ElasticSearch學(xué)習(xí)之文檔API相關(guān)操作
這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之文檔API相關(guān)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01設(shè)計(jì)模式之中介者模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了設(shè)計(jì)模式之中介者模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Java String類簡單用法實(shí)戰(zhàn)示例【字符串輸出、比較】
這篇文章主要介紹了Java String類簡單用法,結(jié)合具體實(shí)例形式分析了Java使用String類實(shí)現(xiàn)字符串的輸出和比較功能相關(guān)操作技巧,需要的朋友可以參考下2019-07-07Java多態(tài)中的向上轉(zhuǎn)型與向下轉(zhuǎn)型淺析
多態(tài)是指不同類的對象在調(diào)用同一個(gè)方法是所呈現(xiàn)出的多種不同行為,下面這篇文章主要給大家介紹了關(guān)于Java多態(tài)中向上轉(zhuǎn)型與向下轉(zhuǎn)型的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02java如何使用正則表達(dá)式限制特殊字符的個(gè)數(shù)
這篇文章主要介紹了java如何使用正則表達(dá)式限制特殊字符的個(gè)數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11