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

Source: Internet
Author: User
Tags redis stub
Publish

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);

	Private 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 {bufferedreader br = new BufferedReader (new InputStreamReader (system.in));
				while (true) {String line = Br.readline (); if (! " Quit ". Equals (Line)} {for (int i = 0; i < Channellen; i++) {if (Channel[i].matches (" ^chiwei.* ")) {L
							Og.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.

Subscribe

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 (REDISSUBSCRI

	Ber.class); Gets the message after the subscription is processed public void onMessage (string s, string s1) {//TODO auto-generated Method Stub log.debug ("Message rece
	ived,channel:{},msg:{} ", S,S1);
		}//Gets the message that is subscribed by expression after processing public void Onpmessage (string s, string s1, string s2) {//TODO auto-generated method stub
		Log.debug ("pattern:{}", s);
	Log.debug ("Pattern Message received,channel:{},msg:{}", S1,S2); }//Initialize the process of subscribing by expression to public void Onpsubscribe (String s, int i) {//TODO auto-generated Method Stub log.debug ("Pat
	Tern subscribe,pattern:{},channelnum:{} ", s,i); }//Cancel the way the subscription is processed by expression public void Onpunsubscribe (String s, int i) {//TODO auto-generated Method Stub log.debug ("Pa
	Ttern unsubscribe,pattern:{},channelnum:{} ", s,i); }//Handling public when initializing a subscriptionvoid Onsubscribe (String s, int i) {//TODO auto-generated Method Stub Log.debug ("subscribe,channel:{},channelnum:{}",
	S,i); }//unsubscribe when handling public void Onunsubscribe (String s, int i) {//TODO auto-generated Method Stub log.debug ("Unsubscrib
	e,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 "};

	Private static final Logger log = Loggerfactory.getlogger (Redispubsubmain.class);
		public static void Main (string[] args) {//TODO auto-generated Method Stub log.debug ("=========================");
		Jedispoolconfig config = new Jedispoolconfig ();
		Config = new Jedispoolconfig ();
		Config.setmaxtotal (100);
		Config.setmaxidle (10);
		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, channel_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.red ISPUBLISHER[34] -9381 [main] DEBUG  -Match ...
2015-04-17 10:35:53,138-com.chiwei.redis.redispublisher[39] -9387 [main] DEBUG  -Publish to Chiwei.momo
2 015-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
2 015-04-17 10:35:53,153-com.chiwei.redis.redispublisher[39] -9402 [main] DEBUG  -Publish to Taotao






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.