How to figure out the number of idle objects and borrowed objects in Apache Common Pool 2

497 Views Asked by At

There are several getXXXCount method defined in the BaseGenericObjectPool class

  1. BaseGenericObjectPool.getBorrowedCount

  2. BaseGenericObjectPool.getCreatedCount

  3. BaseGenericObjectPool.getDestroyedCount

But all of them are computed since the pool was created, that is, the count is accumulated.

I would ask how to figure out the number of being borrowed objects and being idle objects the moment when user asks for these count.

1

There are 1 best solutions below

0
Alexander Ziubin On

BaseGenericObjectPool abstract class declares getNumIdle() method, which returns "the number of instances currently idle in the pool". BaseGenericObjectPool by itself does not provide the number of borrowed instances.

To have the number of borrowed instances you should look at classes, which implement ObjectPool or KeyedObjectPool interfaces, for example GenericObjectPool or GenericKeyedObjectPool class. These interfaces both declares the getNumActive() method, which returns "the number of instances currently borrowed from this pool" (ObjectPool case) or "the total number of instances current borrowed from this pool but not yet returned" (KeyedObjectPool case).