Using the Jakarta Commons, Part 3
Pages: 1, 2, 3, 4
Pool
Summary: A library that allows you to maintain a pool of objects.
Where: Main Page, Binaries, Source.
When: Whenever you need to maintain a pool of object instances.
Example Applications: PoolDemo.java and MyObjectFactory.java require commons-pool.jar and commons-collections.jar in the CLASSPATH.
Description:
Pool serves the purpose of defining a set of interfaces for object-pooling mechanisms. It also provides some general-purpose pool implementations and some base classes that you could use to create your own pooling architecture.
Object pools may not be new to most developers. Many developers have, at some time or other, used a database pooling mechanism for access to databases. Object pools allow you to instantiate a set of objects within a pool as part of configuration and startup, allowing for shorter response times when these objects are actually needed within your application. These objects can be returned to the pool once they've been used, remaining available to any other calling application.
The Pool component allows you to create Object (Instance) pools without tying you to one particular implementation. Several implementations are provided with the component, and you can create your own, if so required.
Three types of base classes make up the Pool component:
ObjectPool, an interface that defines and maintains a pool,
ObjectPoolFactory, responsible for creating instances of
ObjectPool, and PoolableObjectFactory, which defines
a set of lifecycle methods for instances to be used in the
ObjectPool. A variation on these classes is the
KeyedXXX interface. This interface allows you to create several
object pools for different types of objects, only differentiated with the help
of a Key. In a way, the KeyedXXX interface is a
Map implementation of the normal ObjectPool.
As stated before, there are several generic implementations prebuilt within
the Pool component. One of these is the GenericObjectPool, and I
will illustrate its usage with the help of an example.
Create a
PoolableObjectFactory. This factory defines how your objects are created, destroyed, and validated.import org.apache.commons.pool.PoolableObjectFactory; public class MyObjectFactory implements PoolableObjectFactory { private static int counter; // returns a new string public Object makeObject() { return String.valueOf(counter++); } public void destroyObject(Object obj) {} public boolean validateObject(Object obj) { return true; } public void activateObject(Object obj) {} public void passivateObject(Object obj) {} }Notice that we are creating a pool of
Stringobjects with an incrementing number, and that our validations always return true. You can implement the other methods, if required.Using this PoolableObjectFactory, create a
GenericObjectPool, using default values formaxActive,maxIdle, and so on.// create a GenericObjectPool using MyObject factory and default // values for the maxActive, maxIdle etc. GenericObjectPool pool = new GenericObjectPool(new MyObjectFactory());Borrow an object from it.
System.err.println("Borrowed: " + pool.borrowObject());Return it.
pool.returnObject("0");
You can use various methods to find the state of the pool.
// so what's the number of active (borrowed objects) ?
System.err.println("Active objects: " + pool.getNumActive());
The example file PoolDemo.java contains the full source code.