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);
}
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;
}
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);
}
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);
}
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);
}
}
}));
}
}
Aggregations