This example includes
Jedis_demo: Entrance class
Jedis_control:jedis controller (Connection pool for Jedis)
Jedis_pub_sub_listener: Subscribed Listeners
Singleton_agent: proxy class for Singleton (Connection pool configuration)
PackageCom.larry.jedis; ImportRedis.clients.jedis.Jedis; /*** Entrance Class*/ Public classJedis_demo {Jedis_control redis_util=Jedis_control.get_singleton (); Public Static voidMain (string[] args) {Jedis_demo Jedis_demo=NewJedis_demo (); NewThread (NewRunnable () {@Override Public voidrun () {Jedis_control redis_util=Jedis_control.get_singleton (); Jedis Jedis=redis_util.get_connection (); Jedis_pub_sub_listener Pub_sub_listener=NewJedis_pub_sub_listener (); //can subscribe to multiple channels//Subscribe to get information on Lister's OnMessage (...) method is processed in the//Jedis.subscribe (Listener, "News.share", "News.log"); //Jedis.subscribe (Listener, New string[]{"News.share", "News.log"}); Jedis.psubscribe (Pub_sub_listener,NewString[] {"News.share"});//set up a channel using pattern matching}}). Start (); Jedis_demo.publish (); } /*** Release*/ Public voidPublish () {Jedis Jedis=redis_util.get_connection (); Jedis.publish ("News.share", "OK"); Jedis.publish ("News.share", "Hello word"); } } PackageCom.larry.jedis; ImportRedis.clients.jedis.Jedis; /*** Jedis Controller *@authorLu Guijiang * @email [email protected] *@versioncreation Time: 2012-3-28 pm 12:03:40*/ Public Final classJedis_control {//Single Case Private StaticJedis_control _jedis_control; Public StaticJedis_control Get_singleton () {if(_jedis_control = =NULL) {_jedis_control=NewJedis_control (); } return_jedis_control; } /*** Get Connection instance *@returnJedis*/ PublicJedis get_connection () {Jedis Jedis=NULL; Try{Jedis=Singleton_agent.get_jedispool (). GetResource (); } Catch(Exception e) {e.printstacktrace (); } returnJedis; } /*** Release database connection *@paramConn*/ Public voidclose_connection (Jedis Jedis) {if(NULL!=Jedis) { Try{Singleton_agent.get_jedispool (). Returnresource (Jedis); } Catch(Exception e) {e.printstacktrace (); } } } }
PackageCom.larry.jedis; ImportRedis.clients.jedis.JedisPubSub; /*** Listen for subscription events **/ Public classJedis_pub_sub_listenerextendsJedispubsub {//processing after getting a message for a subscription Public voidonMessage (String channel, String message) {SYSTEM.OUT.PRINTLN (channel+ "=" +message); } //handling when initializing a subscription Public voidOnsubscribe (String Channel,intsubscribedchannels) {SYSTEM.OUT.PRINTLN (channel+ "=" +subscribedchannels); } //handling when canceling a subscription Public voidOnunsubscribe (String Channel,intsubscribedchannels) {SYSTEM.OUT.PRINTLN (channel+ "=" +subscribedchannels); } //initialize the processing of the subscription by the way of an expression Public voidOnpsubscribe (String pattern,intsubscribedchannels) {System.out.println (Pattern+ "=" +subscribedchannels); } //canceling the process of subscribing by expression Public voidOnpunsubscribe (String pattern,intsubscribedchannels) {System.out.println (Pattern+ "=" +subscribedchannels); } //processing of a message that has been subscribed by expression Public voidonpmessage (string pattern, string channel, String message) {System.out.println (Pattern+ "=" + Channel + "=" +message); }} [Java] view plain copy PackageCom.larry.jedis; ImportRedis.clients.jedis.JedisPool; ImportRedis.clients.jedis.JedisPoolConfig; /*** All single-instance proxy classes*/ Public classsingleton_agent {//**************** single Case one connection pool *************** Private StaticJedispool Jedispool =NULL; /*** Get Connection Pool *@returnData Source*/ Public StaticJedispool Get_jedispool () {if(Jedispool = =NULL) {jedispoolconfig jedispool_config=NewJedispoolconfig (); Jedispool_config.maxactive= 20; Jedispool_config.maxidle= 0; Jedispool_config.maxwait= 1000; Jedispool_config.testonborrow=true; Jedispool=NewJedispool (Jedispool_config, "localhost", 6379); } returnJedispool; } //end**************** single Case one connection pool ***************}
Redis for the "Go" messaging system