Java uses Redis connection pooling
Jar Package for Jedis-2.9.0.jar+commons-pool2-2.4.2.jar
1 Packagecom.test;2 3 ImportRedis.clients.jedis.Jedis;4 ImportRedis.clients.jedis.JedisPool;5 ImportRedis.clients.jedis.JedisPoolConfig;6 7 Public classRedisutil {8 //Redis Server IP9 Private StaticString ADDR = "192.168.0.41";Ten One //the port number of the Redis A Private Static intPORT = 6379; - - //Access Password the Private StaticString AUTH = "admin"; - - //the maximum number of available connection instances, with a default value of 8; - //If the assignment is-1, it is unrestricted, and if the pool is already assigned a maxactive Jedis instance, the state of the pool is exhausted (exhausted) at this time. + Private Static intMax_total = 8; - + //minimum number of idle connections, default 0 A Private Static intMin_idle=0; at - //controls the maximum number of Jedis instances in a pool that have an idle (idle) state, and the default value is 8. - //Maximum number of idle connections, default 8 - Private Static intMax_idle = 8; - - //The maximum number of wait milliseconds to get a connection (if blockwhenexhausted is set to blocked), if time-out throws an exception, less than 0: blocking indeterminate time, default-1 in //The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means that never times out. If the waiting time is exceeded, the jedisconnectionexception is thrown directly; - Private Static intmax_wait =-1; to + Private Static intTIMEOUT = 10000; - the //If the connection is exhausted, false to report an exception, ture block until timeout, default true * Private Static Booleanblock_when_exhausted =false; $ Panax Notoginseng //Set the Eviction policy class name, default Defaultevictionpolicy (when the connection exceeds the maximum idle time, or the number of connections exceeds the maximum number of idle connections) - Private StaticString eviction_policy_classname= "Org.apache.commons.pool2.impl.DefaultEvictionPolicy"; the + //whether to enable the JMX management feature of pool, by default true A Private Static BooleanJmx_enabled=true; the + //MBean ObjectName = new ObjectName ("org.apache.commons.pool2:type=genericobjectpool,name=" + "pool" + i), the default is "pool", JMX is not familiar, specifically do not know what to do ... The default is good. - Private StaticString jmx_name_prefix= "Pool"; $ $ //whether to enable last-in-first-out, default true - Private Static Booleanlifo=true; - the //minimum idle time for eviction connections default 1800000 milliseconds (30 minutes) - Private Static Longmin_evictable_idle_time_millis=1800000l;Wuyi the //How long the object is idle and evicted when idle time > The value and the idle connection > maximum idle number are evicted directly, no longer judged by Minevictableidletimemillis (default eviction policy) - Private Static Longsoft_min_evictable_idle_time_millis=1800000l; Wu - //maximum number of evictions per eviction check if negative numbers are: 1/abs (n), default 3 About Private Static intNum_tests_per_evicyion_run=3; $ - //whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available; - //check validity when getting a connection, default false - Private Static BooleanTest_on_borrow =false; A + //check validity on idle, default false the Private Static Booleantest_whileidle=false; - $ //Eviction Scan interval (milliseconds) if negative, the eviction thread is not run, default-1 the Private Static LongTime_berween_eviction_runs_millis=-1; the the Private StaticJedispool Jedispool =NULL; the - /** in * Initializing Redis connection pool the */ the Static { About Try { theJedispoolconfig config =Newjedispoolconfig (); the config.setblockwhenexhausted (block_when_exhausted); the Config.setevictionpolicyclassname (eviction_policy_classname); + config.setjmxenabled (jmx_enabled); - Config.setjmxnameprefix (jmx_name_prefix); the Config.setlifo (LIFO);Bayi Config.setmaxidle (max_idle); the config.setmaxtotal (max_total); the Config.setmaxwaitmillis (max_wait); - Config.setminevictableidletimemillis (min_evictable_idle_time_millis); - Config.setminidle (min_idle); the Config.setnumtestsperevictionrun (num_tests_per_evicyion_run); the Config.setsoftminevictableidletimemillis (soft_min_evictable_idle_time_millis); the Config.settestonborrow (test_on_borrow); the Config.settestwhileidle (test_whileidle); - Config.settimebetweenevictionrunsmillis (time_berween_eviction_runs_millis); the theJedispool =Newjedispool (config, ADDR, PORT, TIMEOUT, AUTH); the}Catch(Exception e) {94 e.printstacktrace (); the } the } the 98 /** About * Get Jedis instances - * @return101 */102 Public synchronized StaticJedis Getjedis () {103 Try {104 if(Jedispool! =NULL) { theJedis resource =Jedispool.getresource ();106 returnresource;107}Else {108 return NULL;109 } the}Catch(Exception e) {111 e.printstacktrace (); the return NULL;113 } the } the the /**117 * Release Jedis resources118 * @paramJedis119 */ - Public Static voidCloseFinalJedis Jedis) {121 if(Jedis! =NULL) {122 jedis.close ();123 }124 } the}
Redis Connection Pool Jedis-2.9.0.jar+commons-pool2-2.4.2.jar