Example usage for org.apache.commons.pool2.impl GenericObjectPoolConfig setMaxTotal

List of usage examples for org.apache.commons.pool2.impl GenericObjectPoolConfig setMaxTotal

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPoolConfig setMaxTotal.

Prototype

public void setMaxTotal(int maxTotal) 

Source Link

Document

Set the value for the maxTotal configuration attribute for pools created with this configuration instance.

Usage

From source file:org.zbus.pool.impl.CommonsPool2.java

public CommonsPool2(ObjectFactory<T> supportFactory, PoolConfig config) {
    Commons2PoolFactory factory = new Commons2PoolFactory(supportFactory);
    GenericObjectPoolConfig poolConfig = null;
    if (config.getSupport() instanceof GenericObjectPoolConfig) {
        poolConfig = (GenericObjectPoolConfig) config.getSupport();
    } else {/*ww w. j av  a 2 s .co m*/
        poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(config.getMaxTotal());
        poolConfig.setMaxIdle(config.getMaxIdle());
        poolConfig.setMinIdle(config.getMinIdle());
        poolConfig.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
    }

    this.support = new GenericObjectPool<T>(factory, poolConfig);
}

From source file:rda.agent.client.AgentConnection.java

private void createObjectPool(Integer poolsize, String[] aghost) {
    GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
    conf.setMaxIdle(poolsize);//w w w.j  ava  2s . c o  m
    conf.setMaxTotal(poolsize);

    this._pool = new GenericObjectPool<>(new AgentClientFactory(aghost[0], aghost[1], aghost[2]), conf);

    System.out.println("***********************************************************");
    System.out.println("total:" + ((GenericObjectPool) _pool).getMaxTotal() + " , minIdle:"
            + ((GenericObjectPool) _pool).getMinIdle() + " , maxIdle:"
            + ((GenericObjectPool) _pool).getMaxIdle());
    System.out.println("***********************************************************");

    this.aghost = aghost;
}

From source file:redis.client.jedis.CustomShardedJedisPoolTest.java

@BeforeClass
public void init() throws InterruptedException {
    List<JedisShardInfo> shards = RedisConfigUtils.parseRedisServerList(TestConfigUtils.getRedisServers(),
            TestConfigUtils.getTimeoutMillis());

    GenericObjectPoolConfig poolConfig = new JedisPoolConfig();
    // ?//from ww  w .  j  a v a  2 s  .  c  o m
    poolConfig.setMaxTotal(TestConfigUtils.getMaxTotalNum());
    poolConfig.setMaxIdle(TestConfigUtils.getMaxIdleNum());
    // poolConfig.setMinIdle(TestConfigUtils.getMinIdleNum());
    poolConfig.setMinIdle(3); // local test
    // ?""
    boolean lifo = TestConfigUtils.getPoolBehaviour() == PoolBehaviour.LIFO ? true : false;
    poolConfig.setLifo(lifo);
    // ?
    poolConfig.setBlockWhenExhausted(false);
    // 
    // poolConfig.setBlockWhenExhausted(true);
    // poolConfig.setMaxWaitMillis(TimeUnit.MILLISECONDS.toMillis(10L));

    // ""?
    poolConfig.setTestOnBorrow(false);
    poolConfig.setTestOnReturn(false);

    /*
     * "Evictor?"??""
     */
    poolConfig.setTestWhileIdle(true);
    // ?5?????
    // poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.SECONDS.toMillis(TestConfigUtils.getTimeBetweenEvictionRunsSeconds()));
    poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.SECONDS.toMillis(2L)); // local test
    // ??Evictor
    // poolConfig.setTimeBetweenEvictionRunsMillis(-1L); // local test
    // ?
    // poolConfig.setNumTestsPerEvictionRun(TestConfigUtils.getNumTestsPerEvictionRun());
    poolConfig.setNumTestsPerEvictionRun(3); // local test
    // ?
    poolConfig.setSoftMinEvictableIdleTimeMillis(
            TimeUnit.MINUTES.toMillis(TestConfigUtils.getMinEvictableIdleTimeMinutes()));
    // ??(?)
    // ?
    poolConfig.setMinEvictableIdleTimeMillis(
            TimeUnit.MINUTES.toMillis(TestConfigUtils.getMaxEvictableIdleTimeMinutes()));

    this.shardedJedisPool = new CustomShardedJedisPool(poolConfig, shards, (int) TimeUnit.SECONDS.toMillis(1),
            2);
}

From source file:spnodechecker.client.ConnectionPool.java

public ConnectionPool(String url) {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(200);
    this.connectionPool = new GenericObjectPool(new ConnectionPooledFactory(url), config);
}

From source file:stroom.pool.AbstractPoolCacheBean.java

@Override
protected ObjectPool<PoolItem<K, V>> create(final K key) {
    final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1000);
    config.setMaxIdle(1000);//from  w w w. j  a v a  2 s.c  o m
    config.setBlockWhenExhausted(false);

    return new GenericObjectPool<>(new ObjectFactory<>(this, key), config);
}

From source file:stroom.statistics.server.sql.SQLStatisticEventStore.java

private GenericObjectPoolConfig getObjectPoolConfig() {
    final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    // Max number of idle items .... same as our pool size
    config.setMaxIdle(poolSize);//from ww w  .ja va 2s . c o m
    // Pool size
    config.setMaxTotal(poolSize);
    // Returns the minimum amount of time an object may sit idle in the pool
    // before it is eligible for eviction by the idle object evictor
    // Here if it is idle for 10 min's it will simply return It will also
    // return by validateObject if it is simple more than 10min old
    config.setMinEvictableIdleTimeMillis(poolAgeMsThreshold);
    // Check for idle objects never .... we will do this with task sytstem
    config.setTimeBetweenEvictionRunsMillis(0);
    // Must cause other threads to block to wait for a object
    config.setBlockWhenExhausted(true);
    config.setJmxEnabled(false);
    // Check item on just before returning to pool
    config.setTestOnReturn(true);

    return config;
}

From source file:survey.data.RepositoryFactory.java

public RepositoryFactory(Properties properties) {
    assert properties != null;

    _properties = properties;/* www .j ava 2s . c om*/
    _localCache = new LocalCache(properties);
    String redisHost = _properties == null ? null : _properties.getProperty("redis.host");
    if (redisHost == null) {
        redisHost = "localhost";
    }
    int redisPort;
    try {
        redisPort = Integer.parseInt(_properties.getProperty("redis.port"));
    } catch (Throwable e) {
        redisPort = 6379;
    }
    // Anedotally, the web service will eventually fail due to a broken
    // connection to Redis, unless a pool is used.
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setBlockWhenExhausted(false); // Fail if out of sockets.
    poolConfig.setLifo(true);
    poolConfig.setMaxIdle(50); // All 50 sockets can be briefly idle.
    poolConfig.setMaxTotal(50); // After 50 sockets used then fail.
    poolConfig.setMinEvictableIdleTimeMillis(600000); // 10 minutes even if few sockets are idle.
    poolConfig.setMinIdle(2); // Keep 2 sockets ready.
    poolConfig.setSoftMinEvictableIdleTimeMillis(120000); // 2 minutes unless insufficient sockets are idle.
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestWhileIdle(false);
    poolConfig.setTimeBetweenEvictionRunsMillis(300000); // Check for idle sockets every 5 minutes.
    _jedisPool = new JedisPool(poolConfig, redisHost, redisPort);

    // Monitor changes in Redis.
    _subscribeService = Executors.newSingleThreadExecutor();

    final JedisPubSub jedisPubSub = new JedisPubSub() {
        @Override
        public void onMessage(String channelName, String notificationKey) {
            // _logger.info(String.format("onMessage(%s, %s)", channelName, notificationKey));
            // See the publish() method below.
            String[] splitKey = notificationKey.split(":", 4);
            assert splitKey.length >= 4;
            String magic = splitKey[0];
            assert "survey".equals(magic);
            String namespaceName = splitKey[1];
            String surveyPath = splitKey[2];
            long turnout;
            try {
                turnout = Long.parseLong(splitKey[3]);
            } catch (NumberFormatException e) {
                turnout = 0;
            }
            onVoteAdded(namespaceName, surveyPath, turnout);
        }
    };

    _subscribeService.submit(new Runnable() {
        @Override
        public void run() {
            _logger.info("subscribe thread running");
            int sleepMillis = 0;
            for (;;) {
                Jedis jedis = null;
                try {
                    if (sleepMillis != 0) {
                        Thread.sleep(sleepMillis);
                    }
                    jedis = _jedisPool.getResource();
                    jedis.subscribe(jedisPubSub, _CHANNEL_NAME);
                } catch (InterruptedException e) {
                    _logger.info("subscribe thread interrupted");
                    break;
                } catch (Throwable e) {
                    // e.g. if there is a Jedis exception, sleep 1 minute and try again.
                    sleepMillis = 60000;
                } finally {
                    if (jedis != null) {
                        _jedisPool.returnResourceObject(jedis);
                    }
                }
            }
        }
    });

    _logger = LoggerFactory.getLogger(RepositoryFactory.class);
    _logger.info("created repository factory (singleton)");
}

From source file:vn.ethicconsultant.commons.memcachedpool2.Test.Test.java

public static void main(String[] args) throws Exception {
    String host = "192.168.10.225";
    int port = 11211;
    MemcachedPool memcachedPool = new MemcachedPool();
    MemcachedPoolableFactory memcachedPoolableFactory = new MemcachedPoolableFactory(host, port);
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxWaitMillis(500);//from www .j a  va 2 s  .c  o m
    poolConfig.setMaxTotal(10);
    poolConfig.setMaxIdle(10);
    //poolConfig.setTestOnBorrow(true);
    //poolConfig.setTestOnCreate(true);
    memcachedPool.initPool(poolConfig, memcachedPoolableFactory);

    MemcachedClient mc = memcachedPool.getResource();
    mc.set("hung1334", 3600, "heo");
    mc.set("hung1335", 3600, "heo");
    mc.set("hung1336", 3600, "heo");
    memcachedPool.returnResource(mc);
    mc = memcachedPool.getResource();
    System.out.println(mc.get("hung1334"));
    memcachedPool.returnResource(mc);
    mc = memcachedPool.getResource();

    System.out.println(mc.get("hung1335"));
    memcachedPool.returnResource(mc);
    mc = memcachedPool.getResource();
    System.out.println(mc.get("hung1336"));
    memcachedPool.returnResource(mc);

}

From source file:za.co.wilderness.WildernessPoolingDriver.java

private void setupDriver() throws Exception {

    String jdbcDriverName = "com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource";

    try {//  ww  w .  j  ava2s .  c o  m
        java.lang.Class.forName(jdbcDriverName).newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        //System.out.println("Error when attempting to obtain DB Driver: " + jdbcDriverName + " on "+ new Date().toString() + e.getMessage());
        logger.log(Level.SEVERE,
                "Error when attempting to obtain DB Driver: " + jdbcDriverName + " on " + new Date().toString(),
                e);
        throw new Exception(e);
    }

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(this.connectURI, this.properties);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    GenericObjectPoolConfig genConfig = new GenericObjectPoolConfig();
    genConfig.setMaxIdle(this.config.getMaxIdle());
    genConfig.setMaxTotal(this.config.getMaxActive());
    genConfig.setMinIdle(this.config.getMinIdle());
    genConfig.setMaxWaitMillis(this.config.getMaxWaitMillis());
    genConfig.setTimeBetweenEvictionRunsMillis(5000);
    genConfig.setTestWhileIdle(true);

    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
            genConfig);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself...
    //
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    //System.out.println("Driver : " + driver.toString());
    logger.log(Level.FINE, "Driver : " + driver.toString());

    //
    // ...and register our pool with it.
    //
    driver.registerPool(EXTERNAL_SERVICE.name(), connectionPool);

    //
    // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
    // to access our pool of Connections.
    //
}