Spring-based multi-redis data source Configuration

Source: Internet
Author: User
Tags redis redis server

Spring Bean

    <bean id= "Jedisconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "testwhileidle" va Lue= "true"/> <!--<property name= "maxactive" value= "${redis.pool.maxactive}"/>--&LT;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"/&gt
                ;
<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)); }
    }
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.