3

I am using 1.6 version of apache commons-pool library. As per the javadoc,

whenExhaustedAction specifies the behavior of the borrowObject() method when the pool is exhausted: It can be either WHEN_EXHAUSTED_FAIL, WHEN_EXHAUSTED_GROW, or WHEN_EXHAUSTED_BLOCK.

I want to use the borrowObject and if don't get the object within a specified timeframe, I need some sort of handle to handle the scenario(like i will be rescheduling the tasks, if i don't get the target object)

But the only option, I get here is NoSuchElementException which is a RuntimeException, which I need to catch and handle the error scenario. I am quite sceptical about catching the RuntimeException

Is this the intended way of handling object starvation with GenericObjectPool or do I have any other options ?

user1516873
  • 5,060
  • 2
  • 37
  • 56
Albie Morken
  • 215
  • 1
  • 4
  • 14

1 Answers1

2

I've looked at the borrowObject documentation and it states that it throws these exceptions

IllegalStateException - after close has been called on this pool.
Exception - when makeObject throws an exception.
NoSuchElementException - when the pool is exhausted and cannot or will not return another instance.

Because NoSuchElementException is a documented behavior of this method, there is nothing wrong with catching it around borrowObject and handling to your liking.

I suggest you catch it right around the call and wrap with one of your own, so if other method in your function throws NoSuchElementException the high level handler is not confused with pool exhaustion. Wrapper exception can be either checked or runtime, depending on your preference and project requirements.

e.g

final T obj;

try
{
  obj = pool.borrowObject( );
}
catch ( NoSuchElementException ex )
{
  throw new MyPoolExhausetdException( ex );
}

// Do something with obj
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121