In this post under Apache Pool, I will explain with example the purpose of “MaxWait” duration property.
This property is used in conjunction with “BlockWhenExhausted” property explained in previous post.
This property works only when “BlockWhenExhausted” is set to “true”.
When “BlockWhenExhausted” is set to true and the caller tries to get idle objects more than the available in the pool, the code is blocked indefinitely. This is
the default behavior.
We can change this default behavior with the help of “MaxWait” duration property.
Using this property we configure the time, it has to wait to get an idle object from the pool.
If the caller code doesn’t get any idle object from the pool within the wait time an exception is thrown instead of waiting indefinitely.
Below is the complete main code for your reference
ThreadPooledObjectFactory
package package11;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
public class ThreadPooledObjectFactory extends BasePooledObjectFactory {
private int count = 0;
@Override
public Thread create() throws Exception {
String threadName = "Thread" + count;
count = count + 1;
return new Thread(threadName);
}
@Override
public PooledObject wrap(Thread obj) {
return new DefaultPooledObject(obj);
}
}
Main class
1 package package11;
2
3 import java.time.Duration;
4
5 import org.apache.commons.pool2.impl.GenericObjectPool;
6 import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
7
8 import package10.ThreadPooledObjectFactory;
9
10 public class Example11 {
11 public static void main(String[] args) throws Exception {
12 GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig<>();
13 genericObjectPoolConfig.setMaxTotal(3);
14 genericObjectPoolConfig.setMaxWait(Duration.ofMinutes(2));
15 ThreadPooledObjectFactory threadPooledObjectFactory = new ThreadPooledObjectFactory();
16 GenericObjectPool genericObjectPool = new GenericObjectPool<>(threadPooledObjectFactory, genericObjectPoolConfig);
17
18 try {
19 Thread thread1 = genericObjectPool.borrowObject();
20 Thread thread2 = genericObjectPool.borrowObject();
21 Thread thread3 = genericObjectPool.borrowObject();
22 Thread thread4 = genericObjectPool.borrowObject();
23 } catch(Exception exception) {
24 exception.printStackTrace();
25 }
26 }
27 }
In the above code, at line 14, I call the “setMaxWait” method on “GenericObjectPoolConfig” instance and set it to 2 minutes.
Below is the output
Output
java.util.NoSuchElementException: Timeout waiting for idle object, borrowMaxWaitDuration=PT2M
at org.apache.commons.pool2@2.12.0/org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:316)
at org.apache.commons.pool2@2.12.0/org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:233)
at ApachePoolConcepts.main/package11.Example11.main(Example11.java:22)
In this way we can use the “MaxWait” duration property.