- Publishing and subscribing are a message communication pattern.
- Pros: Reduce the coupling between the message subscriber and the message Publisher, similar to the Observer pattern in design mode.
- Publications and subscriptions for Redis
Publish and subscribe
The commands for subscribing are as follows:
// subscribe to one or more channels Subscribe channel1 channel2 channel3 ... // mode subscription, channel parameters similar to regular expressions psubscribe abc* xyz* ...
The Publish command is as follows:
Publish Channel MSG
Start a Subscriber client X
subscription Cctv-1 returns three parameters: Subscribe subscription succeeded, The subscribed channel is Cctv-1, the number of channels currently subscribed is 1;
Subscription Cctv-2 returns three parameters: Subscribe subscription is successful, the subscribed channel is Cctv-2 and the number of channels currently subscribed is 2.
Start a Subscriber client Y
Subscription cctv-* returns three parameters: Psubcribe subscription succeeded, subscribed channel cctv-*, number of channels subscribed by current client 1.
Start a publisher client, Publish channel Cctv-1 message
Subscriber Client X receives subscription information, returns three parameters: Received message success, channel, received message
Subscriber Client Y receives subscription information, returns four parameters: Received message success, mode subscription channel, received message channel, received message
Cancel Subscription
Unsubscribe cctv-1punsubscribe CCTV-*
The unsubscribe is not emulated on the official client.
View Subscriptions
// View all channels subscribed to pubsub channels;pubsub channels msg*; // View the number of subscribers to this channel pubsub numsub channel1 channel2 ...;
- Programming to showcase Redis publications and subscriptions
1 Public classMySubextendsjedispubsub{2 3 @Override4 Public voidonMessage (String channel, String msg) {5System.out.println ("onMessage-" + Channel + "-" +msg);6 }7 8 @Override9 Public voidonpmessage (string pattern, string channel, String msg) {TenSystem.out.println ("onpmessage-" + Pattern + "-" + Channel + "-" +msg); One } A - @Override - Public voidOnpsubscribe (String Channel,intmsg) { theSystem.out.println ("Onpsubscribe-" + Channel + "-" +msg); - } - - @Override + Public voidOnpunsubscribe (String arg0,intarg1) {} - + @Override A Public voidOnsubscribe (String Channel,intNumber ) { atSystem.out.println ("Onsubscribe-" + Channel + "-" +Number ); - - } - - @Override - Public voidOnunsubscribe (String arg0,intarg1) {} in}
Subscriber Client A
1 public class subclient { 2 public static void main (string[] args) { 3 mysub sub = new mysub (); 4 Jedis Jedis = new Jedis ("127.0.0.1", 6 379 5 jedis.subscribe (Sub, "Cctv-1" 6 7 }
subscriber client B (modal subscription)
1 public class Psubclient { public static void main (string[] args) { 3 mysub sub = new mysub (); 4 Jedis Jedis = new Jedis ("127.0.0.1", 6 379 5 jedis.psubscribe (Sub, "cctv*" 6 7 }
Publisher Client C
1 Public class pubclient {2 Public Static void Main (string[] args) {3 New Jedis ("127.0.0.1", 6379); 4 Jedis.publish ("Cctv-1", "Hello,this is Cctv-1"); 5 }6 }
Run client ABC in turn
A-Terminal output
Onsubscribe-cctv-1-1onmessage-cctv-1-Hello,this is Cctv-1
B-Terminal output
onpsubscribe-cctv*-1onpmessage-cctv*-Cctv-1-Hello,this is Cctv-1
Learn more and remember more.
Publish and subscribe