Search in sources :

Example 51 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project micrometer by micrometer-metrics.

the class CommonsObjectPool2MetricsTest method createGenericObjectPool.

private GenericObjectPool<Object> createGenericObjectPool() {
    genericObjectPoolCount++;
    GenericObjectPoolConfig<Object> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(10);
    return new GenericObjectPool<>(new BasePooledObjectFactory<Object>() {

        @Override
        public Object create() {
            return new Object();
        }

        @Override
        public PooledObject<Object> wrap(Object testObject) {
            return new DefaultPooledObject<>(testObject);
        }
    }, config);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 52 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project x-pipe by ctripcorp.

the class XpipeNettyClientPool method doInitialize.

@Override
protected void doInitialize() throws Exception {
    if (factory == null) {
        NettyClientFactory factory = new NettyClientFactory(target, false);
        factory.start();
        this.factory = factory;
    }
    GenericObjectPool<NettyClient> genericObjectPool = new GenericObjectPool<>(factory, config);
    genericObjectPool.setTestOnBorrow(true);
    genericObjectPool.setTestOnCreate(true);
    this.objectPool = genericObjectPool;
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) NettyClientFactory(com.ctrip.xpipe.netty.commands.NettyClientFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 53 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project jooby by jooby-project.

the class RedisModule method install.

@Override
public void install(@Nonnull Jooby application) throws Exception {
    if (uri == null) {
        Config config = application.getConfig();
        uri = Stream.of(name + ".uri", name).filter(config::hasPath).map(config::getString).map(RedisURI::create).findFirst().orElseThrow(() -> new IllegalStateException("Redis uri missing from application configuration: " + name));
    }
    RedisClient client = RedisClient.create(uri);
    StatefulRedisConnection<String, String> connection = client.connect();
    StatefulRedisPubSubConnection<String, String> connectPubSub = client.connectPubSub();
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(() -> client.connect(), poolConfig);
    // Close client and connection on shutdown
    application.onStop(pool::close);
    application.onStop(connection::close);
    application.onStop(connectPubSub::close);
    application.onStop(client::shutdown);
    ServiceRegistry registry = application.getServices();
    registry.putIfAbsent(ServiceKey.key(RedisClient.class), client);
    registry.put(ServiceKey.key(RedisClient.class, name), client);
    registry.putIfAbsent(ServiceKey.key(StatefulRedisConnection.class), connection);
    registry.put(ServiceKey.key(StatefulRedisConnection.class, name), connection);
    registry.putIfAbsent(ServiceKey.key(GenericObjectPool.class), pool);
    registry.put(ServiceKey.key(GenericObjectPool.class, name), pool);
    registry.putIfAbsent(ServiceKey.key(StatefulRedisPubSubConnection.class), connectPubSub);
    registry.put(ServiceKey.key(StatefulRedisPubSubConnection.class, name), connectPubSub);
}
Also used : Config(com.typesafe.config.Config) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) RedisClient(io.lettuce.core.RedisClient) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ServiceRegistry(io.jooby.ServiceRegistry) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection)

Example 54 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project cdap by caskdata.

the class PostgreSqlStorageProvider method createDataSource.

/**
 * Creates a {@link DataSource} for the sql implementation to use. It optionally loads an external JDBC driver
 * to use with JDBC.
 */
@VisibleForTesting
public static DataSource createDataSource(CConfiguration cConf, SConfiguration sConf, MetricsCollectionService metricsCollectionService) {
    String storageImpl = cConf.get(Constants.Dataset.DATA_STORAGE_IMPLEMENTATION);
    if (!storageImpl.equals(Constants.Dataset.DATA_STORAGE_SQL)) {
        throw new IllegalArgumentException(String.format("The storage implementation is not %s, cannot create the " + "DataSource", Constants.Dataset.DATA_STORAGE_SQL));
    }
    if (cConf.getBoolean(Constants.Dataset.DATA_STORAGE_SQL_DRIVER_EXTERNAL)) {
        loadJDBCDriver(cConf, storageImpl);
    }
    String jdbcUrl = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_JDBC_CONNECTION_URL);
    if (jdbcUrl == null) {
        throw new IllegalArgumentException("The jdbc connection url is not specified.");
    }
    Properties properties = retrieveJDBCConnectionProperties(cConf, sConf);
    LOG.info("Creating the DataSource with jdbc url: {}", jdbcUrl);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, properties);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // The GenericObjectPool is thread safe according to the javadoc,
    // the PoolingDataSource will be thread safe as long as the connectin pool is thread-safe
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    connectionPool.setMaxTotal(cConf.getInt(Constants.Dataset.DATA_STORAGE_SQL_CONNECTION_SIZE));
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
    return new MetricsDataSource(dataSource, metricsCollectionService, connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) MetricsDataSource(io.cdap.cdap.spi.data.sql.jdbc.MetricsDataSource) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 55 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project blog-example by my-dlq.

the class Application method main.

public static void main(String[] args) {
    // =====================创建线程池=====================
    ExecutorService excutor = Executors.newFixedThreadPool(10);
    // =====================创建对象池=====================
    // 对象池工厂
    PersonPoolFactory personPoolFactory = new PersonPoolFactory();
    // 对象池配置
    GenericObjectPoolConfig<Person> objectPoolConfig = new GenericObjectPoolConfig<>();
    objectPoolConfig.setMaxTotal(5);
    // 对象池
    GenericObjectPool<Person> personPool = new GenericObjectPool<>(personPoolFactory, objectPoolConfig);
    // 循环100次,从线程池中取多个多线程执行任务,来测试对象池
    for (int i = 0; i < 15; i++) {
        excutor.submit(new Thread(() -> {
            // 模拟从对象池取出对象,执行任务
            Person person = null;
            try {
                // 从对象池取出对象
                person = personPool.borrowObject();
                // 让对象工作
                person.executeTask();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 回收对象到对象池
                if (person != null) {
                    personPool.returnObject(person);
                }
            }
        }));
    }
}
Also used : PersonPoolFactory(club.mydlq.pool.PersonPoolFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ExecutorService(java.util.concurrent.ExecutorService) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Person(club.mydlq.pool.Person)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)79 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)32 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)27 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)23 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)19 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)16 Test (org.junit.jupiter.api.Test)13 Properties (java.util.Properties)11 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)9 SQLException (java.sql.SQLException)8 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)8 Connection (java.sql.Connection)7 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Bean (org.springframework.context.annotation.Bean)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 IOException (java.io.IOException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 Test (org.junit.Test)3 TimeInterval (com.adaptris.util.TimeInterval)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)2