org.apache.commons.pool2.KeyedPooledObjectFactory Java Examples

The following examples show how to use org.apache.commons.pool2.KeyedPooledObjectFactory. 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: TestGenericKeyedObjectPool.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
@Override
protected KeyedObjectPool<Object,Object> makeEmptyPool(final int minCapacity) {
    final KeyedPooledObjectFactory<Object,Object> perKeyFactory =
            new SimplePerKeyFactory();
    final GenericKeyedObjectPool<Object,Object> perKeyPool =
        new GenericKeyedObjectPool<>(perKeyFactory);
    perKeyPool.setMaxTotalPerKey(minCapacity);
    perKeyPool.setMaxIdlePerKey(minCapacity);
    return perKeyPool;
}
 
Example #2
Source File: BaseTestProxiedKeyedObjectPool.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    final GenericKeyedObjectPoolConfig<TestObject> config = new GenericKeyedObjectPoolConfig<>();
    config.setMaxTotal(3);

    final KeyedPooledObjectFactory<String, TestObject> factory =
            new TestKeyedObjectFactory();

    @SuppressWarnings("resource")
    final KeyedObjectPool<String, TestObject> innerPool =
            new GenericKeyedObjectPool<>(
                    factory, config);

    pool = new ProxiedKeyedObjectPool<>(innerPool, getproxySource());
}
 
Example #3
Source File: GenericKeyedObjectPool.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@code GenericKeyedObjectPool} using a specific
 * configuration.
 *
 * @param factory the factory to be used to create entries
 * @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 GenericKeyedObjectPool(final KeyedPooledObjectFactory<K, T> factory,
        final GenericKeyedObjectPoolConfig<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;
    this.fairness = config.getFairness();

    setConfig(config);
}
 
Example #4
Source File: PoolConfiguration.java    From spring-thrift-starter with MIT License 5 votes vote down vote up
private KeyedPooledObjectFactory<ThriftClientKey, TServiceClient> thriftClientPoolFactory() {
    return ThriftClientPooledObjectFactory
            .builder()
            .protocolFactory(protocolFactory)
            .propertyResolver(propertyResolver)
            .loadBalancerClient(loadBalancerClient)
            .tracing(tracing)
            .tracer(tracer)
            .build();
}
 
Example #5
Source File: ThriftClientPool.java    From spring-thrift-starter with MIT License 4 votes vote down vote up
public ThriftClientPool(KeyedPooledObjectFactory<ThriftClientKey, TServiceClient> factory, GenericKeyedObjectPoolConfig config) {
    super(factory, config);
}
 
Example #6
Source File: TestGenericKeyedObjectPool.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 maxTotalPerKey = 1;
    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 KeyedPooledObjectFactory<Object, Object> dummyFactory = new DummyFactory();

    try (GenericKeyedObjectPool<Object, Object> objPool = new GenericKeyedObjectPool<>(dummyFactory)) {
        assertEquals(GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY, objPool.getMaxTotalPerKey());
        assertEquals(GenericKeyedObjectPoolConfig.DEFAULT_MAX_IDLE_PER_KEY, objPool.getMaxIdlePerKey());
        assertEquals(BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS, objPool.getMaxWaitMillis());
        assertEquals(GenericKeyedObjectPoolConfig.DEFAULT_MIN_IDLE_PER_KEY, objPool.getMinIdlePerKey());
        assertEquals(GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL, objPool.getMaxTotal());
        assertEquals(BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
                objPool.getMinEvictableIdleTimeMillis());
        assertEquals(BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, objPool.getNumTestsPerEvictionRun());
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW),
                Boolean.valueOf(objPool.getTestOnBorrow()));
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_ON_RETURN),
                Boolean.valueOf(objPool.getTestOnReturn()));
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE),
                Boolean.valueOf(objPool.getTestWhileIdle()));
        assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
                objPool.getTimeBetweenEvictionRunsMillis());
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED),
                Boolean.valueOf(objPool.getBlockWhenExhausted()));
        assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_LIFO), Boolean.valueOf(objPool.getLifo()));
    }

    final GenericKeyedObjectPoolConfig<Object> config = new GenericKeyedObjectPoolConfig<>();
    config.setLifo(lifo);
    config.setMaxTotalPerKey(maxTotalPerKey);
    config.setMaxIdlePerKey(maxIdle);
    config.setMinIdlePerKey(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 (GenericKeyedObjectPool<Object, Object> objPool = new GenericKeyedObjectPool<>(dummyFactory, config)) {
        assertEquals(maxTotalPerKey, objPool.getMaxTotalPerKey());
        assertEquals(maxIdle, objPool.getMaxIdlePerKey());
        assertEquals(maxWait, objPool.getMaxWaitMillis());
        assertEquals(minIdle, objPool.getMinIdlePerKey());
        assertEquals(maxTotal, objPool.getMaxTotal());
        assertEquals(minEvictableIdleTimeMillis, objPool.getMinEvictableIdleTimeMillis());
        assertEquals(numTestsPerEvictionRun, objPool.getNumTestsPerEvictionRun());
        assertEquals(Boolean.valueOf(testOnBorrow), Boolean.valueOf(objPool.getTestOnBorrow()));
        assertEquals(Boolean.valueOf(testOnReturn), Boolean.valueOf(objPool.getTestOnReturn()));
        assertEquals(Boolean.valueOf(testWhileIdle), Boolean.valueOf(objPool.getTestWhileIdle()));
        assertEquals(timeBetweenEvictionRunsMillis, objPool.getTimeBetweenEvictionRunsMillis());
        assertEquals(Boolean.valueOf(blockWhenExhausted), Boolean.valueOf(objPool.getBlockWhenExhausted()));
        assertEquals(Boolean.valueOf(lifo), Boolean.valueOf(objPool.getLifo()));
    }
}
 
Example #7
Source File: TestGenericKeyedObjectPool.java    From commons-pool with Apache License 2.0 4 votes vote down vote up
@Override
protected KeyedObjectPool<Object,Object> makeEmptyPool(final KeyedPooledObjectFactory<Object,Object> fac) {
    return new GenericKeyedObjectPool<>(fac);
}
 
Example #8
Source File: JmsPoolConnectionFactory.java    From pooled-jms with Apache License 2.0 4 votes vote down vote up
public void initConnectionsPool() {
    if (this.connectionsPool == null) {
        final GenericKeyedObjectPoolConfig<PooledConnection> poolConfig = new GenericKeyedObjectPoolConfig<>();
        poolConfig.setJmxEnabled(false);
        this.connectionsPool = new GenericKeyedObjectPool<PooledConnectionKey, PooledConnection>(
            new KeyedPooledObjectFactory<PooledConnectionKey, PooledConnection>() {
                @Override
                public PooledObject<PooledConnection> makeObject(PooledConnectionKey connectionKey) throws Exception {
                    Connection delegate = createProviderConnection(connectionKey);

                    PooledConnection connection = createPooledConnection(delegate);
                    connection.setIdleTimeout(getConnectionIdleTimeout());
                    connection.setMaxSessionsPerConnection(getMaxSessionsPerConnection());
                    connection.setBlockIfSessionPoolIsFull(isBlockIfSessionPoolIsFull());
                    if (isBlockIfSessionPoolIsFull() && getBlockIfSessionPoolIsFullTimeout() > 0) {
                        connection.setBlockIfSessionPoolIsFullTimeout(getBlockIfSessionPoolIsFullTimeout());
                    }
                    connection.setUseAnonymousProducers(isUseAnonymousProducers());
                    connection.setExplicitProducerCacheSize(getExplicitProducerCacheSize());

                    LOG.trace("Created new connection: {}", connection);

                    JmsPoolConnectionFactory.this.mostRecentlyCreated.set(connection);

                    return new DefaultPooledObject<PooledConnection>(connection);
                }

                @Override
                public void destroyObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) throws Exception {
                    PooledConnection connection = pooledObject.getObject();
                    try {
                        LOG.trace("Destroying connection: {}", connection);
                        connection.close();
                    } catch (Exception e) {
                        LOG.warn("Close connection failed for connection: " + connection + ". This exception will be ignored.",e);
                    }
                }

                @Override
                public boolean validateObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) {
                    PooledConnection connection = pooledObject.getObject();
                    if (connection != null && connection.expiredCheck()) {
                        LOG.trace("Connection has expired: {} and will be destroyed", connection);
                        return false;
                    }

                    return true;
                }

                @Override
                public void activateObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) throws Exception {
                }

                @Override
                public void passivateObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) throws Exception {
                }

            }, poolConfig);

        // Set max idle (not max active) since our connections always idle in the pool.
        this.connectionsPool.setMaxIdlePerKey(DEFAULT_MAX_CONNECTIONS);
        this.connectionsPool.setLifo(false);
        this.connectionsPool.setMinIdlePerKey(1);
        this.connectionsPool.setBlockWhenExhausted(false);

        // We always want our validate method to control when idle objects are evicted.
        this.connectionsPool.setTestOnBorrow(true);
        this.connectionsPool.setTestWhileIdle(true);
    }
}
 
Example #9
Source File: ConnectionPool.java    From fastdfs-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * 默认构造函数
 */
public ConnectionPool(KeyedPooledObjectFactory<InetSocketAddress, Connection> factory) {
    super(factory);
}
 
Example #10
Source File: ConnectionPool.java    From fastdfs-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * 默认构造函数
 */
public ConnectionPool(KeyedPooledObjectFactory<InetSocketAddress, Connection> factory, GenericKeyedObjectPoolConfig config) {
    super(factory, config);
}
 
Example #11
Source File: DefaultThriftConnectionPool.java    From ThriftJ with Apache License 2.0 4 votes vote down vote up
public DefaultThriftConnectionPool(KeyedPooledObjectFactory<ThriftServer, TTransport> factory, GenericKeyedObjectPoolConfig config) {
	connections = new GenericKeyedObjectPool<>(factory, config);
}
 
Example #12
Source File: FdfsConnectionPool.java    From FastDFS_Client with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 默认构造函数
 *
 * @param factory
 */
public FdfsConnectionPool(KeyedPooledObjectFactory<InetSocketAddress, Connection> factory) {
    super(factory);
}
 
Example #13
Source File: FdfsConnectionPool.java    From FastDFS_Client with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 默认构造函数
 *
 * @param factory
 * @param config
 */
@Autowired
public FdfsConnectionPool(KeyedPooledObjectFactory<InetSocketAddress, Connection> factory,
                          GenericKeyedObjectPoolConfig config) {
    super(factory, config);
}
 
Example #14
Source File: GenericKeyedObjectPool.java    From commons-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new {@code GenericKeyedObjectPool} using defaults from
 * {@link GenericKeyedObjectPoolConfig}.
 * @param factory the factory to be used to create entries
 */
public GenericKeyedObjectPool(final KeyedPooledObjectFactory<K, T> factory) {
    this(factory, new GenericKeyedObjectPoolConfig<T>());
}
 
Example #15
Source File: GenericKeyedObjectPool.java    From commons-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Obtain a reference to the factory used to create, destroy and validate
 * the objects used by this pool.
 *
 * @return the factory
 */
public KeyedPooledObjectFactory<K, T> getFactory() {
    return factory;
}
 
Example #16
Source File: FdfsConnectionPool.java    From FastDFS_Client with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 默认构造函数
 *
 * @param factory
 */
public FdfsConnectionPool(KeyedPooledObjectFactory<InetSocketAddress, Connection> factory) {
    super(factory);
}
 
Example #17
Source File: FdfsConnectionPool.java    From FastDFS_Client with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 默认构造函数
 *
 * @param factory
 * @param config
 */
@Autowired
public FdfsConnectionPool(KeyedPooledObjectFactory<InetSocketAddress, Connection> factory,
                          GenericKeyedObjectPoolConfig config) {
    super(factory, config);
}