I've added commons-pooling-1.6.jar to my classpath and trying to instantiate a StackObjectPool
and am failing at every turn:
// Deprecated.
ObjectPool<T> oPool = new StackObjectPool<T>();
// Error: Cannot instantiate the type BasePoolableObjectFactory<T>.
PoolableObjectFactory<T> oFact = new BasePoolableObjectFactory<T>();
ObjectPool<T> oPool = new StackObjectPool<T>(oFact);
Is this a deprecated API altogether? If so, what are some open source alternatives to Commons Pooling? Else, how do I instantiate a StackObjectPool
?
You need to write your own Factory possibly extending BasePoolableObjectFactory. See here for more information: http://commons.apache.org/pool/examples.html
Below is a PoolableObjectFactory implementation that creates StringBuffers:
import org.apache.commons.pool.BasePoolableObjectFactory;
public class StringBufferFactory extends BasePoolableObjectFactory<StringBuffer> {
// for makeObject we'll simply return a new buffer
public StringBuffer makeObject() {
return new StringBuffer();
}
// when an object is returned to the pool,
// we'll clear it out
public void passivateObject(StringBuffer buf) {
buf.setLength(0);
}
// for all other methods, the no-op
// implementation in BasePoolableObjectFactory
// will suffice
}
Then use it as follows:
new StackObjectPool<StringBuffer>(new StringBufferFactory())