4

I want to create a pool of Object P with Apache Commons Pool 2. I create a P object with variable and method.

I create a PPool like this:

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

And after that I create a 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);
    }

}

Now if I want to add, for example, 10 instances of P object how do I do that? I try with this:

        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxIdle(1);
        config.setMaxTotal(10);


        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        pool = new PPool(new PFactory(), config);

but now?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
DevOps85
  • 6,473
  • 6
  • 23
  • 42

1 Answers1

3

You don't need to add the instances. You supply the Factory of the P class. So the Pool will manage object lifecycle. If you obtain object, it will be created by the Pool if it is required. Therefore just borrow the object to use.

P pObject = pool.borrowObject();

See description GenericObjectPool.html#borrowObject()

Please look at some test cases: TestGenericObjectPool.java

Luciano
  • 2,695
  • 6
  • 38
  • 53
Chk0nDanger
  • 1,211
  • 1
  • 10
  • 26
  • but if i want for example 5 instance alway in execution? so when i borrow the object i have this 5 instance just ready – DevOps85 Nov 12 '14 at 09:09
  • I think you can set minIdle to 5. then I think the pool will keep 5 instances in idle. I think you can borrow objects before to use in initialisation of your programme. `for (int i=0,l=config.getMinIdle(); i – Chk0nDanger Nov 12 '14 at 09:31
  • See the testcase: https://apache.googlesource.com/commons-pool/+/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java find testMinIdle – Chk0nDanger Nov 12 '14 at 09:49
  • Note that if you want to create objects automatically when there are no idle objects left you can call [BaseGenericObjectPool#setBlockWhenExhausted()](http://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/BaseGenericObjectPool.html#setBlockWhenExhausted-boolean-) – SiXoS May 17 '16 at 13:35
  • Maybe off topic, but how can I initialise pool with pre-created objects. I have a list of available addresses and each thread borrows one address for its work... – kensai Jan 28 '18 at 14:16