码迷,mamicode.com
首页 > 编程语言 > 详细

Spring RedisTemplate操作-发布订阅操作(8)

时间:2017-04-25 11:43:43      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:extc   send   autowire   订阅   serial   void   thread   except   div   

@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)

标签:extc   send   autowire   订阅   serial   void   thread   except   div   

原文地址:http://www.cnblogs.com/aoeiuv/p/6760798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!