In this post under Apache Pool, I will explain with example the purpose of “getCreatedCount” method.
This method is mainly used when generating statistics of a particular object pool.
This method returns the total number of objects created during the lifetime of the pool.
So for example, if we have pool running for 1 hour and during that time we requested 10 objects from the empty pool, it will create 10 objects, and the result of “getCreatedCount” method will be 10.
Below is the main class showing its usage.
Main class
package package14;
import org.apache.commons.pool2.impl.GenericObjectPool;
public class StringBufferPoolExample14 {
public static void main(String[] args) {
GenericObjectPool<StringBuffer> genericObjectPool = new GenericObjectPool<StringBuffer>(new StringBufferPooledObjectFactory());
StringBuffer stringBuffer1 = null, stringBuffer2 = null;
try {
stringBuffer1 = genericObjectPool.borrowObject();
stringBuffer2 = genericObjectPool.borrowObject();
System.out.println("Created Count: " + genericObjectPool.getCreatedCount());
} catch(Exception exception) {
exception.printStackTrace();
} finally {
if(stringBuffer1 != null) {
genericObjectPool.returnObject(stringBuffer1);
}
if(stringBuffer2 != null) {
genericObjectPool.returnObject(stringBuffer2);
}
genericObjectPool.close();
}
}
}
In the above code, we start with an empty pool and borrow 2 objects.
Since the object pool is empty it will create two objects.
So the line at 15, will print 2 to the console.
In this way we can use “getCreatedCount” to gather statistics of a pool