Spring RedisTemplate操作-发布订阅操作

Posted aoeiuv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring RedisTemplate操作-发布订阅操作相关的知识,希望对你有一定的参考价值。

@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操作-发布订阅操作的主要内容,如果未能解决你的问题,请参考以下文章

Spring RedisTemplate操作-哈希操作

Spring RedisTemplate操作-String操作

Spring RedisTemplate操作-ZSet操作

Spring RedisTemplate操作-HyperLogLog操作

Spring RedisTemplate操作-Set操作

spring 的redis操作类RedisTemplate