zoukankan      html  css  js  c++  java
  • 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 反向序列化慢
    
        }
        
    
    }
  • 相关阅读:
    easyui 单元格超出鼠标放上弹出全部
    EasyUI datagrid单元格文本超出显示省略号,鼠标移动到单元格显示文本
    easyui 回车搜索
    js控制easyui文本框例子及控制html例子
    Android错误---NoClassDefFoundError: org.ksoap2.transport.HttpTransportSE
    [Android错误]The literal 100000000000000 of type int is out of range
    Eclipse错误:Conversion to Dalvik format failed with error 1
    Android微信分享音乐加网络图片失败分析
    android.view.WindowManager$BadTokenException异常
    Android错误--Attempted to add application window with unknown token
  • 原文地址:https://www.cnblogs.com/aoeiuv/p/6760798.html
Copyright © 2011-2022 走看看