一、相关依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
1 |
二、redis 监听器配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.data.redis.connection.RedisConnectionFactory; 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 RedisConfig{ /** * Redis消息监听器容器 * @param connectionFactory * @return */ @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); //订阅了一个叫pmp和channel 的通道,多通道 container.addMessageListener(listenerAdapter( new RedisPmpSub()), new PatternTopic( "pmp" )); container.addMessageListener(listenerAdapter( new RedisChannelSub()), new PatternTopic( "channel" )); //这个container 可以添加多个 messageListener return container; } /** * 配置消息接收处理类 * @param redisMsg 自定义消息接收类 * @return */ @Bean() @Scope( "prototype" ) MessageListenerAdapter listenerAdapter(RedisMsg redisMsg) { //这个地方 是给messageListenerAdapter 传入一个消息接受的处理器,利用反射的方法调用“receiveMessage” //也有好几个重载方法,这边默认调用处理器的方法 叫handleMessage 可以自己到源码里面看 return new MessageListenerAdapter(redisMsg, "receiveMessage" ); //注意2个通道调用的方法都要为receiveMessage } } |
三、消息处理器
import org.springframework.stereotype.Component; @Component public interface RedisMsg { public void receiveMessage(String message); } public class RedisPmpSub implements RedisMsg{ /** * 接收消息的方法 * @param message 订阅消息 */ public void receiveMessage(String message){ //注意通道调用的方法名要和RedisConfig2的listenerAdapter的MessageListenerAdapter参数2相同 System.out.println(message); } }
四、消息发布者
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; //定时器 @EnableScheduling @Component public class TestSenderController { @Autowired private StringRedisTemplate stringRedisTemplate; //向redis消息队列index通道发布消息 @Scheduled(fixedRate = 2000) public void sendMessage(){ stringRedisTemplate.convertAndSend("pmp",String.valueOf(Math.random())); stringRedisTemplate.convertAndSend("channel",String.valueOf(Math.random())); } }
备注: 使用策略模式优化消息处理器,模板化建立通道
你好世界!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!