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