随笔 - 367  文章 - 0  评论 - 20  阅读 - 63万 

环境依赖

创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖:

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

创建一个消息接收者

REcevier类,它是一个普通的类,需要注入到springboot中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
    }
}

  

注入消息接收者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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);
    }

  

注入消息监听容器

在spring data redis中,利用redis发送一条消息和接受一条消息,需要三样东西:

  • 一个连接工厂
  • 一个消息监听容器
  • Redis template

上述1、3步已经完成,所以只需注入消息监听容器即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
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发送一条消息,接收者接收到后,打印出来。启动springboot程序,控制台打印:

1
2
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

  

posted on   巨象  阅读(2095)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示