Features for publishing subscriptions with Reids

Source: Internet
Author: User

Redis is now longer used as a cache server, NoSQL database, and it also has the ability to publish subscriptions that implement messages. This article will implement a demo that uses Redis to implement a publish subscription feature


First, use an entity object

Package Com.wtf.demo.redis;import Java.io.serializable;public class UserBean implements Serializable {private String us   Ername;   Private String address;   Public String GetUserName () {return username;   } public void Setusername (String username) {this.username = username;   } public String getaddress () {return address;   The public void setaddress (String address) {this.address = address; }}

We will publish this object when this object, and can change the properties of this object, publish the code as follows

package com.wtf.demo.redis;import redis.clients.jedis.jedis;import  Java.io.bytearrayoutputstream;import java.io.ioexception;import java.io.objectoutputstream;public  class testpub {   public static void main (String[] args)  throws IOException {      // TODO Auto-generated  Method stub      jedis jedis = new jedis ("127.0.0.1", 6379);       userbean ub = new userbean ();       ub.setusername ("Zhang San"),       ub.setaddress ("Shanghai City");       bytearrayoutputstream os = new bytearrayoutputstream ();        objectoutputstream op = new objectoutputstream (OS);       op.writeobject (UB); &NBsp;     string msg1 = os.tostring ("iso-8859-1");       jedis.publish ("UB",  MSG1);    }}


After the publication is completed, we create a subscription class, which releases the UB object, and in the subscription class we need to jedispubsub the object and implement the relevant methods inside.

package com.wtf.demo.redis;import redis.clients.jedis.jedis;import  Redis.clients.jedis.jedispubsub;import java.io.bytearrayinputstream;import java.io.objectinputstream ;p ublic class testsub {        public static  Void main (String[] args)  {          jedis  jedis = new jedis ("uat.www.nanapanda.cn", 6379);           jedispubsub jedispubsub = new jedispubsub ()  {              @Override              public void onunsubscribe (string channel, int  Subscribedchannels)  {            }              @Override             public void  Onsubscribe (string channel, int subscribedchannels)  {             }            @ override            public void  Onpunsubscribe (string pattern, int subscribedchannels)  {             }              @Override             public void  Onpsubscribe (string pattern, int subscribedchannels)  {             }            @ override             public void onpmessage (String pattern,  string channel,                 string message)  {            }              @Override              public void onmessage (String channel,  String message)  {               try {                 bytearrayinputstream bis = new bytearrayinputstream (                     message.getbytes (" Iso-8859-1 "));//Specify the character set here to encode the string into a byte array,The character set needs to be consistent with the character set at publish time                  objectinputstream ois = new objectinputstream (bis);                 UserBean bean =  (UserBean)  ois.readobject ();                 system.out.println (Bean.getusername ());                 system.out.println (Bean.getaddress ());               } catch  (exception e)  {                 e.printstacktrace ();               } finally {               }             }          };           jedis.subscribe (jedispubsub,  "UB");         }}


So after running the Testpub class, then run the TestSub class, you will have the following output

--------------------

Tom
Shanghai City

Change the properties of the Userbean in Testpub, change the address to Beijing, and the subscription window will have the following values

------------------

Tom

Beijing

Features for publishing subscriptions with Reids

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.