我想用 Apache Commons Pool 2 创建一个对象 P 池。我用变量和方法创建了一个 P 对象。
我创建一个这样的PPool:
public class PPool extends GenericObjectPool<P>{
/**
* Constructor.
*
* It uses the default configuration for pool provided by
* apache-commons-pool2.
*
* @param factory
*/
public PPool(PooledObjectFactory<P> factory) {
super(factory);
}
/**
*
*
* @param factory
* @param config
*/
public PPool(PooledObjectFactory<P> factory,
GenericObjectPoolConfig config) {
super(factory, config);
}
}
之后我创建了一个 PFactory:
public class PhantomJsFactory extends BasePooledObjectFactory<Phantom> {
@Override
public P create() throws Exception {
// TODO Auto-generated method stub
return new P();
}
@Override
public PooledObject<P> wrap(P phantomjs) {
// TODO Auto-generated method stub
return new DefaultPooledObject<P>(phantomjs);
}
}
现在,如果我想添加例如 10 个 P 对象实例,我该怎么做?我试试这个:
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxIdle(1);
config.setMaxTotal(10);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
pool = new PPool(new PFactory(), config);
但现在?