rpc.turbo.pool.BlazeObjectPool Maven / Gradle / Ivy
package rpc.turbo.pool;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import stormpot.BlazePool;
import stormpot.Config;
import stormpot.PoolException;
import stormpot.Timeout;
/**
* high performance objectpool
* https://github.com/chrisvest/object-pool-benchmarks
*
* @author Hank
*
* @param
*/
public class BlazeObjectPool implements Closeable {
private final BlazePool> blazePool;
private final Timeout timeout = new Timeout(1, TimeUnit.MINUTES);
public BlazeObjectPool(int poolSize, ObjectFactory allocator) {
Config> config = new Config<>();
config.setAllocator(allocator);
config.setSize(poolSize);
config.setExpiration(new NoneExpiration());
blazePool = new BlazePool<>(config);
}
public PoolableObject claim() throws PoolException, InterruptedException {
return blazePool.claim(timeout);
}
@Override
public void close() throws IOException {
try {
blazePool.shutdown().await(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}