use of org.apache.commons.pool.impl.GenericKeyedObjectPool in project jmxtrans by jmxtrans.
the class JmxTransModule method getObjectPool.
private <K, V> GenericKeyedObjectPool<K, V> getObjectPool(KeyedPoolableObjectFactory<K, V> factory, String poolName) {
GenericKeyedObjectPool<K, V> pool = new GenericKeyedObjectPool<>(factory);
pool.setTestOnBorrow(true);
pool.setMaxActive(-1);
pool.setMaxIdle(-1);
pool.setTimeBetweenEvictionRunsMillis(MILLISECONDS.convert(5, MINUTES));
pool.setMinEvictableIdleTimeMillis(MILLISECONDS.convert(5, MINUTES));
try {
ManagedGenericKeyedObjectPool mbean = new ManagedGenericKeyedObjectPool(pool, poolName);
ManagementFactory.getPlatformMBeanServer().registerMBean(mbean, mbean.getObjectName());
} catch (Exception e) {
log.error("Could not register mbean for pool [{}]", poolName, e);
}
return pool;
}
use of org.apache.commons.pool.impl.GenericKeyedObjectPool in project jmxtrans by jmxtrans.
the class JmxTransModule method getObjectPool.
private <K, V> GenericKeyedObjectPool<K, V> getObjectPool(KeyedPoolableObjectFactory<K, V> factory, String poolName, long maxWaitMillis) {
GenericKeyedObjectPool<K, V> pool = new GenericKeyedObjectPool<>(factory);
pool.setTestOnBorrow(true);
pool.setMaxActive(-1);
pool.setMaxIdle(-1);
pool.setTimeBetweenEvictionRunsMillis(MILLISECONDS.convert(5, MINUTES));
pool.setMinEvictableIdleTimeMillis(MILLISECONDS.convert(5, MINUTES));
pool.setMaxWait(maxWaitMillis);
pool.setTestOnReturn(true);
pool.setTestOnBorrow(true);
try {
ManagedGenericKeyedObjectPool mbean = new ManagedGenericKeyedObjectPool(pool, poolName);
ManagementFactory.getPlatformMBeanServer().registerMBean(mbean, mbean.getObjectName());
} catch (Exception e) {
log.error("Could not register mbean for pool [{}]", poolName, e);
}
return pool;
}
use of org.apache.commons.pool.impl.GenericKeyedObjectPool in project jmxtrans by jmxtrans.
the class StatsDWriter method start.
@Override
public void start() throws LifecycleException {
try {
pool = new GenericKeyedObjectPool<>(new DatagramSocketFactory());
pool.setTestOnBorrow(true);
pool.setMaxActive(-1);
pool.setMaxIdle(-1);
pool.setTimeBetweenEvictionRunsMillis(MILLISECONDS.convert(5, MINUTES));
pool.setMinEvictableIdleTimeMillis(MILLISECONDS.convert(5, MINUTES));
this.mbean = new ManagedGenericKeyedObjectPool((GenericKeyedObjectPool) pool, "StatsdConnectionPool");
ManagementFactory.getPlatformMBeanServer().registerMBean(this.mbean, this.mbean.getObjectName());
} catch (Exception e) {
throw new LifecycleException(e);
}
}
use of org.apache.commons.pool.impl.GenericKeyedObjectPool in project otter by alibaba.
the class CommunicationConnectionPoolFactory method initial.
public void initial() {
if (factory == null) {
throw new IllegalArgumentException("factory is null!");
}
// 创建链接池对象
pool = new GenericKeyedObjectPool();
pool.setMaxActive(maxActive);
pool.setMaxIdle(maxActive);
pool.setMinIdle(0);
// 60s
pool.setMaxWait(60 * 1000);
pool.setTestOnBorrow(false);
pool.setTestOnReturn(false);
pool.setTimeBetweenEvictionRunsMillis(10 * 1000);
pool.setNumTestsPerEvictionRun(maxActive * 2);
pool.setMinEvictableIdleTimeMillis(30 * 60 * 1000);
pool.setTestWhileIdle(true);
// 设置连接池管理对象
pool.setFactory(new CommunicationConnectionPoolableFactory(factory));
}
use of org.apache.commons.pool.impl.GenericKeyedObjectPool in project voldemort by voldemort.
the class ResourcePoolPerfTest method main.
public static void main(String[] args) throws Exception {
final int numKeys = 10;
final int numThreads = 10;
final int numRequests = 10000000;
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(2);
for (int poolSize : new int[] { 1, 5, 10 }) {
System.out.println("Perf test for voldemort pool with numThreads = " + numThreads + ", poolSize = " + poolSize + ":");
final KeyedResourcePool<Integer, String> pool = KeyedResourcePool.create(new StringResourceFactory(), new ResourcePoolConfig().setMaxPoolSize(poolSize).setIsFair(true));
PerformanceTest test = new PerformanceTest() {
@Override
public void doOperation(int id) throws Exception {
Integer key = id % numKeys;
String s = pool.checkout(key);
pool.checkin(key, s);
}
};
test.run(numRequests, numThreads);
test.printStats();
System.out.println();
}
System.out.println("--------------------------------------");
System.out.println();
for (int poolSize : new int[] { 1, 5, 10 }) {
System.out.println("Perf test for commons pool with numThreads = " + numThreads + ", poolSize = " + poolSize + ":");
GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
config.maxActive = poolSize;
config.testOnBorrow = true;
config.whenExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK;
config.maxWait = 10000;
StringPoolableObjectFactory objFactory = new StringPoolableObjectFactory();
final KeyedObjectPool pool = new GenericKeyedObjectPool(objFactory, config);
PerformanceTest test = new PerformanceTest() {
@Override
public void doOperation(int id) throws Exception {
Integer key = id % numKeys;
String s = (String) pool.borrowObject(key);
pool.returnObject(key, s);
}
};
test.run(numRequests, numThreads);
test.printStats();
System.out.println();
}
}
Aggregations