溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在springboot中用redis實現(xiàn)消息隊列

發(fā)布時間:2021-09-14 18:20:12 來源:億速云 閱讀:536 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“怎么在springboot中用redis實現(xiàn)消息隊列”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么在springboot中用redis實現(xiàn)消息隊列”吧!

準(zhǔn)備階段

  • 安裝redis

  • java 1.8

  • maven 3.0

  • idea

環(huán)境依賴

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

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

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

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ā)送一條消息和接受一條消息,需要三樣?xùn)|西:

  • 一個連接工廠

  • 一個消息監(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

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

到此,相信大家對“怎么在springboot中用redis實現(xiàn)消息隊列”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI