Java Code Examples for org.apache.commons.pool2.PooledObjectFactory

The following are top voted examples for showing how to use org.apache.commons.pool2.PooledObjectFactory. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: commons-pool2-sr   File: GenericObjectPool.java   Source Code and License 6 votes vote down vote up
/**
 * 使用特定的配置来创建一个新的"通用对象池"实例。
 * <p>
 * Create a new <code>GenericObjectPool</code> using a specific
 * configuration.
 *
 * @param factory   The object factory to be used to create object instances
 *                  used by this pool (用于创建"池对象"实例的对象工厂)
 * @param config    The configuration to use for this pool instance. (用于该对象池实例的配置信息)
 *                  The configuration is used by value. Subsequent changes to
 *                  the configuration object will not be reflected in the
 *                  pool. (随后对配置对象的更改将不会反映到对象池中)
 */
public GenericObjectPool(PooledObjectFactory<T> factory,
        GenericObjectPoolConfig config) {

    super(config, ONAME_BASE, config.getJmxNamePrefix());

    if (factory == null) {
        jmxUnregister(); // tidy up
        throw new IllegalArgumentException("factory may not be null");
    }
    this.factory = factory;

    this.setConfig(config);
    // 启动"驱逐者线程"
    this.startEvictor(this.getTimeBetweenEvictionRunsMillis());
}
 
Example 2
Project: commons-pool   File: GenericObjectPool.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new <code>GenericObjectPool</code> using a specific
 * configuration.
 *
 * @param factory   The object factory to be used to create object instances
 *                  used by this pool
 * @param config    The configuration to use for this pool instance. The
 *                  configuration is used by value. Subsequent changes to
 *                  the configuration object will not be reflected in the
 *                  pool.
 */
public GenericObjectPool(final PooledObjectFactory<T> factory,
        final GenericObjectPoolConfig config) {

    super(config, ONAME_BASE, config.getJmxNamePrefix());

    if (factory == null) {
        jmxUnregister(); // tidy up
        throw new IllegalArgumentException("factory may not be null");
    }
    this.factory = factory;

    idleObjects = new LinkedBlockingDeque<>(config.getFairness());

    setConfig(config);

    startEvictor(getTimeBetweenEvictionRunsMillis());
}
 
Example 3
Project: commons-pool   File: BaseTestProxiedObjectPool.java   Source Code and License 6 votes vote down vote up
@Before
public void setUp() {
    log = new StringWriter();

    final PrintWriter pw = new PrintWriter(log);
    final AbandonedConfig abandonedConfig = new AbandonedConfig();
    abandonedConfig.setLogAbandoned(true);
    abandonedConfig.setRemoveAbandonedOnBorrow(true);
    abandonedConfig.setUseUsageTracking(true);
    abandonedConfig.setRemoveAbandonedTimeout(ABANDONED_TIMEOUT_SECS);
    abandonedConfig.setLogWriter(pw);

    final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(3);

    final PooledObjectFactory<TestObject> factory = new TestObjectFactory();

    final ObjectPool<TestObject> innerPool =
            new GenericObjectPool<>(factory, config, abandonedConfig);

    pool = new ProxiedObjectPool<>(innerPool, getproxySource());
}
 
Example 4
Project: JRediClients   File: Pool.java   Source Code and License 5 votes vote down vote up
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {

		if (this.internalPool != null) {
			try {
				closeInternalPool();
			} catch (Exception e) {
			}
		}

		this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
	}
 
Example 5
Project: x7   File: Pool.java   Source Code and License 5 votes vote down vote up
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {

    if (this.internalPool != null) {
      try {
        closeInternalPool();
      } catch (Exception e) {
      }
    }

    this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
  }
 
Example 6
Project: presto-kudu   File: KuduClientPool.java   Source Code and License 5 votes vote down vote up
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<KuduClient> factory)
{
    if (this.internalPool != null) {
        try {
            closeInternalPool();
        }
        catch (Exception e) {
        }
    }

    this.internalPool = new GenericObjectPool<KuduClient>(factory, poolConfig);
}
 
Example 7
Project: cachecloud   File: Pool.java   Source Code and License 5 votes vote down vote up
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {

    if (this.internalPool != null) {
      try {
        closeInternalPool();
      } catch (Exception e) {
      }
    }

    this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
  }
 
Example 8
Project: Jedis   File: Pool.java   Source Code and License 5 votes vote down vote up
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
  initPool(poolConfig, factory);
}
 
Example 9
Project: Jedis   File: Pool.java   Source Code and License 5 votes vote down vote up
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {

    if (this.internalPool != null) {
      try {
        closeInternalPool();
      } catch (Exception e) {
      }
    }

    this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
  }
 
Example 10
Project: connection-pool-client   File: PoolBase.java   Source Code and License 5 votes vote down vote up
/**
 * <p>Title: initPool</p>
 * <p>Description: 初始化对象池</p>
 *
 * @param poolConfig 池配置
 * @param factory    池对象工厂
 */
protected void initPool(final GenericObjectPoolConfig poolConfig,
                        PooledObjectFactory<T> factory) {
    if (this.internalPool != null)
        this.destroy();

    this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
}
 
Example 11
Project: BStalkJ   File: BeanstalkPool.java   Source Code and License 5 votes vote down vote up
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<Client> factory) {
    if (this.internalPool != null) {
        try {
            closeInternalPool();
        } catch (Exception e) {
            //ignore
        }
    }
    this.internalPool = new GenericObjectPool<Client>(factory, poolConfig);
}
 
Example 12
Project: jedis-sr   File: Pool.java   Source Code and License 5 votes vote down vote up
/**
 * 初始化"连接池"。
 * 
 * @param poolConfig
 *            连接池配置信息
 * @param factory
 *            连接池对象的工厂
 */
public void initPool(GenericObjectPoolConfig poolConfig,
		PooledObjectFactory<R> factory) {
	// 若原有的连接池资源未被释放,则先关闭
	if (this.internalPool != null) {
		try {
			this.closeInternalPool();
		} catch (Exception e) {
		}
	}

	this.internalPool = new GenericObjectPool<R>(factory, poolConfig);
}
 
Example 13
Project: proctor   File: SvnObjectPools.java   Source Code and License 5 votes vote down vote up
private static <T> ObjectPool<T> createObjectPool(final PooledObjectFactory<T> factory) {
    final GenericObjectPoolConfig objectPoolConfig = new GenericObjectPoolConfig();
    objectPoolConfig.setMinEvictableIdleTimeMillis(TimeUnit.HOURS.toMillis(1)); // arbitrary, but positive so objects do get evicted
    objectPoolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.MINUTES.toMillis(10)); // arbitrary, but positive so objects do get evicted
    objectPoolConfig.setJmxEnabled(false);
    objectPoolConfig.setBlockWhenExhausted(false);
    objectPoolConfig.setMaxTotal(-1); // uncapped number of objects in the pool
    final AbandonedConfig abandonedConfig = new AbandonedConfig();
    abandonedConfig.setRemoveAbandonedOnBorrow(true);
    abandonedConfig.setRemoveAbandonedTimeout((int) TimeUnit.MINUTES.toSeconds(30));
    return new GenericObjectPool<T>(factory, objectPoolConfig, abandonedConfig);
}
 
Example 14
Project: JRediClients   File: Pool.java   Source Code and License 4 votes vote down vote up
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
	initPool(poolConfig, factory);
}
 
Example 15
Project: FKC   File: KryoPoolCommonsImpl.java   Source Code and License 4 votes vote down vote up
public KryoPoolCommonsImpl(GenericObjectPoolConfig genericObjectPoolConfig, PooledObjectFactory<Kryo> pooledObjectFactory, AbandonedConfig abandonedConfig, FKCExceptionHandler fkcExceptionHandler) {
    this.kryoPool = new GenericObjectPool<Kryo>(pooledObjectFactory, genericObjectPoolConfig, abandonedConfig);
    this.fkcExceptionHandler = fkcExceptionHandler;
}
 
Example 16
Project: FKC   File: KryoPool.java   Source Code and License 4 votes vote down vote up
public CommonsBuilder(PooledObjectFactory<Kryo> kryoPooledObjectFactory) {
    if (kryoPooledObjectFactory == null) {
        throw new IllegalArgumentException("pooledObjectFactory must not be null");
    }
    this.pooledObjectFactory = kryoPooledObjectFactory;
}
 
Example 17
Project: FKC   File: KryoPoolConfig.java   Source Code and License 4 votes vote down vote up
public void setPooledObjectFactory(PooledObjectFactory<Kryo> pooledObjectFactory) {
    this.pooledObjectFactory = pooledObjectFactory;
}
 
Example 18
Project: FKC   File: KryoPoolConfig.java   Source Code and License 4 votes vote down vote up
public PooledObjectFactory<Kryo> getPooledObjectFactory() {
    return pooledObjectFactory;
}
 
Example 19
Project: x7   File: Pool.java   Source Code and License 4 votes vote down vote up
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
  initPool(poolConfig, factory);
}
 
Example 20
Project: game-executor   File: UpdateEventCacheFactory.java   Source Code and License 4 votes vote down vote up
public UpdateEventCacheFactory(PooledObjectFactory<UpdateEvent> factory, GenericObjectPoolConfig config) {
    super(factory, config);
}
 
Example 21
Project: presto-kudu   File: KuduClientPool.java   Source Code and License 4 votes vote down vote up
private KuduClientPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<KuduClient> factory)
{
    initPool(poolConfig, factory);
}
 
Example 22
Project: asglogic   File: IOBehaviourSimulationStepPool.java   Source Code and License 4 votes vote down vote up
public IOBehaviourSimulationStepPool(PooledObjectFactory<IOBehaviourSimulationStep> factory) {
    super(factory);
}
 
Example 23
Project: cachecloud   File: JedisPoolAbstract.java   Source Code and License 4 votes vote down vote up
public JedisPoolAbstract(GenericObjectPoolConfig poolConfig, PooledObjectFactory<Jedis> factory) {
  super(poolConfig, factory);
}
 
Example 24
Project: cachecloud   File: Pool.java   Source Code and License 4 votes vote down vote up
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
  initPool(poolConfig, factory);
}
 
Example 25
Project: Jedis   File: JedisPoolAbstract.java   Source Code and License 4 votes vote down vote up
public JedisPoolAbstract(GenericObjectPoolConfig poolConfig, PooledObjectFactory<Jedis> factory) {
  super(poolConfig, factory);
}
 
Example 26
Project: shachi   File: HBaseResourcePoolDefault.java   Source Code and License 4 votes vote down vote up
public HBaseResourcePoolDefault(final PooledObjectFactory<R> factory, final GenericObjectPoolConfig config, final AbandonedConfig abandonedConfig) {
    super(factory, config, abandonedConfig);
}
 
Example 27
Project: shachi   File: HBaseResourcePoolDefault.java   Source Code and License 4 votes vote down vote up
public HBaseResourcePoolDefault(final PooledObjectFactory<R> factory, final GenericObjectPoolConfig config) {
    super(factory, config);
}
 
Example 28
Project: shachi   File: HBaseResourcePoolDefault.java   Source Code and License 4 votes vote down vote up
public HBaseResourcePoolDefault(final PooledObjectFactory<R> factory) {
    super(factory);
}
 
Example 29
Project: knowledge   File: DictionaryPool.java   Source Code and License 4 votes vote down vote up
public DictionaryObjectPool(PooledObjectFactory<Dictionary> factory)
{
    super(factory);
    setLifo(false);
}
 
Example 30
Project: knowledge   File: MultiQueryEnginePool.java   Source Code and License 4 votes vote down vote up
public MultiQueryEngineObjectPool(PooledObjectFactory<MultiQueryEngine> factory)
{
    super(factory);
    setLifo(false);
}
 
Example 31
Project: knowledge   File: QueryEnginePool.java   Source Code and License 4 votes vote down vote up
public QueryEngineObjectPool(PooledObjectFactory<QueryEngine> factory)
{
    super(factory);
    setLifo(false);
}
 
Example 32
Project: connection-pool-client   File: RedisSentinelConnPoolOldTest.java   Source Code and License 4 votes vote down vote up
@Override
protected void initPool(GenericObjectPoolConfig poolConfig,
                        PooledObjectFactory<Jedis> factory) {
    // TODO Auto-generated method stub
    super.initPool(poolConfig, factory);
}
 
Example 33
Project: BStalkJ   File: BeanstalkPool.java   Source Code and License 4 votes vote down vote up
public BeanstalkPool(final GenericObjectPoolConfig poolConfig, BeanstalkFactory factory) {
    PooledObjectFactory<Client> pooledFactory = BeanstalkPooledFactory.pooledFactory().factory(factory).build();
    initPool(poolConfig, pooledFactory);
}
 
Example 34
Project: flow   File: ConfigObjectPool.java   Source Code and License 4 votes vote down vote up
public ConfigObjectPool(@Nonnull PooledObjectFactory<T> factory, @Nonnull Config config) {
	super(checkNotNull(factory), buildConfig(checkNotNull(config)));
}
 
Example 35
Project: ddth-redis   File: RedisClientPool.java   Source Code and License 4 votes vote down vote up
public RedisClientPool(PooledObjectFactory<T> factory, PoolConfig poolConfig) {
    super(factory);
    setPoolConfig(poolConfig);
}
 
Example 36
Project: ddth-redis   File: JedisClientPool.java   Source Code and License 4 votes vote down vote up
public JedisClientPool(PooledObjectFactory<JedisRedisClient> factory, PoolConfig poolConfig) {
    super(factory, poolConfig);
}
 
Example 37
Project: commons-pool   File: TestGenericObjectPool.java   Source Code and License 4 votes vote down vote up
@Override
protected ObjectPool<Object> makeEmptyPool(
        final PooledObjectFactory<Object> fac) {
    return new GenericObjectPool<>(fac);
}
 
Example 38
Project: commons-pool   File: TestGenericObjectPool.java   Source Code and License 4 votes vote down vote up
@Test(timeout=60000)
public void testConstructors() throws Exception {

    // Make constructor arguments all different from defaults
    final int minIdle = 2;
    final long maxWait = 3;
    final int maxIdle = 4;
    final int maxTotal = 5;
    final long minEvictableIdleTimeMillis = 6;
    final int numTestsPerEvictionRun = 7;
    final boolean testOnBorrow = true;
    final boolean testOnReturn = true;
    final boolean testWhileIdle = true;
    final long timeBetweenEvictionRunsMillis = 8;
    final boolean blockWhenExhausted = false;
    final boolean lifo = false;
    final PooledObjectFactory<Object> dummyFactory = new DummyFactory();
    try (GenericObjectPool<Object> dummyPool = new GenericObjectPool<>(dummyFactory)) {
        assertEquals(GenericObjectPoolConfig.DEFAULT_MAX_IDLE, dummyPool.getMaxIdle());
        assertEquals(BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS, dummyPool.getMaxWaitMillis());
        assertEquals(GenericObjectPoolConfig.DEFAULT_MIN_IDLE, dummyPool.getMinIdle());
        assertEquals(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL, dummyPool.getMaxTotal());
        assertEquals(BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
                dummyPool.getMinEvictableIdleTimeMillis());
        assertEquals(BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
                dummyPool.getNumTestsPerEvictionRun());
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW),
                Boolean.valueOf(dummyPool.getTestOnBorrow()));
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_ON_RETURN),
                Boolean.valueOf(dummyPool.getTestOnReturn()));
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE),
                Boolean.valueOf(dummyPool.getTestWhileIdle()));
        assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
                dummyPool.getTimeBetweenEvictionRunsMillis());
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED),
                Boolean.valueOf(dummyPool.getBlockWhenExhausted()));
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_LIFO), Boolean.valueOf(dummyPool.getLifo()));
    }

    final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setLifo(lifo);
    config.setMaxIdle(maxIdle);
    config.setMinIdle(minIdle);
    config.setMaxTotal(maxTotal);
    config.setMaxWaitMillis(maxWait);
    config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    config.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    config.setTestOnBorrow(testOnBorrow);
    config.setTestOnReturn(testOnReturn);
    config.setTestWhileIdle(testWhileIdle);
    config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    config.setBlockWhenExhausted(blockWhenExhausted);
    try (GenericObjectPool<Object> dummyPool = new GenericObjectPool<>(dummyFactory, config)) {
        assertEquals(maxIdle, dummyPool.getMaxIdle());
        assertEquals(maxWait, dummyPool.getMaxWaitMillis());
        assertEquals(minIdle, dummyPool.getMinIdle());
        assertEquals(maxTotal, dummyPool.getMaxTotal());
        assertEquals(minEvictableIdleTimeMillis, dummyPool.getMinEvictableIdleTimeMillis());
        assertEquals(numTestsPerEvictionRun, dummyPool.getNumTestsPerEvictionRun());
        assertEquals(Boolean.valueOf(testOnBorrow), Boolean.valueOf(dummyPool.getTestOnBorrow()));
        assertEquals(Boolean.valueOf(testOnReturn), Boolean.valueOf(dummyPool.getTestOnReturn()));
        assertEquals(Boolean.valueOf(testWhileIdle), Boolean.valueOf(dummyPool.getTestWhileIdle()));
        assertEquals(timeBetweenEvictionRunsMillis, dummyPool.getTimeBetweenEvictionRunsMillis());
        assertEquals(Boolean.valueOf(blockWhenExhausted), Boolean.valueOf(dummyPool.getBlockWhenExhausted()));
        assertEquals(Boolean.valueOf(lifo), Boolean.valueOf(dummyPool.getLifo()));
    }
}
 
Example 39
Project: commons-pool   File: TestSoftReferenceObjectPool.java   Source Code and License 4 votes vote down vote up
@Override
protected ObjectPool<Object> makeEmptyPool(final PooledObjectFactory<Object> factory) {
    return new SoftReferenceObjectPool<>(factory);
}
 
Example 40
Project: commons-pool   File: TestSynchronizedPooledObjectFactory.java   Source Code and License 3 votes vote down vote up
/**
 * Create a SynchronizedPoolableObjectFactory wrapping the given factory.
 *
 * @param factory
 *            underlying factory to wrap
 * @throws IllegalArgumentException
 *             if the factory is null
 */
TestSynchronizedPooledObjectFactory(final PooledObjectFactory<T> factory) throws IllegalArgumentException {
	if (factory == null) {
		throw new IllegalArgumentException("factory must not be null.");
	}
	this.factory = factory;
}