Redis pubsub Command Usage

Source: Internet
Author: User
Tags redis server

I. What is pub/sub and implementation
The Pub/sub feature (means Publish, Subscribe) is the Publish and subscribe feature.

Redis implements subscription and publishing functionality through the Publish and subscribe commands.

Subscribers subscribe to Redis Server by subscribe the type of message they are interested in. Redis calls the information type a channel.
When a publisher sends a specific type of information to Redis server through the Publish command, all Subscribers to the message type receive this message.

Client 1 Subscription CCTV1:

127.0.0.1:6379> Subscribe cctv1reading messages ... (Press Ctrl-C to quit)1) "Subscribe" 2) "CCTV1" 3) (integer) 1

Client 2 subscriptions CCTV1 and CCTV2:

127.0.0.1:6379> Subscribe CCTV1 cctv2reading Messages ... (Press Ctrl-C to quit)1) "Subscribe" 2) "CCTV1" 3) (integer) one) "Subscribe" 2) "CCTV2" 3) (integer) 2

At this point the two clients listen to the specified channel respectively. Now another client pushes the two channel information to the server.

127.0.0.1:6379> Publish CCTV1 "CCTV1 is good"// returns 2 indicates that two clients have received the message. 

The client receiving the message is shown below.
Client 1:

1) "Message" 2) "CCTV1" 3) "CCTV1 is good"

Client 2:

1) "Message" 2) "CCTV1" 3) "CCTV1 is good"

As on the subscription/release also called the subscription published to the channel (using the Publish and subscribe commands), there are also subscriptions published to the mode (using Psubscribe to subscribe to a schema)

Subscribe to all CCTV channels

127.0.0.1:6379> psubscribe cctv*Reading messages ... (Press Ctrl-C to quit)1) "Psubscribe" 2) "cctv*" 3) (integer) 1

When a CCTV1 message is still first pushed, the client receives it normally.


Second, the implementation of Pub/sub in Java
To import Redis drivers:

' redis.clients:jedis:2.4.2 '}

The Redis driver package provides an abstract class: Jedispubsub ... Inheriting this class completes the monitoring of the client's subscription. Example code:

 Packagecom.ljq.durian.test;ImportOrg.apache.log4j.Logger;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPubSub;/*** Client Subscription Listener class * *@authorJqlin **/ Public classPubsubserviceextendsJedispubsub {Private Static FinalLogger Logger = Logger.getlogger (pubsubservice.class); /*** Listen to the subscription channel received the message*/@Override Public voidonMessage (String channel, String message) {Logger.info (String.Format ("onsubscribe:channel[%s", "+" message[%s] ", channel, message)); }    /*** Listen to the subscription mode to receive messages*/@Override Public voidonpmessage (string pattern, string channel, String message) {Logger.info (String.Format ("onpmessage:pattern[%s], channel[%s], message[%s]", pattern, channel, message)); }    /*** Callback when subscribing to a channel **/@Override Public voidOnsubscribe (String Channel,intsubscribedchannels) {Logger.info (String.Format ("onsubscribe:channel[%s", "+" subscribedchannels[%s] ", channel, Subscribedchannels)); }    /*** Callback when canceling subscription to channel*/@Override Public voidOnunsubscribe (String Channel,intsubscribedchannels) {Logger.info (String.Format ("onunsubscribe:channel[%s", "+" subscribedchannels[%s] ", channel, Subscribedchannels)); }    /*** Callback when canceling subscription mode*/@Override Public voidOnpunsubscribe (String pattern,intsubscribedchannels) {Logger.info (String.Format ("onpunsubscribe:pattern[%s", "+" subscribedchannels[%s] ", pattern, subscribedchannels)); }    /*** callback when subscribing to channel mode*/@Override Public voidOnpsubscribe (String pattern,intsubscribedchannels) {Logger.info (String.Format ("onpsubscribe:pattern[%s", "+" subscribedchannels[%s] ", pattern, subscribedchannels)); }         Public Static voidMain (string[] args) {Jedis Jedis=NULL; Try{Jedis=NewJedis ("127.0.0.1", 6379, 0);//Redis Service address and port numberPubsubservice PubSub =NewPubsubservice (); Jedis.subscribe (PubSub,"News.share", "News.blog"); } Catch(Exception e) {e.printstacktrace (); } finally {            if(Jedis! =NULL) {jedis.disconnect (); }        }    }}


From the code we can see that one of the Redis links we've declared will perform some actions after setting up the listener, such as posting messages, subscribing to messages, etc...

When the above code is run, it is output in the console:

Onsubscribe:channel[news.share],subscribedchannels[1]onsubscribe:channel[news.blog],subscribedchannels[ 2]//Onsubscribe method runs successfully

At this point, the OnMessage method is appropriate when a client publish a message to the New.share or New.blog channel. (Jedis.publish (channel, message)).

Redis pubsub Command Usage

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.