Creating Object Pool

In this post under Apache Pool, I will show with simple example how to create a pool of objects.

We need the below jar in our classpath to use Apache Pool framework

We need to know about 3 Apache Pool interface
1) ObjectPool –> manages pool of objects
2) PooledObjectFactory –> takes responsibility of creating and destroying the objects.
3) PooledObject –> it is wrapper around the objects which are pooled. This wrapper stores statistics information

For our example, we will create a pool of StringBuffer objects.

We will create a factory class which implements PooledObjectFactory interface as shown below


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> {
    public void activateObject(PooledObject<StringBuffer> pooledObject) throws Exception {

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

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

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

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

In the above code, at minimum we need to provide implementations for “makeObject” and “validateObject” method.

In the “makeObject” method, we create StringBuffer instance and pass it as constructor argument to DefaultPooledObject instance. At the end we return the instance of DefaultPooledObject. DefaultPooledObject is an implementation of PooledObject interface.

Next I will show the main code.

Main Code

1  package package1;
3  import org.apache.commons.pool2.impl.GenericObjectPool;
5  public class StringBufferPoolExample1 {
6      public static void main(String[] args) {
7          GenericObjectPool<StringBuffer> genericObjectPool = new GenericObjectPool<StringBuffer>(new StringBufferPooledObjectFactory());
8          genericObjectPool.setMaxTotal(10);
10         StringBuffer stringBuffer = null;
11         try {
12             stringBuffer = genericObjectPool.borrowObject();
13             stringBuffer.append("Welcome To Apache Pool");
14             System.out.println(stringBuffer.toString());
15         } catch(Exception exception) {
16             exception.printStackTrace();
17         } finally {
18             if(stringBuffer != null) {
19                 genericObjectPool.returnObject(stringBuffer);
20             }
21             genericObjectPool.close();
22         }
23     }
24 }

In the above code, at line 7, I create an instance of GenericObjectPool. We pass an instance of StringBufferPooledObjectFactory class as constructor argument to GenericObjectPool.

At line 8, we set the maximum number of objects in the pool to 10.

At line 12, when we need an instance of StringBuffer class from the pool, we call “borrowObject” method of GenericObjectPool class. This method checks if there is a pre-existing instance of StringBuffer free, if yes it will return that or create a new instance making sure that the number of instances doesn’t go beyond 10.

Once we are done with StringBuffer instance, we need to return it back to the pool by calling “returnObject” method of GenericObjectPool class. Refer to line 19.

In this way, we can create an pool of objects of desired class and access it.

Leave a Reply