BaseKeyedPooledObjectFactory example

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

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

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

ThreadKeyedPoolObjectFactory


package package3;

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

public class ThreadKeyedPoolObjectFactory implements KeyedPooledObjectFactory<String, Thread> {
    private int normalThreadObjectCount = 0;
    private int advancedThreadObjectCount = 0;
    public static enum keys {NORMAL, ADVANCED};

    @Override
    public void activateObject(String key, PooledObject<Thread> p) throws Exception {
    }

    @Override
    public void destroyObject(String key, PooledObject<Thread> p) throws Exception {
    }

    @Override
    public PooledObject<Thread> makeObject(String key) throws Exception {
        keys enumKey = keys.valueOf(key);
        String threadName = (keys.NORMAL.equals(enumKey)) ? keys.NORMAL.name() : keys.ADVANCED.name();
        int currentTheadObjectCount = 0;

        if(keys.NORMAL.equals(enumKey)) {
            normalThreadObjectCount = normalThreadObjectCount + 1;
            currentTheadObjectCount = normalThreadObjectCount;
        } else {
            advancedThreadObjectCount = advancedThreadObjectCount + 1;
            currentTheadObjectCount = advancedThreadObjectCount;
        }

        threadName = threadName + currentTheadObjectCount;
        Thread thread = new Thread(threadName);
        DefaultPooledObject<Thread> defaultPooledObject = new DefaultPooledObject<Thread>(thread);
        return defaultPooledObject;
    }

    @Override
    public void passivateObject(String key, PooledObject<Thread> p) throws Exception {
    }

    @Override
    public boolean validateObject(String key, PooledObject<Thread> p) {
        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 “KeyedPooledObjectFactory” named “BaseKeyedPooledObjectFactory” 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 ThreadKeyedPoolObjectFactory


1  package package4;
2  
3  import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
4  import org.apache.commons.pool2.PooledObject;
5  import org.apache.commons.pool2.impl.DefaultPooledObject;
6  
7  public class ThreadKeyedPoolObjectFactory extends BaseKeyedPooledObjectFactory<String, Thread> {
8      private int normalThreadObjectCount = 0;
9      private int advancedThreadObjectCount = 0;
10     public static enum keys {NORMAL, ADVANCED};
11     
12     @Override
13     public Thread create(String key) throws Exception {
14         keys enumKey = keys.valueOf(key);
15         String threadName = (keys.NORMAL.equals(enumKey)) ? keys.NORMAL.name() : keys.ADVANCED.name();
16         int currentTheadObjectCount = 0;
17         
18         if(keys.NORMAL.equals(enumKey)) {
19             normalThreadObjectCount = normalThreadObjectCount + 1;
20             currentTheadObjectCount = normalThreadObjectCount;
21         } else {
22             advancedThreadObjectCount = advancedThreadObjectCount + 1;
23             currentTheadObjectCount = advancedThreadObjectCount;
24         }
25         
26         threadName = threadName + currentTheadObjectCount;
27         return new Thread(threadName);
28     }
29 
30     @Override
31     public PooledObject<Thread> wrap(Thread thread) {
32         DefaultPooledObject<Thread> defaultPooledObject = new DefaultPooledObject<Thread>(thread);
33         return defaultPooledObject;
34     }
35 }

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 “ThreadKeyedPoolObjectFactory” code but with less code.

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

Main Class


package package4;

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

public class ThreadPoolExample4 {
public static void main(String[] args) {
GenericKeyedObjectPool<String, Thread> genericKeyedObjectPool = new GenericKeyedObjectPool<String, Thread>(new ThreadKeyedPoolObjectFactory());


Thread normalThread1 = null, normalThread2 = null;
Thread advancedThread = null;
try {
normalThread1 = genericKeyedObjectPool.borrowObject(ThreadKeyedPoolObjectFactory.keys.NORMAL.name());
System.out.println(normalThread1);
advancedThread = genericKeyedObjectPool.borrowObject(ThreadKeyedPoolObjectFactory.keys.ADVANCED.name());
System.out.println(advancedThread);
normalThread2 = genericKeyedObjectPool.borrowObject(ThreadKeyedPoolObjectFactory.keys.NORMAL.name());
System.out.println(normalThread2);
} catch(Exception exception) {
exception.printStackTrace();
} finally {
if(normalThread1 != null) {
genericKeyedObjectPool.returnObject(ThreadKeyedPoolObjectFactory.keys.NORMAL.name(), normalThread1);
}

if(normalThread2 != null) {
genericKeyedObjectPool.returnObject(ThreadKeyedPoolObjectFactory.keys.NORMAL.name(), normalThread2);
}

if(advancedThread != null) {
genericKeyedObjectPool.returnObject(ThreadKeyedPoolObjectFactory.keys.ADVANCED.name(), advancedThread);
}
genericKeyedObjectPool.close();
}
}
}

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

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

Leave a Reply