Spring Bean
<bean id= "Jedisconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "testwhileidle" va Lue= "true"/> <!--<property name= "maxactive" value= "${redis.pool.maxactive}"/>--<PR Operty name= "Maxtotal" value= "${redis.pool.maxtotal}"/> <property name= "Maxidle" value= "${redis.pool.maxIdl e} "/> <!--<property name=" maxwait "value=" ${redis.pool.maxwait} "/>--<property name = "Maxwaitmillis" value= "${redis.pool.maxwaitmillis}"/> <property name= "Testonborrow" value= "${redis.pool.te
Stonborrow} "/> <property name=" Testonreturn "value=" ${redis.pool.testonreturn} "/> </bean> <bean id= "jedispool_xx" class= "Redis.clients.jedis.JedisPool" destroy-method= "destroy" > <constru Ctor-arg ref= "Jedisconfig"/> <constructor-arg value= "${xx.redis.ip}"/> <constructor-arg type = "int" value= "${xx.redis. Port} "/> <constructor-arg value=" 100000 "type=" int "/> <constructor-arg value=" ${XX.REDIS.PA SSWD} "/> </bean> <bean id=" Jedispool_yy "class=" Redis.clients.jedis.JedisPool "Destroy-metho D= "Destroy" > <constructor-arg ref= "jedisconfig"/> <constructor-arg value= "${yy.redis.ip}"/&G
T
<constructor-arg type= "int" value= "${yy.redis.port}"/> <constructor-arg value= "100000" type= "int"/> <constructor-arg value= "${yy.redis.passwd}"/> </bean> <!--Add the Jedispool configured above Muyiljedispoo
Lmap, remember the map key, the code needs to operate through this key different Redis server-<bean id= "Mutiljedispoolmap" class= "Java.util.HashMap" > <constructor-arg> <map> <entry key= "Xxjedispool" value-ref= "Jedispool_xx"/>
;
<entry key= "Yyjedispool" value-ref= "Jedispool_yy"/> </map> </constructor-arg> </bean>
Use
@Resource (name = "Mutiljedispoolmap") public map<string, jedispool> Mutiljedispoolmap; /** * Method Description: Get Jedis object from Connection pool */public Jedis getresource (String redispoolkey) {logger.debug ("[Mutiljedis
Util:getresource]: Get Jedis Resource from Pool ... "); Jedis Jedis = null;//Declaration Jedis object//int cycletimes = 0;//The number of times the exception has been recycled try{Jedis = Mutiljedispoolm Ap.get (Redispoolkey). GetResource ();//Gets the Jedis object from the pool if (Jedis = = null) {Logger.error ("[Mutilje
Disutil:getresource]:get Jedis Object failed.message:\n ");
return null; }}catch (Jedisconnectionexception ex) {Logger.error ("[Mutiljedisutil:getresource]:get Jedis Obje
CT failed.message:\n "+exceptionutil.exceptiontostring (ex)"; }//Gets the object if it is not empty returns if (Jedis! = null) {Logger.debug ("[Mutiljedisutil:getresource]: Get Jedis Res Ource from Pool success. ");
else{ Logger.error ("[Mutiljedisutil:getresource]:get Jedis object Failed.if redis server is runing,please check the CONFIGR
ation and Network connection. ");
return Jedis;
}/** * Method Description: Return Jedis object to connection pool after use */public void Returnresource (String Redispoolkey,jedis Jedis) { try{if (Jedis! = null) this.mutilJedisPoolMap.get (Redispoolkey). Returnresourceobject (Jedis);
/Return the object to the pool Logger.debug ("[Mutiljedisutil:returnresource]: return Jedis Resource to pool ..."); }catch (Jedisconnectionexception ex) {//return failed, force destroy the link//this.jedispool.returnresourceobject (Jedis);
/returnbrokenresource (Jedis);
Logger.error ("[Mutiljedisutil:returnresource]:" + exceptionutil.exceptiontostring (ex)); }
}