High concurrency connection to redis in jedispool
When the java end uses the jedispool to connect to redis, it often gets stuck in high concurrency, or reports connection exceptions, JedisConnectionException, or getResource exceptions.
Pay attention to two points when using jedispool
1. When obtaining jedisPool and jedis, add thread synchronization to ensure that too many jedispool and jedis are not created.
2. After the Jedis instance is used up, it must be returned to the JedisPool.
I sorted out the redis tool class, through a large number of tests and high concurrency tests
Package com. caspar. util; import org. apache. log4j. logger; import redis. clients. jedis. jedis; import redis. clients. jedis. jedisPool; import redis. clients. jedis. jedisPoolConfig;/*** Redis tool class * @ author caspar * http://blog.csdn.net/tuposky */public class RedisUtil {protected static Logger logger = Logger. getLogger (RedisUtil. class); // Redis Server IP private static String ADDR_ARRAY = FileUtil. getPropertyValue ("/ Properties/redis. properties "," server "); // Redis PORT number private static int PORT = FileUtil. getPropertyValueInt ("/properties/redis. properties "," port "); // access password // private static String AUTH = FileUtil. getPropertyValue ("/properties/redis. properties "," auth "); // The maximum number of available connected instances. The default value is 8. // if the value is-1, no limit is imposed; if maxActive jedis instances have been allocated to the pool, the pool status is exhausted (depletion ). Private static int MAX_ACTIVE = FileUtil. getPropertyValueInt ("/properties/redis. properties "," max_active "); // controls the maximum number of idle jedis instances in a pool. The default value is 8. Private static int MAX_IDLE = FileUtil. getPropertyValueInt ("/properties/redis. properties "," max_idle "); // maximum waiting time for available connections, in milliseconds. The default value is-1, indicating that the connection will never time out. If the wait time is exceeded, JedisConnectionException is directly thrown; private static int MAX_WAIT = FileUtil. getPropertyValueInt ("/properties/redis. properties "," max_wait "); // TIMEOUT value: private static int TIMEOUT = FileUtil. getPropertyValueInt ("/properties/redis. properties "," timeout "); // whether to perform the validate operation in advance when a jedis instance is in borrow; if it is true, the obtained jedis instance is available; private static boolean TEST_ON_BORROW = FileUtil. getPropertyValueBoolean ("/properti Es/redis. properties "," test_on_borrow "); private static JedisPool jedisPool = null;/*** redis expiration time, in seconds */public final static int EXRP_HOUR = 60*60; // one hour public final static int EXRP_DAY = 60*60*24; // One day public final static int EXRP_MONTH = 60*60*24*30; // one month/*** initialize Redis connection pool */private static void initialPool () {try {JedisPoolConfig config = new JedisPoolConfig (); config. setMaxTotal (MAX_ACTIV E); config. setMaxIdle (MAX_IDLE); config. setMaxWaitMillis (MAX_WAIT); config. setTestOnBorrow (TEST_ON_BORROW); jedisPool = new JedisPool (config, ADDR_ARRAY.split (",") [0], PORT, TIMEOUT);} catch (Exception e) {logger. error ("First create JedisPool error:" + e); try {// if the First IP address is abnormal, access the second IP address JedisPoolConfig = new JedisPoolConfig (); config. setMaxTotal (MAX_ACTIVE); config. setMaxIdle (MAX_IDLE); config. SetMaxWaitMillis (MAX_WAIT); config. setTestOnBorrow (TEST_ON_BORROW); jedisPool = new JedisPool (config, ADDR_ARRAY.split (",") [1], PORT, TIMEOUT);} catch (Exception e2) {logger. error ("Second create JedisPool error:" + e2) ;}}/*** synchronously initialize in a multithreaded environment */private static synchronized void poolInit () {if (jedisPool = null) {initialPool () ;}/ *** synchronously obtain the Jedis instance * @ return Jedis */public synchronized static Jedis getJedis () {if (jedisPool = null) {poolInit ();} Jedis jedis = null; try {if (jedisPool! = Null) {jedis = jedisPool. getResource () ;}} catch (Exception e) {logger. error ("Get jedis error:" + e);} finally {returnResource (jedis);} return jedis ;} /*** release jedis resource * @ param jedis */public static void returnResource (final Jedis jedis) {if (jedis! = Null & jedisPool! = Null) {jedisPool. returnResource (jedis) ;}}/*** set String * @ param key * @ param value */public static void setString (String key, String value) {try {value = StringUtil. isEmpty (value )? "": Value; getJedis (). set (key, value);} catch (Exception e) {logger. error ("Set key error:" + e );}} /*** set the expiration time * @ param key * @ param seconds in seconds * @ param value */public static void setString (String key, int seconds, String value) {try {value = StringUtil. isEmpty (value )? "": Value; getJedis (). setex (key, seconds, value);} catch (Exception e) {logger. error ("Set keyex error:" + e) ;}/ *** get the String value * @ param key * @ return value */public static String getString (String key) {if (getJedis () = null |! GetJedis (). exists (key) {return null;} return getJedis (). get (key );}}