日期:2014-05-16 浏览次数:20534 次
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:22) at com.derbysoft.jredis.longkeytest.BorrowObject.run(BorrowObject.java:22) at java.lang.Thread.run(Thread.java:662) Caused by: java.util.NoSuchElementException: Timeout waiting for idle object at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1134) at redis.clients.util.Pool.getResource(Pool.java:20) ... 2 more
<bean id="dataJedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> [color=red]<property name="maxActive" value="5000"/>[/color] <property name="maxIdle" value="5000"/> <property name="maxWait" value="10000"/> <property name="testOnBorrow" value="true"/> </bean>
public class BorrowObject implements Runnable { private ShardedJedisPool jedisPool; public BorrowObject(ShardedJedisPool jedisPool) { this.jedisPool = jedisPool; } @Override public void run() { ShardedJedis shardedJedis = null; try { shardedJedis = jedisPool.getResource(); String value = shardedJedis.hget("LONG_KEY_TEST:AA059E03E0AB7D806E6C351F87404B06C1190", "Roc El Pinar Aparthotel"); System.out.println(value); } catch (Exception e) { //logger.error(e); e.printStackTrace(); } finally { jedisPool.returnResource(shardedJedis); } } }
public class BorrowObjectTest { private ShardedJedisPool jedisPool = null; public BorrowObjectTest() { List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>(); JedisShardInfo jedisShardInfo = new JedisShardInfo("192.168.1.112"); jedisShardInfo.setTimeout(1000000); jedisShardInfos.add(jedisShardInfo); jedisPool = new ShardedJedisPool(createJedisConfig(), jedisShardInfos); } private JedisPoolConfig createJedisConfig() { JedisPoolConfig jedisConfig = new JedisPoolConfig(); [color=red]jedisConfig.setMaxActive(2);[/color] jedisConfig.setMaxIdle(2); jedisConfig.setMaxWait(5); jedisConfig.setTestOnBorrow(true); return jedisConfig; } public static void main(String[] args) { BorrowObjectTest borrowObjectTest = new BorrowObjectTest(); for (int i = 0; i < 300; i++) { new Thread(new BorrowObject(borrowObjectTest.jedisPool)).start(); } } }