"Redis" Jedis client implements the publication subscription for REDIS messages (real-time message middleware)

Source: Internet
Author: User

Release

Package Com.chiwei.redis;import Java.io.bufferedreader;import Java.io.inputstreamreader;import Org.slf4j.Logger; Import Org.slf4j.loggerfactory;import Redis.clients.jedis.jedis;public class Redispublisher {private static final Logger log = Loggerfactory.getlogger (redispublisher.class);p rivate final Jedis pubjedis;private final string[] channel; Public Redispublisher (Jedis Pubjedis, string[] channel) {This.pubjedis = Pubjedis;this.channel = Channel;} public void Start () {Log.debug ("Type your message (type quit to exit)"), int channellen = channel.length;try {Bufferedreade R br = new BufferedReader (new InputStreamReader (system.in)), while (true) {String line = Br.readline (); Quit ". Equals (Line)} {for (int i = 0; i < Channellen; i++) {if (Channel[i].matches (" ^chiwei.* ") {log.debug (" Match ... "); Pubjedis.publish (Channel[i], line + "haha");} else {pubjedis.publish (channel[i], line);} Log.debug ("Publish to {}", Channel[i]);}} else {break;}}} catch (Exception e) {log.error ("IO fail while Reading input ", e);}}} 


The above publishing class, the published channel is an array, that is, to publish the content to multiple channels, you can judge according to the content, different content published to different channels.

Subscription

Package Com.chiwei.redis;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Redis.clients.jedis.jedispubsub;public class Redissubscriber extends jedispubsub{private static final Logger log = Loggerfactory.getlogger (Redissubscriber.class);//Get a subscription message after processing public void OnMessage (string s, string s1) {//TODO Auto-generated method Stublog.debug ("Message received,channel:{},msg:{}", S,S1);} Gets the message that is subscribed to by expression after processing public void Onpmessage (string s, string s1, string s2) {//TODO auto-generated method Stublog.debug ("P attern:{} ", s); Log.debug (" Pattern Message received,channel:{},msg:{} ", S1,S2);} Initializes the process of subscribing by expression to public void Onpsubscribe (String s, int i) {//TODO auto-generated method Stublog.debug ("Pattern SUBSCR ibe,pattern:{},channelnum:{} ", s,i);} Cancels the way the subscription is handled by expression public void Onpunsubscribe (String s, int i) {//TODO auto-generated method Stublog.debug ("Pattern unsub scribe,pattern:{},channelnum:{} ", s,i);} Process of initializing a subscription when public void Onsubscribe (String s, int i) {//TODO auto-generated method StubloG.debug ("subscribe,channel:{},channelnum:{}", s,i);} When you unsubscribe from the process public void Onunsubscribe (String s, int i) {//TODO auto-generated method Stublog.debug ("Unsubscribe,channel: {},channelnum:{} ", S,i);}}


This class is the implementation class of the subscription and implements the specific processing method for each operation of the subscription.

Start the main class

Package Com.chiwei.redis;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Redis.clients.jedis.Jedis; Import Redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;public class RedisPubSubMain { public static final string[] Channel_name = new string[] {"Chiwei.momo", "Chiwei.nono", "Taotao"};p rivate static final Log Ger log = Loggerfactory.getlogger (redispubsubmain.class);p ublic static void Main (string[] args) {//TODO auto-generated m Ethod stublog.debug ("========================="); Jedispoolconfig config = new jedispoolconfig (); config = new Jedispoolconfig (); config.setmaxtotal (100); Config.setmaxidle; Config.setmaxwaitmillis (1000L); Config.settestonborrow (true); Config.settestonreturn (True) ; Jedispool Jedispool = new Jedispool (config, "192.168.11.176", 7379); final Jedis Subjedis = Jedispool.getresource (); final Redissubscriber sub = new Redissubscriber (); New Thread (New Runnable () {public void run () {try {//subjedis.subscribe (sub, C Hannel_name); subjedis.psubscrIbe (Sub, "^chiwei.*"); Log.debug ("Subscribe ended");} catch (Exception e) {log.error ("Subscribe failed", e);}}). Start (); Jedis Pubjedis = Jedispool.getresource (); New Redispublisher (Pubjedis, Channel_name). Start (); Sub.unsubscribe (); Jedispool.returnresourceobject (Subjedis); Jedispool.returnresourceobject (Pubjedis); JedisPool.close ();}}

Subjedis.psubscribe (Sub, "^chiwei.*"); subscription-based channel matching

Because the subscription class blocks the execution of the current thread, another thread in the main thread starts the subscription and then launches the publishing thread to publish the content.

2015-04-17 10:35:43,751-com.chiwei.redis.redispubsubmain[18]-0 [main] DEBUG-=========================
2015-04-17 10:35:43,843-com.chiwei.redis.redissubscriber[29] -92 [Thread-3] Debug-pattern Subscribe,pattern:^chiwei . *,channelnum:1
2015-04-17 10:35:43,848-com.chiwei.redis.redispublisher[25] -97 [main] debug-type your message (Type quit to exit)
3
2015-04-17 10:35:53,132-com.chiwei.redis.redispublisher[34] -9381 [main] debug-match ...
2015-04-17 10:35:53,138-com.chiwei.redis.redispublisher[39] -9387 [main] debug-publish to Chiwei.momo
2015-04-17 10:35:53,140-com.chiwei.redis.redispublisher[34] -9389 [main] debug-match ...
2015-04-17 10:35:53,146-com.chiwei.redis.redispublisher[39] -9395 [main] debug-publish to Chiwei.nono
2015-04-17 10:35:53,153-com.chiwei.redis.redispublisher[39] -9402 [main] debug-publish to Taotao






"Redis" Jedis client implements the publication subscription for REDIS messages (real-time message middleware)

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.