Redis Learning Notes (8)-Publish/Subscribe

Source: Internet
Author: User

Package Cn.com;import Java.util.list;import Redis.clients.jedis.jedis;public class Redis_pubsub {public static Jedis Redis = new Jedis ("localhost", 6379);//Connection redis/** * PUBLISH Channel message * Sends message message to specified channel channel. * */public static void publish () {redis.publish ("student_2", "123"), Redis.publish ("Student_1", "ABC1");}  /** * PUBSUB <subcommand> [argument [argument ...]] * PUBSUB is an introspective command that looks at the status of a subscription and the publishing system, which consists of several subcommands of different formats, which are described in the following separate sub-commands. * List current active channels. * Active Channel refers to a channel with at least one subscriber, and the subscription mode client is not counted. * The pattern parameter is optional: * If the pattern parameter is not given, all active channels in the subscription and publishing system are listed. * If the pattern parameter is given, only those active channels matching the pattern pattern are listed. * */public static void PubSub () {Pubsublistener listener=new pubsublistener (); Redis.subscribe (Listener,new String[]{" Student_1 "," student_2 "}); }public static void Pubsubchannels () {list<string> list=redis.pubsubchannels ("student_1"); for (String s:list) { System.out.println (s); }}public static void Main (String [] args) {publish ();p ubsub ();p ubsubchannels ();}}

Package Cn.com;import Redis.clients.jedis.jedispubsub;public Class Pubsublistener extends Jedispubsub {//Get a subscription message after processing      public void OnMessage (string channel, String message) {SYSTEM.OUT.PRINTLN ("Get subscription message:" +channel + "=" + message); }//Initialize the subscription when handling public void Onsubscribe (String channel, int subscribedchannels) {//System.out.      println (channel + "=" + Subscribedchannels); }//unsubscribe when handling public void Onunsubscribe (String channel, int subscribedchannels) {//System.out.prin      TLN (channel + "=" + Subscribedchannels); }//Initialize the process of subscribing by expression to public void Onpsubscribe (String pattern, int subscribedchannels) {//SYSTEM.O      Ut.println (pattern + "=" + Subscribedchannels); }//cancels the process of subscribing by expression to public void Onpunsubscribe (String pattern, int subscribedchannels) {//System.      Out.println (pattern + "=" + Subscribedchannels); }//Gets the message that is subscribed by expression to the processing public void Onpmessage (String Pattern, string channel, String message) {System.out.println (pattern + "=" + Channel + "=" + message); }  }

  

Redis Learning Notes (8)-Publish/Subscribe

Related Article

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.