BasePooledObjectFactory class Example

In the previous post under Apache Pool, I showed you with example how to use PooledObjectFactory interface to create an object pool.

In this post I will show how to use “BasePooledObjectFactory” class provided Apache Pool framework.

We created our own implementation of PooledObjectFactory interface in the previous post as shown below. I have added the code from previous post for comparison.

StringBufferPooledObjectFactory


package package1;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

public class StringBufferPooledObjectFactory implements PooledObjectFactory<StringBuffer> {
    @Override
    public void activateObject(PooledObject<StringBuffer> pooledObject) throws Exception {
    }

    @Override
    public void destroyObject(PooledObject<StringBuffer> pooledObject) throws Exception {
    }

    @Override
    public PooledObject<StringBuffer> makeObject() throws Exception {
        DefaultPooledObject<StringBuffer> defaultPooledObject = new DefaultPooledObject<StringBuffer>(new StringBuffer());
        return defaultPooledObject;
    }

    @Override
    public void passivateObject(PooledObject<StringBuffer> pooledObject) throws Exception {
    }

    @Override
    public boolean validateObject(PooledObject<StringBuffer> pooledObject) {
        return true;
    }
}

In the above code, at minimum you have to provide implementation for “makeObject” and “validateObject” method. The remaining methods can remain empty.

Apache Pool framework comes with a default implementation of PooledObjectFactory named “BasePooledObjectFactory” class.

You can use this class or the interface. Its a design choice.

The above code can be rewritten using the class as shown below

Modified StringBufferPooledObjectFactory


1  package package2;
2  
3  import org.apache.commons.pool2.BasePooledObjectFactory;
4  import org.apache.commons.pool2.PooledObject;
5  import org.apache.commons.pool2.impl.DefaultPooledObject;
6  
7  public class StringBufferPooledObjectFactory extends BasePooledObjectFactory<StringBuffer> {
8      @Override
9      public StringBuffer create() throws Exception {
10         return new StringBuffer();
11     }
12 
13     @Override
14     public PooledObject<StringBuffer> wrap(StringBuffer stringBuffer) {
15         return new DefaultPooledObject<StringBuffer>(stringBuffer);
16     }
17 }

As you see in the above code, you need to provide implementation for “create” and “wrap” method.

The “create” method will create the instance of the object which you want to be available in the pool.

The “wrap” method will wrap each instance returned from “create” method with DefaultPooledObject instance.

It is similar to what I did in previous “StringBufferPooledObjectFactory” code but with less code.

Below is the complete main code which shows how to use the class.

Main class


package package2;

import org.apache.commons.pool2.impl.GenericObjectPool;

public class StringBufferPoolExample2 {
    public static void main(String[] args) {
        GenericObjectPool<StringBuffer> genericObjectPool = new GenericObjectPool<StringBuffer>(new StringBufferPooledObjectFactory());
        genericObjectPool.setMaxTotal(10);

        StringBuffer stringBuffer = null;
        try {
            stringBuffer = genericObjectPool.borrowObject();
            stringBuffer.append("Welcome To Apache Pool");
            System.out.println(stringBuffer.toString());
        } catch(Exception exception) {
            exception.printStackTrace();
        } finally {
            if(stringBuffer != null) {
                genericObjectPool.returnObject(stringBuffer);
            }
            genericObjectPool.close();
        }
    }
}

The above code is exact copy of Main class code from previous post.

In this way we can use the “BasePooledObjectFactory” class provided by Apache framework instead of using the “PooledObjectFactory” interface.

Leave a Reply