package com.loan.msg.config; import com.loan.msg.service.MessageReceiver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; /** * @author 周志伟 * @projectname 项目名称: ${project_name} * @classname: RedisMQConfig * @description: * @date 2018/11/13:10:12 */ @Configuration public class RedisMQConfig { /** * 注入消息监听容器 * * @param connectionFactory 连接工厂 * @param listenerAdapter 监听处理器1 * @param listenerAdapter 监听处理器2 (参数名称需和监听处理器的方法名称一致,因为@Bean注解默认注入的id就是方法名称) * @return */ @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter, MessageListenerAdapter listenerAdapter2) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); //订阅一个叫mq_01 的信道 container.addMessageListener(listenerAdapter, new PatternTopic("mq_01")); //订阅一个叫mq_02 的信道 container.addMessageListener(listenerAdapter2, new PatternTopic("mq_02")); //这个container 可以添加多个 messageListener return container; } /** * 消息监听处理器1 * * @param receiver 处理器类 * @return */ @Bean MessageListenerAdapter listenerAdapter(MessageReceiver receiver) { //给messageListenerAdapter 传入一个消息接收的处理器,利用反射的方法调用“receiveMessage” return new MessageListenerAdapter(receiver, "receiveMessage"); //receiveMessage:接收消息的方法名称 } /** * 消息监听处理器2 * * @param receiver 处理器类 * @return */ @Bean MessageListenerAdapter listenerAdapter2(MessageReceiver receiver) { //给messageListenerAdapter 传入一个消息接收的处理器,利用反射的方法调用“receiveMessage2” return new MessageListenerAdapter(receiver, "receiveMessage2"); //receiveMessage:接收消息的方法名称 } }
package com.loan.msg.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; /** * @author 周志伟 * @projectname 项目名称: ${project_name} * @classname: SendMessageService * @description: * @date 2018/11/13:10:11 */ @Service public class SendMessageService { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 发布消息 * * @param channel 消息信道 * @param message 消息内容 */ public void sendMessage(String channel, String message) { stringRedisTemplate.convertAndSend(channel, message); } /** * 发布消息的方法 */ public void setStr01() { this.sendMessage("mq_01", "发送信息内容01"); this.sendMessage("mq_01", "发送信息内容011"); this.sendMessage("mq_02", "发送信息内容02"); } }
package com.loan.msg.service; import org.springframework.stereotype.Component; /** * @author 周志伟 * @projectname 项目名称: ${project_name} * @classname: MessageReceiver * @description: * @date 2018/11/13:10:14 */ @Component public class MessageReceiver { /** * 接收消息的方法1 **/ public void receiveMessage(String message) { System.out.println("receiveMessage接收到的消息:" + message); } /** * 接收消息的方法2 **/ public void receiveMessage2(String message) { System.out.println("receiveMessage2接收到的消息:" + message); } }
//向redis里存入数据和设置缓存时间 stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS); //val做-1操作 stringRedisTemplate.boundValueOps("test").increment(-1); //根据key获取缓存中的val stringRedisTemplate.opsForValue().get("test") //val +1 stringRedisTemplate.boundValueOps("test").increment(1); //根据key获取过期时间 stringRedisTemplate.getExpire("test") //根据key获取过期时间并换算成指定单位 stringRedisTemplate.getExpire("test",TimeUnit.SECONDS) //根据key删除缓存 stringRedisTemplate.delete("test"); //检查key是否存在,返回boolean值 stringRedisTemplate.hasKey("546545"); //向指定key中存放set集合 stringRedisTemplate.opsForSet().add("red_123", "1","2","3"); //设置过期时间 stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS); //根据key查看集合中是否存在指定数据 stringRedisTemplate.opsForSet().isMember("red_123", "1") //根据key获取set集合 stringRedisTemplate.opsForSet().members("red_123")