"Redis" Jedis client implementation Redis message publishing subscriptions (real-time messaging middleware)

Source: Internet
Author: User
Tags redis stub thread
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;
		(Exception e) {log.error (IO fail while reading input, e);
 }
	}
}


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

Subscribe to

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); Processing public void OnMessage (string s, string s1) {//TODO auto-generated method stub log.debug after getting the subscription message rece
	ived,channel:{},msg:{} ", S,S1);
		//Get processing public void Onpmessage (string s, string s1, string s2) {//TODO auto-generated method stub after messages are subscribed in an expression way
		Log.debug ("pattern:{}", s);
	Log.debug ("pattern Message received,channel:{},msg:{}", S1,S2); }//Initialize the processing of the subscription by expression public void Onpsubscribe (String s, int i) {//TODO auto-generated Method Stub log.debug ("Pat
	Tern subscribe,pattern:{},channelnum:{} ", s,i); //Cancel the processing of a subscription by expression public void Onpunsubscribe (String s, int i) {//TODO auto-generated Method Stub log.debug ("Pa
	Ttern unsubscribe,pattern:{},channelnum:{} ", s,i); The processing public at the time the subscription is initializedvoid Onsubscribe (String s, int i) {//TODO auto-generated Method Stub Log.debug ("subscribe,channel:{},channelnum:{}",
	S,i); //Unsubscribe processing 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 of the subscription class, for the various operations of the subscription to achieve specific processing methods.

start 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 the 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.*"); Channel with regular matching subscriptions

Because the subscription class blocks execution of the current thread, a second 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.