@Component ("Sub") Public classSubImplementsmessagelistener{@AutowiredPrivateStringredisserializer Stringredisserializer; /*(non-Javadoc) * Description: * @see org.springframework.data.redis.connection.messagelistener#onmessage (ORG.SPR Ingframework.data.redis.connection.Message, byte[])*/@Override Public voidOnMessage (Message message,byte[] pattern) { byte[] BODY = Message.getbody ();//Please use ValueSerializer byte[] Channel =Message.getchannel (); String msg=(String) stringredisserializer.deserialize (body); String Topic=(String) stringredisserializer.deserialize (channel); System.out.println ("I am a sub, listening to" +topic+ ", I received the message:" +msg); }}
@Component ("Sub2") Public classSub2Implementsmessagelistener{@AutowiredPrivateStringredisserializer Stringredisserializer; @AutowiredPrivateJdkserializationredisserializer Jdkserializationredisserializer; /*(non-Javadoc) * Description: * @see org.springframework.data.redis.connection.messagelistener#onmessage (ORG.SPR Ingframework.data.redis.connection.Message, byte[])*/@Override Public voidOnMessage (Message message,byte[] pattern) { byte[] BODY = Message.getbody ();//Please use ValueSerializer byte[] Channel =Message.getchannel (); String msg=(String) stringredisserializer.deserialize (body); String Topic=(String) stringredisserializer.deserialize (channel); System.out.println ("I am sub2, listening to" +topic+ ", I received the message:" +msg); }}
@Component ("Sub3") Public classSub3Implementsmessagelistener{@AutowiredPrivateGenericjackson2jsonredisserializer Jackson2jsonredisserializer; @AutowiredPrivateStringredisserializer Stringredisserializer; @AutowiredPrivateJdkserializationredisserializer Jdkserializationredisserializer; /*(non-Javadoc) * Description: * @see org.springframework.data.redis.connection.messagelistener#onmessage (ORG.SPR Ingframework.data.redis.connection.Message, byte[])*/@Override Public voidOnMessage (Message message,byte[] pattern) { byte[] BODY = Message.getbody ();//Please use ValueSerializer byte[] Channel =Message.getchannel (); User u= Jackson2jsonredisserializer.deserialize (Body,user.class); String Topic=(String) stringredisserializer.deserialize (channel); System.out.println ("I am sub3, listening to" +topic+ ", I received the message:" +u.getid () + "--" +u.getname ()); }}
@Service Public classPub {@Autowired @Resource (name= "Redistemplate") PrivateRedistemplate<string, string>RT; @AutowiredPrivateGenericjackson2jsonredisserializer Jackson2jsonredisserializer; @AutowiredPrivateJdkserializationredisserializer Jdkserializationredisserializer; Public voidsendMessage (String channel, String message) {Rt.convertandsend (channel, message); } Public voidSendMessage (String Channel, user user) {byte[] msg =jackson2jsonredisserializer.serialize (user); Rt.convertandsend (channel,NewString (msg)); //Rt.convertandsend (channel, user); } }
@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= "Classpath:conf/spring/spring*.xml") Public classpubsubtest {@AutowiredPrivatePub Pub; @Test Public voidTest ()throwsinterruptedexception{User u=NewUser (); U.setid ("1"); U.setname ("Wzg"); Pub.sendmessage ("Dddchannel", "I sent a message."); Pub.sendmessage ("Cccchannel", u); Thread.Sleep (100);//Jackson reverse serialization slow } }
Spring redistemplate Action-Publish subscription action (8)