In this post under Apache Pool, I will explain with example the purpose of “BlockWhenExhausted” property of an Object Pool.
Lets say we have 3 idle objects in the object pool and the caller code retrieves all the 3 objects. As a result of which the pool will be empty i.e., no idle objects will be present. At that situation if the caller request for another idle objects, the call is blocked indefinitely. This is the default behavior of Apache Pool.
We can change this default behavior by changing the value of “BlockWhenExhausted” boolean property to “false” by calling the “setBlockWhenExhausted” on “GenericObjectPoolConfig” instance or other pool config classes.
Once the default behavior is changed and you request more than the available idle objects you get an exception, the code doesn’t get blocked indefinitely.
Below is the complete code for your reference.
ThreadPooledObjectFactory
package package10;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
public class ThreadPooledObjectFactory extends BasePooledObjectFactory<Thread> {
private int count = 0;
@Override
public Thread create() throws Exception {
String threadName = "Thread" + count;
count = count + 1;
return new Thread(threadName);
}
@Override
public PooledObject<Thread> wrap(Thread obj) {
return new DefaultPooledObject<Thread>(obj);
}
}
Main class
1 package package10;
2
3 import org.apache.commons.pool2.impl.GenericObjectPool;
4 import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
5
6 public class Example10 {
7 public static void main(String[] args) throws Exception {
8 GenericObjectPoolConfig<Thread> genericObjectPoolConfig = new GenericObjectPoolConfig<>();
9 genericObjectPoolConfig.setMaxTotal(3);
10 genericObjectPoolConfig.setBlockWhenExhausted(false);
11 ThreadPooledObjectFactory threadPooledObjectFactory = new ThreadPooledObjectFactory();
12 GenericObjectPool<Thread> genericObjectPool = new GenericObjectPool<>(threadPooledObjectFactory, genericObjectPoolConfig);
13
14 try {
15 Thread thread1 = genericObjectPool.borrowObject();
16 Thread thread2 = genericObjectPool.borrowObject();
17 Thread thread3 = genericObjectPool.borrowObject();
18 Thread thread4 = genericObjectPool.borrowObject();
19 } catch(Exception exception) {
20 exception.printStackTrace();
21 }
22 }
23 }
As shown in the above code, at line 8, 9, and 10 I created an instance of “GenericObjectPoolConfig” configured it to have 3 max items and set “BlockWhenExhausted” property to false by calling “setBlockWhenExhausted” on “GenericObjectPoolConfig” instance.
Next at line 12, we are using this “GenericObjectPoolConfig” instance to create an object pool.
As a result the new object pool will allow only 3 objects and throws an exception if we attempt to request more than the available idle objects.
Below is the output
Output
java.util.NoSuchElementException: Pool exhausted
at org.apache.commons.pool2@2.12.0/org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:320)
at org.apache.commons.pool2@2.12.0/org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:233)
at ApachePoolConcepts.main/package10.Example10.main(Example10.java:18)
In this way, we use “BlockWhenExhausted” property.