SpringBoot非官方教程 | 第十四篇:在springboot中用redis實(shí)現(xiàn)消息隊(duì)列

轉(zhuǎn)載請(qǐng)標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/71023652
本文出自方志朋的博客

這篇文章主要講述如何在springboot中用reids實(shí)現(xiàn)消息隊(duì)列。

準(zhǔn)備階段

環(huán)境依賴

創(chuàng)建一個(gè)新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依賴:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

創(chuàng)建一個(gè)消息接收者

REcevier類,它是一個(gè)普通的類,需要注入到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ā)送一條消息和接受一條消息,需要三樣?xùn)|西:

  • 一個(gè)連接工廠
  • 一個(gè)消息監(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");
    }


測(cè)試

在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ā)送一條消息,接收者接收到后,打印出來。啟動(dòng)springboot程序,控制臺(tái)打印:

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 <Hello from Redis!>

測(cè)試通過,接收者確實(shí)接收到了發(fā)送者的消息。

源碼下載:

https://github.com/forezp/SpringBootLearning

參考資料

messaging-redis

優(yōu)秀文章推薦:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,933評(píng)論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,958評(píng)論 6 342
  • 轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/forezp/article/details/71023...
    方志朋閱讀 555評(píng)論 1 2
  • 秦嶺山中最天然的美食----- 純正蜂蜜,已于夏至進(jìn)入采收旺季。 以下文字來源于網(wǎng)絡(luò)僅用于說明蜂蜜來由,食用及藥用...
    富硒特產(chǎn)閱讀 447評(píng)論 0 0
  • 又是個(gè)熱熱的不眠夜 體表的溫度要超過上海氣溫 相擁纏綿在一起 耳鬢廝磨,呼吸急促 不眠夜是朦朧的 借著窗外的微光 ...
    誓約之黑星閱讀 810評(píng)論 0 0