首页 > 代码库 > Spring RedisTemplate操作-发布订阅操作(8)
Spring RedisTemplate操作-发布订阅操作(8)
@Component("sub") public class Sub implements MessageListener{ @Autowired private StringRedisSerializer stringRedisSerializer; /* (非 Javadoc) * Description: * @see org.springframework.data.redis.connection.MessageListener#onMessage(org.springframework.data.redis.connection.Message, byte[]) */ @Override public void onMessage(Message message, byte[] pattern) { byte[] body = message.getBody();//请使用valueSerializer byte[] channel = message.getChannel(); String msg = (String)stringRedisSerializer.deserialize(body); String topic = (String)stringRedisSerializer.deserialize(channel); System.out.println("我是sub,监听"+topic+",我收到消息:"+msg); } }
@Component("sub2") public class Sub2 implements MessageListener{ @Autowired private StringRedisSerializer stringRedisSerializer; @Autowired private JdkSerializationRedisSerializer jdkSerializationRedisSerializer; /* (非 Javadoc) * Description: * @see org.springframework.data.redis.connection.MessageListener#onMessage(org.springframework.data.redis.connection.Message, byte[]) */ @Override public void onMessage(Message message, byte[] pattern) { byte[] body = message.getBody();//请使用valueSerializer byte[] channel = message.getChannel(); String msg = (String)stringRedisSerializer.deserialize(body); String topic = (String)stringRedisSerializer.deserialize(channel); System.out.println("我是sub2,监听"+topic+",我收到消息:"+msg); } }
@Component("sub3") public class Sub3 implements MessageListener{ @Autowired private GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer; @Autowired private StringRedisSerializer stringRedisSerializer; @Autowired private JdkSerializationRedisSerializer jdkSerializationRedisSerializer; /* (非 Javadoc) * Description: * @see org.springframework.data.redis.connection.MessageListener#onMessage(org.springframework.data.redis.connection.Message, byte[]) */ @Override public void onMessage(Message message, byte[] pattern) { byte[] body = message.getBody();//请使用valueSerializer byte[] channel = message.getChannel(); User u = jackson2JsonRedisSerializer.deserialize(body,User.class); String topic = (String)stringRedisSerializer.deserialize(channel); System.out.println("我是sub3,监听"+topic+",我收到消息:"+u.getId()+"--"+u.getName()); } }
@Service public class Pub { @Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; @Autowired private GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer; @Autowired private JdkSerializationRedisSerializer jdkSerializationRedisSerializer; public void sendMessage(String channel, String message) { rt.convertAndSend(channel, message); } public void sendMessage(String channel, User user) { byte[] msg =jackson2JsonRedisSerializer.serialize(user); rt.convertAndSend(channel, new String(msg)); // rt.convertAndSend(channel, user); } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:conf/spring/spring*.xml") public class PubSubTest { @Autowired private Pub pub; @Test public void test() throws InterruptedException{ User u = new User(); u.setId("1"); u.setName("wzg"); pub.sendMessage("dddchannel", "我发消息了"); pub.sendMessage("cccchannel", u); Thread.sleep(100);//jackson 反向序列化慢 } }
Spring RedisTemplate操作-发布订阅操作(8)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。