springboot2.0整合redis的发布和订阅
2024-09-24 17:19:48
1.Maven引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.redis属性配置
spring.redis.database=
spring.redis.host=127.0.0.1
spring.redis.port=
spring.redis.password=******
server.port=
3.设置监听相关对象
3.1接听对象
RedisReceiver可以是普通类或者继承MessageListener,普通类的写法如下,接收的时候只接收到消息,没有频道名
package com.example.redistest.config; import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component; @Component
public class RedisReceiver { public void receiveMessage(String message) {
// TODO 这里是收到通道的消息之后执行的方法
System.out.println(message);
}
}
继承MessageListener,就能拿到消息体和频道名。
package com.example.redistest.config; import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component; @Component
public class RedisReceiver implements MessageListener { @Override
public void onMessage(Message message, byte[] pattern) {
System.out.println(new String(message.getBody()));
System.out.println(new String(message.getChannel()));
}
}
3.2 配置监听适配器、消息监听容器
container.addMessageListener(listenerAdapter, new PatternTopic("channel:test"));
消息监听容器增加监听的消息,第一个参数是监听适配器,第2个参数是监听的频道。
package com.example.redistest.config; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; @Configuration
@EnableCaching
public class RedisCacheConfig { @Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// 可以添加多个 messageListener,配置不同的交换机
container.addMessageListener(listenerAdapter, new PatternTopic("channel:test"));
return container;
} @Bean
MessageListenerAdapter listenerAdapter(RedisReceiver receiver) {
System.out.println("消息适配器1");
return new MessageListenerAdapter(receiver, "onMessage");
} @Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
} }
3.3 消息发送
package com.example.redistest.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date; @RequestMapping("/redis")
@Controller
public class RedisController { @Autowired
StringRedisTemplate template; /**
* 发布消息
*
* @param id
* @return
*/
@RequestMapping("/sendMessage/{id}")
public String sendMessage(@PathVariable String id) {
for(int i = 1; i <= 5; i++) {
template.convertAndSend("channel:test", String.format("我是消息{%d}号: %tT", i, new Date()));
}
return "";
} }
测试
postman访问http://localhost:5555/redis/sendMessage/1
接收消息后打印
最新文章
- WSDL项目----操作和请求
- JQuery(选择器、事件、DOM操作)
- JMeter学习(十七)JMeter测试Java
- ssh相关操作
- [Unity2D]脚本基类MonoBehaviour介绍
- socket异步编程--libevent的使用
- C语言之内存覆盖
- android4.4内核移植
- LeetCode_ 4 sum
- Linux下Bash运行脚本
- phpcms 去掉默认自动获取关键词、自动提取第一张图片?
- Java 学习 第三篇;面向对象
- xml drawable
- hibernate 多对多 最佳实践
- Date类型常用概念及方法总结(1)
- python——爬虫
- 爬虫框架Scrapy 之(四) --- scrapy运行原理(管道)
- SQL其他常用的语句
- esp32驱动SSD1306的oled显示汉字(micropython)
- Java高级面试题解析(一)
热门文章
- fengmiantu3
- python的filter,reduce,map
- Gradel 多渠道打包 代码混淆
- RAM: Residual Attention Module for Single Image Super-Resolution
- Delphi XE2 之 FireMonkey 入门(14) - 滤镜: 概览
- 测开之路九十九:js函数、事件、window窗体对象
- postman连接mysql执行操作
- c++调用c#代码
- Python解决ModuleNotFoundError: No module named &#39;Queue&#39;的问题
- EasyUI选项卡避免重复打开