org.apache.commons.pool2.PooledObjectFactory Java Examples

The following examples show how to use org.apache.commons.pool2.PooledObjectFactory. 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: KryoSerialization.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void initPool() {
    // assume that application context is already initialized
    Configuration configuration = AppBeans.get(Configuration.NAME);
    config = configuration.getConfig(KryoSerializationConfig.class);

    int poolSize = config.getMaxPoolSize();
    GenericObjectPoolConfig<Kryo> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMaxIdle(poolSize);
    poolConfig.setMaxTotal(poolSize);
    poolConfig.setMaxWaitMillis(config.getMaxBorrowWaitMillis());

    String jmxName = "kryo-" + AppContext.getProperty("cuba.webContextName");
    poolConfig.setJmxNamePrefix(jmxName);

    PooledObjectFactory<Kryo> factory = new KryoObjectFactory(this);
    pool = new GenericObjectPool<>(factory, poolConfig);
    log.debug("Kryo context pool created");
}
 
Example #2
Source File: BaseTestProxiedObjectPool.java    From commons-pool with Apache License 2.0 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<TestObject> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(3);

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

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

    pool = new ProxiedObjectPool<>(innerPool, getproxySource());
}
 
Example #3
Source File: GenericObjectPool.java    From commons-pool with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@code GenericObjectPool} 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<T> 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);
}
 
Example #4
Source File: PoolImplUtils.java    From commons-pool with Apache License 2.0 6 votes vote down vote up
/**
 * Identifies the concrete type of object that an object factory creates.
 *
 * @param factoryClass
 *            The factory to examine
 *
 * @return the type of object the factory creates
 */
@SuppressWarnings("rawtypes")
static Class<?> getFactoryType(final Class<? extends PooledObjectFactory> factoryClass) {
    final Class<PooledObjectFactory> type = PooledObjectFactory.class;
    final Object genericType = getGenericType(type, factoryClass);
    if (genericType instanceof Integer) {
        // POOL-324 org.apache.commons.pool2.impl.GenericObjectPool.getFactoryType() throws
        // java.lang.ClassCastException
        //
        // A bit hackish, but we must handle cases when getGenericType() does not return a concrete types.
        final ParameterizedType pi = getParameterizedType(type, factoryClass);
        if (pi != null) {
            final Type[] bounds = ((TypeVariable) pi.getActualTypeArguments()[(Integer) genericType]).getBounds();
            if (bounds != null && bounds.length > 0) {
                final Type bound0 = bounds[0];
                if (bound0 instanceof Class) {
                    return (Class<?>) bound0;
                }
            }
        }
        // last resort: Always return a Class
        return Object.class;
    }
    return (Class<?>) genericType;
}
 
Example #5
Source File: ApiLdapClientApiOsgiTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Override
protected void useBundleClasses() throws Exception
{
    new LdapNetworkConnection().close();
    new SaslGssApiRequest();
    new Krb5LoginConfiguration();
    new AddFuture( new LdapNetworkConnection(), 2 );
    new LdapConnectionTemplate( new LdapConnectionPool( new DefaultPoolableLdapConnectionFactory(
        new LdapConnectionConfig() ) ) );
    FilterBuilder.and( FilterBuilder.not( FilterBuilder.contains( "cn", "a", "b" ) ) ).toString();

    // Test for DIRAPI-239
    PooledObjectFactory<LdapConnection> factory = new DefaultPoolableLdapConnectionFactory(
        new LdapConnectionConfig() );
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    LdapConnectionPool ldapConnectionPool = new LdapConnectionPool( factory, config );
    ldapConnectionPool.getLdapApiService();
    ldapConnectionPool.getTestOnBorrow();
    ldapConnectionPool.close();
}
 
Example #6
Source File: SvnObjectPools.java    From proctor with Apache License 2.0 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 #7
Source File: Pool.java    From cachecloud with Apache License 2.0 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
Source File: Dom4jTools.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected void initPool() {
    int poolSize = config.getMaxPoolSize();
    GenericObjectPoolConfig<SAXParser> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMaxIdle(poolSize);
    poolConfig.setMaxTotal(poolSize);
    poolConfig.setMaxWaitMillis(config.getMaxBorrowWaitMillis());

    String jmxName = "dom4JTools-" + globalConfig.getWebContextName();
    poolConfig.setJmxNamePrefix(jmxName);

    PooledObjectFactory<SAXParser> factory = new SAXParserObjectFactory();
    pool = new GenericObjectPool<>(factory, poolConfig);
}
 
Example #9
Source File: Pool.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" 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
Source File: Pool.java    From elasticsearch-pool with Apache License 2.0 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 #11
Source File: Pool.java    From seppb with MIT 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) {
            log.error("", e);
        }
    }
    this.internalPool = new GenericObjectPool<>(factory, poolConfig);
}
 
Example #12
Source File: TestSoftReferenceObjectPool.java    From commons-pool with Apache License 2.0 4 votes vote down vote up
@Override
protected ObjectPool<Object> makeEmptyPool(final PooledObjectFactory<Object> factory) {
    return new SoftReferenceObjectPool<>(factory);
}
 
Example #13
Source File: RpcJedisDelegatePool.java    From hasting with MIT License 4 votes vote down vote up
@Override
public void initPool(GenericObjectPoolConfig poolConfig,
		PooledObjectFactory<Jedis> factory) {
	redisPool.initPool(poolConfig, factory);
}
 
Example #14
Source File: TestGenericObjectPool.java    From commons-pool with Apache License 2.0 4 votes vote down vote up
@Override
protected ObjectPool<Object> makeEmptyPool(
        final PooledObjectFactory<Object> fac) {
    return new GenericObjectPool<>(fac);
}
 
Example #15
Source File: AnalyzerPool.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public AnalyzerPool(PooledObjectFactory<T> factory, GenericObjectPoolConfig config) {
    super(factory, config);
}
 
Example #16
Source File: AnalyzerPool.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public AnalyzerPool(PooledObjectFactory<T> factory) {
    super(factory);
}
 
Example #17
Source File: InstrumentedJedisPool.java    From kork with Apache License 2.0 4 votes vote down vote up
@Override
public void initPool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<Jedis> factory) {
  // Explicitly not initializing the pool here, as the delegated pool will initialize itself
}
 
Example #18
Source File: XpipeNettyClientPool.java    From x-pipe with Apache License 2.0 4 votes vote down vote up
public XpipeNettyClientPool(Endpoint target, GenericObjectPoolConfig config, PooledObjectFactory<NettyClient> factory) {
	this.factory = factory;
	this.config = config;
	this.target = target;
}
 
Example #19
Source File: TestGenericObjectPool.java    From commons-pool with Apache License 2.0 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<Object> 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 #20
Source File: UnsafeGenericObjectPool.java    From vividus with Apache License 2.0 4 votes vote down vote up
public UnsafeGenericObjectPool(PooledObjectFactory<T> factory)
{
    super(factory);
    setMaxWaitMillis(MAX_WAIT_MILLIS);
}
 
Example #21
Source File: JedisPoolAbstract.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public JedisPoolAbstract(GenericObjectPoolConfig poolConfig, PooledObjectFactory<Jedis> factory) {
  super(poolConfig, factory);
}
 
Example #22
Source File: Pool.java    From seppb with MIT License 4 votes vote down vote up
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
    initPool(poolConfig, factory);
}
 
Example #23
Source File: ElasticsearchClientPool.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
public ElasticsearchClientPool(PooledObjectFactory<RestHighLevelClient> factory) {
	super(factory);
}
 
Example #24
Source File: ElasticsearchClientPool.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
public ElasticsearchClientPool(PooledObjectFactory<RestHighLevelClient> factory,
		GenericObjectPoolConfig<RestHighLevelClient> config) {
	super(factory, config);
}
 
Example #25
Source File: ElasticsearchClientPool.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
public ElasticsearchClientPool(PooledObjectFactory<RestHighLevelClient> factory,
		GenericObjectPoolConfig<RestHighLevelClient> config, AbandonedConfig abandonedConfig) {
	super(factory, config, abandonedConfig);
}
 
Example #26
Source File: Pool.java    From elasticsearch-pool with Apache License 2.0 4 votes vote down vote up
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory){
    initPool(poolConfig, factory);
}
 
Example #27
Source File: Pool.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
  initPool(poolConfig, factory);
}
 
Example #28
Source File: UpdateEventCacheFactory.java    From game-executor with Apache License 2.0 4 votes vote down vote up
public UpdateEventCacheFactory(PooledObjectFactory<UpdateEvent> factory, GenericObjectPoolConfig config) {
    super(factory, config);
}
 
Example #29
Source File: FTPClientPool.java    From hsweb-framework with Apache License 2.0 4 votes vote down vote up
public FTPClientPool(PooledObjectFactory<FTPClient> factory) {
    super(factory);
}
 
Example #30
Source File: FTPClientPool.java    From hsweb-framework with Apache License 2.0 4 votes vote down vote up
public FTPClientPool(PooledObjectFactory<FTPClient> factory, GenericObjectPoolConfig config) {
    super(factory, config);
}