public static ZKPool getZkPool(String zkAddr) throws PlatformException {
    if (pools.get(zkAddr) == null) {
      synchronized (LOCK) {
        if (pools.get(zkAddr) == null) {
          GenericObjectPoolConfig config = new GenericObjectPoolConfig();
          config.setMaxTotal(total);
          config.setTestOnBorrow(false);
          config.setMinIdle(10);
          int timeOut = 2000;
          if (StringUtils.isBlank(zkAddr)) {
            throw new PlatformException(
                ZkErrorCodeConstants.NO_PARSE_ZK_INFO,
                "zkAddr can not be null,and its format is ip:port");
          }

          if (!StringUtils.isBlank(zkTimeout)) {
            timeOut = Integer.parseInt(zkTimeout);
          }

          ZKPool zkPool = new ZKPool(config, zkAddr, timeOut);
          pools.put(zkAddr, zkPool);
        }
      }
    }

    return pools.get(zkAddr);
  }
Example #2
0
 @Test(expected = JedisConnectionException.class)
 public void testIfPoolConfigAppliesToClusterPools() {
   GenericObjectPoolConfig config = new GenericObjectPoolConfig();
   config.setMaxTotal(0);
   config.setMaxWaitMillis(2000);
   Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
   jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
   JedisCluster jc = new JedisCluster(jedisClusterNode, config);
   jc.set("52", "poolTestValue");
 }
  @Test
  public void testGetResourceValidate() {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setTestOnBorrow(true);
    pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
    pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
    pool.afterPropertiesSet();
    RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
    assertNotNull(client);
    client.close();
  }
  @Test
  public void testReturnResource() {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(1);
    poolConfig.setMaxWaitMillis(1);
    pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
    pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
    pool.afterPropertiesSet();
    RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
    assertNotNull(client);
    pool.returnResource(client);
    assertNotNull(pool.getResource());
    client.close();
  }
  @Test
  public void testGetResourcePoolExhausted() {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(1);
    poolConfig.setMaxWaitMillis(1);
    pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
    pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
    pool.afterPropertiesSet();
    RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
    assertNotNull(client);
    try {
      pool.getResource();
      fail("PoolException should be thrown when pool exhausted");
    } catch (PoolException e) {
    } finally {
      client.close();
    }
  }
 {
   super.setMaxIdle(6);
   super.setMaxTotal(40);
   super.setMinIdle(1);
   super.setTimeBetweenEvictionRunsMillis(1000L * 60L * 10L);
   super.setNumTestsPerEvictionRun(10);
   super.setMinEvictableIdleTimeMillis(1000L * 60L * 5L);
 }
  @Test
  public void testReturnBrokenResource() {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(1);
    poolConfig.setMaxWaitMillis(1);
    pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
    pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
    pool.afterPropertiesSet();
    RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
    assertNotNull(client);
    pool.returnBrokenResource(client);
    RedisAsyncConnection<byte[], byte[]> client2 = pool.getResource();
    assertNotSame(client, client2);
    try {
      client.ping();
      fail("Broken resouce connection should be closed");
    } catch (RedisException e) {
    } finally {
      client.close();
      client2.close();
    }
  }
  static {
    // int timeOut = PGHelper.toInteger(Config.getParam("mysql", "timeout"));
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMinIdle(1);
    config.setMaxIdle(4);
    config.setTestOnBorrow(true);
    config.setTestOnCreate(true);
    config.setTestOnReturn(true);
    // pool = new GenericObjectPool<Connection>(SQLConnectionFactory.getInstance(), config);

    dataSource = SQLConnectionFactory.getInstance().createDataSoure();
  }
Example #9
0
  public synchronized void init() {
    if (status != 0) {
      throw new RuntimeException("client has inited");
    } else {
      status = 1;
    }

    // 配置基本属性
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setTestOnBorrow(true);
    poolConfig.setJmxEnabled(true);
    poolConfig.setMaxTotal(maxTotal);
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setTimeBetweenEvictionRunsMillis(1000 * 60 * 5);
    poolConfig.setNumTestsPerEvictionRun(10);
    AbandonedConfig abandonedConfig = new AbandonedConfig();

    ConnectionPoolFactory factory = new ConnectionPoolFactory(servers, soTimeout, ioMode);
    pool = new GenericObjectPool<Connection>(factory, poolConfig, abandonedConfig);
  }