Java Code Examples for org.apache.commons.pool2.impl.GenericObjectPoolConfig#setMaxTotal()

The following examples show how to use org.apache.commons.pool2.impl.GenericObjectPoolConfig#setMaxTotal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisQParserPlugin.java    From solr-redis with Apache License 2.0 6 votes vote down vote up
@Override
public void init(final NamedList args) {
  final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
  poolConfig.setMaxTotal(getInt(args, MAX_CONNECTIONS_FIELD, DEFAULT_MAX_CONNECTIONS));

  final String host = getString(args, HOST_FIELD, HostAndPort.LOCALHOST_STR);
  final int timeout = getInt(args, TIMEOUT_FIELD, Protocol.DEFAULT_TIMEOUT);
  final String password = getString(args, PASSWORD_FIELD, null);
  final int database = getInt(args, DATABASE_FIELD, Protocol.DEFAULT_DATABASE);
  final int retries = getInt(args, RETRIES_FIELD, DEFAULT_RETRIES);

  final String[] hostAndPort = host.split(":");
  final JedisPool jedisConnectionPool = createPool(poolConfig, hostAndPort[0],
      hostAndPort.length == 2 ? Integer.parseInt(hostAndPort[1]) : Protocol.DEFAULT_PORT, timeout, password,
      database);

  connectionHandler = createCommandHandler(jedisConnectionPool, retries);

  log.info("Initialized RedisQParserPlugin with host: " + host);
}
 
Example 2
Source File: BrpcPooledChannel.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
public BrpcPooledChannel(ServiceInstance serviceInstance,
                         CommunicationOptions communicationOptions) {
    super(serviceInstance, communicationOptions);
    this.readTimeOut = communicationOptions.getReadTimeoutMillis();
    this.latencyWindowSize = communicationOptions.getLatencyWindowSizeOfFairLoadBalance();
    this.latencyWindow = new ConcurrentLinkedQueue<Integer>();
    GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
    // Maximum waiting time, when you need to borrow a connection, the maximum waiting time,
    // if the time is exceeded, throw an exception, -1 is no time limit
    conf.setMaxWaitMillis(communicationOptions.getConnectTimeoutMillis());
    conf.setMaxTotal(communicationOptions.getMaxTotalConnections());
    conf.setMaxIdle(communicationOptions.getMaxTotalConnections());
    conf.setMinIdle(communicationOptions.getMinIdleConnections());
    // Connect test when idle, start asynchronous evict thread for failure detection
    conf.setTestWhileIdle(true);
    // Maximum time for connection idle, testWhileIdle needs to be true
    conf.setTimeBetweenEvictionRunsMillis(communicationOptions.getTimeBetweenEvictionRunsMillis());
    channelFuturePool = new GenericObjectPool<Channel>(new ChannelPooledObjectFactory(
            this, serviceInstance.getIp(), serviceInstance.getPort()), conf);
    try {
        channelFuturePool.preparePool();
    } catch (Exception ex) {
        log.warn("init min idle object pool failed");
    }
}
 
Example 3
Source File: ESUtils.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
public static Client getClient(String hostIps, int port, String clusterName){
    //System.out.println( "hostIps = [" + hostIps + "], port = [" + port + "], clusterName = [" + clusterName + "]" );
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();//对象池配置类,不写也可以,采用默认配置
    //poolConfig.setMaxTotal(list.length);//采用默认配置maxTotal是8,池中有8个client
    // System.out.println(Integer.parseInt( prop.get("dkSearch.commons-pool.MaxTotal") )  );
    poolConfig.setMaxTotal( Integer.parseInt( prop.get("dkSearch.commons-pool.MaxTotal") ) );

    poolConfig.setMaxIdle(Integer.parseInt(prop.get("dkSearch.commons-pool.MaxIdlel")));
    poolConfig.setMinIdle(Integer.parseInt(prop.get("dkSearch.commons-pool.MinIdle")));
    poolConfig.setMaxWaitMillis(Integer.parseInt(prop.get("dkSearch.commons-pool.MaxWaitMillis")));
    EsClientPoolFactory esClientPoolFactory = new EsClientPoolFactory(hostIps,port,clusterName);//要池化的对象的工厂类,这个是我们要实现的类
    GenericObjectPool<TransportClient> clientPool = new GenericObjectPool<>(esClientPoolFactory,poolConfig);//利用对象工厂类和配置类生成对象池
    try {
        client = clientPool.borrowObject(); //从池中取一个对象
    } catch (Exception e) {
        e.printStackTrace();
    }
    clientPool.returnObject( (TransportClient) client );//使用完毕之后,归还对象
    return client;
}
 
Example 4
Source File: RedisConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public JedisCluster jedisCluster(){
    Set<HostAndPort> nodes = new HashSet<>();
    for(String node : this.getNodes()){
        try{
            String[] parts = StringUtils.split(node, ":");
            Assert.state(parts.length==2, "redis node shoule be defined as 'host:port', not '" + Arrays.toString(parts) + "'");
            nodes.add(new HostAndPort(parts[0], Integer.parseInt(parts[1])));
        }catch(RuntimeException e){
            throw new IllegalStateException("Invalid redis cluster nodes property '" + node + "'", e);
        }
    }
    if(nodes.isEmpty()){
        return null;
    }
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(this.getMaxTotal());
    config.setMaxIdle(this.getMaxIdle());
    config.setMinIdle(this.getMinIdle());
    return new JedisCluster(nodes, this.getConnectionTimeout(), this.getSoTimeout(), this.getMaxRedirections(), this.getPassword(), config);
}
 
Example 5
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkResourceIsCloseable() {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(1);
  config.setBlockWhenExhausted(false);
  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
      "foobared", 2);

  Jedis jedis = pool.getResource();
  try {
    jedis.set("hello", "jedis");
  } finally {
    jedis.close();
  }

  Jedis jedis2 = pool.getResource();
  try {
    assertEquals(jedis, jedis2);
  } finally {
    jedis2.close();
  }
}
 
Example 6
Source File: J2CacheSpringRedisAutoConfiguration.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
private GenericObjectPoolConfig getGenericRedisPool(Properties props, String prefix) {
    GenericObjectPoolConfig cfg = new GenericObjectPoolConfig();
    cfg.setMaxTotal(Integer.valueOf((String) props.getOrDefault(key(prefix, "maxTotal"), "-1")));
    cfg.setMaxIdle(Integer.valueOf((String) props.getOrDefault(key(prefix, "maxIdle"), "100")));
    cfg.setMaxWaitMillis(Integer.valueOf((String) props.getOrDefault(key(prefix, "maxWaitMillis"), "100")));
    cfg.setMinEvictableIdleTimeMillis(
        Integer.valueOf((String) props.getOrDefault(key(prefix, "minEvictableIdleTimeMillis"), "864000000")));
    cfg.setMinIdle(Integer.valueOf((String) props.getOrDefault(key(prefix, "minIdle"), "10")));
    cfg.setNumTestsPerEvictionRun(
        Integer.valueOf((String) props.getOrDefault(key(prefix, "numTestsPerEvictionRun"), "10")));
    cfg.setLifo(Boolean.valueOf(props.getProperty(key(prefix, "lifo"), "false")));
    cfg.setSoftMinEvictableIdleTimeMillis(
        Integer.valueOf((String) props.getOrDefault(key(prefix, "softMinEvictableIdleTimeMillis"), "10")));
    cfg.setTestOnBorrow(Boolean.valueOf(props.getProperty(key(prefix, "testOnBorrow"), "true")));
    cfg.setTestOnReturn(Boolean.valueOf(props.getProperty(key(prefix, "testOnReturn"), "false")));
    cfg.setTestWhileIdle(Boolean.valueOf(props.getProperty(key(prefix, "testWhileIdle"), "true")));
    cfg.setTimeBetweenEvictionRunsMillis(
        Integer.valueOf((String) props.getOrDefault(key(prefix, "timeBetweenEvictionRunsMillis"), "300000")));
    cfg.setBlockWhenExhausted(Boolean.valueOf(props.getProperty(key(prefix, "blockWhenExhausted"), "false")));
    return cfg;
}
 
Example 7
Source File: JedisConfig.java    From kayenta with Apache License 2.0 6 votes vote down vote up
@Bean
JedisPool jedisPool(
    @Value("${redis.connection:redis://localhost:6379}") String connection,
    @Value("${redis.timeout:2000}") int timeout) {
  RedisConnectionInfo connectionInfo = RedisConnectionInfo.parseConnectionUri(connection);
  GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

  poolConfig.setMaxTotal(100);
  poolConfig.setMinIdle(25);
  poolConfig.setMaxIdle(100);

  return new JedisPool(
      poolConfig,
      connectionInfo.getHost(),
      connectionInfo.getPort(),
      timeout,
      connectionInfo.getPassword(),
      connectionInfo.getDatabase(),
      null /* clientName */,
      connectionInfo.isSsl());
}
 
Example 8
Source File: RedisAutoConfig.java    From unimall with Apache License 2.0 5 votes vote down vote up
@Bean
public GenericObjectPoolConfig defaultPoolConfig() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(maxActive);
    config.setMaxIdle(maxIdle);
    config.setMinIdle(minIdle);
    config.setMaxWaitMillis(maxWait);
    return config;
}
 
Example 9
Source File: ProtostuffSerializePool.java    From Lottor with MIT License 5 votes vote down vote up
public ProtostuffSerializePool(final int maxTotal, final int minIdle, final long maxWaitMillis, final long minEvictableIdleTimeMillis) {
    protostuffpool = new GenericObjectPool<>(new ProtostuffSerializeFactory());

    GenericObjectPoolConfig config = new GenericObjectPoolConfig();

    config.setMaxTotal(maxTotal);
    config.setMinIdle(minIdle);
    config.setMaxWaitMillis(maxWaitMillis);
    config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    protostuffpool.setConfig(config);
}
 
Example 10
Source File: XpipeNettyClientKeyedObjectPool.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
private static GenericObjectPoolConfig createDefaultConfig(int maxPerKey) {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setJmxEnabled(false);
    config.setMaxTotal(maxPerKey);
    config.setBlockWhenExhausted(false);
    return config;
}
 
Example 11
Source File: RateLimiterPluginDataHandler.java    From soul with Apache License 2.0 5 votes vote down vote up
private GenericObjectPoolConfig<?> getPoolConfig(final RateLimiterConfig rateLimiterConfig) {
    GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(rateLimiterConfig.getMaxActive());
    config.setMaxIdle(rateLimiterConfig.getMaxIdle());
    config.setMinIdle(rateLimiterConfig.getMinIdle());
    if (rateLimiterConfig.getMaxWait() != null) {
        config.setMaxWaitMillis(rateLimiterConfig.getMaxWait().toMillis());
    }
    return config;
}
 
Example 12
Source File: JedisHelper.java    From netty.book.kor with MIT License 5 votes vote down vote up
/**
 * 제디스 연결풀 생성을 위한 도우미 클래스 내부 생성자. 싱글톤 패턴이므로 외부에서 호출할 수 없다.
 */
private JedisHelper() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(20);
    config.setBlockWhenExhausted(true);

    this.pool = new JedisPool(config, REDIS_HOST, REDIS_PORT);
}
 
Example 13
Source File: TestCEFParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private GenericObjectPool<StringBuilder> getStringBuilderPool() {
  GenericObjectPoolConfig stringBuilderPoolConfig = new GenericObjectPoolConfig();
  stringBuilderPoolConfig.setMaxTotal(1);
  stringBuilderPoolConfig.setMinIdle(1);
  stringBuilderPoolConfig.setMaxIdle(1);
  stringBuilderPoolConfig.setBlockWhenExhausted(false);
  return new GenericObjectPool<>(new StringBuilderPoolFactory(1024), stringBuilderPoolConfig);
}
 
Example 14
Source File: RedisClusterJedisProvider.java    From conductor with Apache License 2.0 5 votes vote down vote up
@Inject
public RedisClusterJedisProvider(HostSupplier hostSupplier, DynomiteConfiguration configuration) {
    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMaxTotal(configuration.getMaxConnectionsPerHost());
    Host host = hostSupplier.getHosts().get(0);
    this.jedisCluster = new JedisCluster(new redis.clients.jedis.JedisCluster(new HostAndPort(host.getHostName(), host.getPort()),
                                                                              genericObjectPoolConfig));
}
 
Example 15
Source File: ShardedJedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotShareInstances() {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(2);

  ShardedJedisPool pool = new ShardedJedisPool(config, shards);

  ShardedJedis j1 = pool.getResource();
  ShardedJedis j2 = pool.getResource();

  assertNotSame(j1.getShard("foo"), j2.getShard("foo"));
}
 
Example 16
Source File: RedisConfiguration.java    From ad with Apache License 2.0 5 votes vote down vote up
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMaxTotal(maxActive);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setMaxWaitMillis(maxWait);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnCreate(true);
    poolConfig.setTestWhileIdle(true);
    return poolConfig;
}
 
Example 17
Source File: BaseTest.java    From lite-pool with Apache License 2.0 5 votes vote down vote up
public GenericObjectPool<TestObject> createCommonsPool2(int minimum, int maximum, long timeout) {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(maximum);
    config.setMinIdle(minimum);
    config.setMaxIdle(minimum);
    config.setFairness(false);
    config.setJmxEnabled(false);
    config.setBlockWhenExhausted(true);
    config.setTestOnBorrow(false);
    config.setMaxWaitMillis(timeout);
    config.setTestOnCreate(false);
    config.setTestOnReturn(false);
    config.setTestWhileIdle(false);
    return new GenericObjectPool<>( new CommonsPool2Factory(), config);
}
 
Example 18
Source File: DataParserFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected GenericObjectPool<StringBuilder> getStringBuilderPool(Settings settings) {
  int maxRecordLen = getSettings().getMaxRecordLen();
  int poolSize = getSettings().getStringBuilderPoolSize();
  GenericObjectPoolConfig stringBuilderPoolConfig = new GenericObjectPoolConfig();
  stringBuilderPoolConfig.setMaxTotal(poolSize);
  stringBuilderPoolConfig.setMinIdle(poolSize);
  stringBuilderPoolConfig.setMaxIdle(poolSize);
  stringBuilderPoolConfig.setBlockWhenExhausted(false);
  return new GenericObjectPool<>(
    new StringBuilderPoolFactory(maxRecordLen > 0 ? maxRecordLen : DEFAULT_MAX_RECORD_LENGTH),
    stringBuilderPoolConfig
  );
}
 
Example 19
Source File: DynamicJdbcIO.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
DataSource buildDatasource() {
  BasicDataSource basicDataSource = new BasicDataSource();
  if (getDriverClassName() != null) {
    if (getDriverClassName().get() == null) {
      throw new RuntimeException("Driver class name is required.");
    }
    basicDataSource.setDriverClassName(getDriverClassName().get());
  }
  if (getUrl() != null) {
    if (getUrl().get() == null) {
      throw new RuntimeException("Connection url is required.");
    }
    basicDataSource.setUrl(getUrl().get());
  }
  if (getUsername() != null) {
    basicDataSource.setUsername(getUsername().get());
  }
  if (getPassword() != null) {
    basicDataSource.setPassword(getPassword().get());
  }
  if (getConnectionProperties() != null && getConnectionProperties().get() != null) {
    basicDataSource.setConnectionProperties(getConnectionProperties().get());
  }

  /**
   * Since the jdbc connection connection class might have dependencies that are not available
   * to the template, we will localize the user provided dependencies from GCS and create a new
   * {@link URLClassLoader} that is added to the {@link
   * BasicDataSource#setDriverClassLoader(ClassLoader)}
   */
  if (getDriverJars() != null) {
    ClassLoader urlClassLoader = getNewClassLoader(getDriverJars());
    basicDataSource.setDriverClassLoader(urlClassLoader);
  }

  // wrapping the datasource as a pooling datasource
  DataSourceConnectionFactory connectionFactory =
      new DataSourceConnectionFactory(basicDataSource);
  PoolableConnectionFactory poolableConnectionFactory =
      new PoolableConnectionFactory(connectionFactory, null);
  GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
  poolConfig.setMaxTotal(1);
  poolConfig.setMinIdle(0);
  poolConfig.setMinEvictableIdleTimeMillis(10000);
  poolConfig.setSoftMinEvictableIdleTimeMillis(30000);
  GenericObjectPool connectionPool =
      new GenericObjectPool(poolableConnectionFactory, poolConfig);
  poolableConnectionFactory.setPool(connectionPool);
  poolableConnectionFactory.setDefaultAutoCommit(false);
  poolableConnectionFactory.setDefaultReadOnly(false);
  PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool);
  return poolingDataSource;
}
 
Example 20
Source File: TestDriverManagerConnectionFactory.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
public void testDriverManagerInit(final boolean withProperties) throws Exception {
    final GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(10);
    config.setMaxIdle(0);
    final Properties properties = new Properties();
    // The names "user" and "password" are specified in java.sql.DriverManager.getConnection(String, String, String)
    properties.put("user", "foo");
    properties.put("password", "bar");
    final ConnectionFactory connectionFactory = withProperties ?
            new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", properties) :
            new DriverManagerConnectionFactory("jdbc:apache:commons:testdriver", "foo", "bar");
    final PoolableConnectionFactory poolableConnectionFactory =
        new PoolableConnectionFactory(connectionFactory, null);
    poolableConnectionFactory.setDefaultReadOnly(Boolean.FALSE);
    poolableConnectionFactory.setDefaultAutoCommit(Boolean.TRUE);

    final GenericObjectPool<PoolableConnection> connectionPool =
            new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    final PoolingDataSource<PoolableConnection> dataSource =
            new PoolingDataSource<>(connectionPool);

    final ConnectionThread[] connectionThreads = new ConnectionThread[10];
    final Thread[] threads = new Thread[10];

    for (int i = 0; i < 10; i++) {
        connectionThreads[i] = new ConnectionThread(dataSource);
        threads[i] = new Thread(connectionThreads[i]);
    }
    for (int i = 0; i < 10; i++) {
        threads[i].start();
    }
    for (int i = 0; i < 10; i++) {
        while (threads[i].isAlive()){//JDK1.5: getState() != Thread.State.TERMINATED) {
            Thread.sleep(100);
        }
        if (!connectionThreads[i].getResult()) {
            fail("Exception during getConnection(): " + connectionThreads[i]);
        }
    }
}