T - the type of objects managed by the object poolE - the type of exceptions involved in the life cycle of the managed objectspublic class GenericObjectPool<T,E extends java.lang.Exception> extends java.lang.Object implements ObjectPool<T,E>
ObjectPool.
The pool has a certain number of slots that is determined by
PoolConfiguration.getMaxObjectCount(). Some slots contain checked out objects, all other
slots are available. They are either empty or contain available objects that can be checked out.
If no object's are available, an empty slot will be filled with a new object which can then be
checked out.
Please note: If at all, Object.equals(Object) and Object.hashCode() on the pooled objects
must be implemented in such a way that no two different objects in the pool share the same
"identity".
Some features:
| Modifier and Type | Field and Description |
|---|---|
protected java.util.logging.Logger |
logger
The logger for log messages.
|
| Constructor and Description |
|---|
GenericObjectPool(ObjectLifeCycle<T,E> objectLifeCycle,
ObjectWrapperLifeCycle<T,E> objectWrapperLifeCycle,
PoolConfiguration configuration,
java.util.logging.Logger logger)
Constructs a new
GenericObjectPool. |
| Modifier and Type | Method and Description |
|---|---|
void |
awaitTermination()
Waits and blocks until this pool is terminated.
|
boolean |
awaitTermination(long timeout)
Waits for the given amount of milliseconds for the pool to terminate.
|
void |
checkIn(T object)
Returns an object to the pool.
|
void |
checkIn(T origObject,
boolean indicateError)
Besides returning an object to the pool, this method allows the borrower to
indicate that the object being checked in is or may be
invalid. |
T |
checkOut()
Returns an available object from the object pool.
|
T |
checkOutInterruptibly()
This method is equivalent to
checkOut() except that the calling
thread may be interrupted. |
T |
checkOutInterruptibly(long timeout)
This method is equivalent to
checkOut() except that the calling
thread may be interrupted and that it only ways for the given amount of
time for an object to become available. |
protected void |
finalize() |
void |
forceShutdown()
Forces a shutdown of this object pool, i.e. no more objects can be checked
out.
|
void |
forceShutdown(long delay)
Initiates a shutdown of this object pool, i.e. no more objects can be
checked out, but objects can still be checked back in.
|
int |
getAvailableObjectCount()
Returns the current number of available objects, i.e. objects that are
sitting idle in the pool.
|
int |
getCheckedOutObjectCount()
Returns the current number of checked out objects.
|
int |
getObjectCount()
Returns the current number of pooled objects.
|
ObjectPoolListener<T> |
getObjectPoolListener()
Returns the current listener of this pool.
|
boolean |
isShutdown()
Returns
true if either ObjectPool.shutdown() or
ObjectPool.forceShutdown() have been called on this pool. |
boolean |
isTerminated()
Returns
true if this pool is completely shut down and has ceased
all activities. |
void |
setObjectPoolListener(ObjectPoolListener<T> objectPoolListener)
Registers the given listener with this pool.
|
void |
shutdown()
Initiates an orderly shutdown of this object pool, i.e. no more objects can
be checked out, but objects can still be checked back in.
|
public GenericObjectPool(ObjectLifeCycle<T,E> objectLifeCycle, ObjectWrapperLifeCycle<T,E> objectWrapperLifeCycle, PoolConfiguration configuration, java.util.logging.Logger logger)
GenericObjectPool. With the given object life
cycle, wrapper life cycle (optional) and configuration.objectLifeCycle - the object life cycle to useobjectWrapperLifeCycle - the object wrapper life cycle to use
(optional)configuration - the configuration to uselogger - The logger for log messages.public ObjectPoolListener<T> getObjectPoolListener()
public void setObjectPoolListener(ObjectPoolListener<T> objectPoolListener)
null is
allowed.objectPoolListener - the new listenerpublic int getObjectCount()
public int getAvailableObjectCount()
public int getCheckedOutObjectCount()
public T checkOut() throws E extends java.lang.Exception
checkOut in interface ObjectPool<T,E extends java.lang.Exception>E - if the pool has to create a new object and this triggers an
exception (e.g. passed through exceptions from
ObjectLifeCycle.create())E extends java.lang.Exceptionpublic T checkOutInterruptibly() throws E extends java.lang.Exception, java.lang.InterruptedException
checkOut() except that the calling
thread may be interrupted.E - see checkOut()java.lang.InterruptedException - if the calling thread is interruptedE extends java.lang.Exceptionpublic T checkOutInterruptibly(long timeout) throws E extends java.lang.Exception, java.lang.InterruptedException
checkOut() except that the calling
thread may be interrupted and that it only ways for the given amount of
time for an object to become available.timeout - the time (in milliseconds) to wait for an object to become
availablenull if the
timeout expiresE - see checkOut()java.lang.InterruptedException - if the calling thread is interruptedE extends java.lang.Exceptionpublic void checkIn(T object) throws E extends java.lang.Exception
ObjectPoolshut down, but not once the
pool is terminated.
If the returned object is a wrapper for the actual object, the wrapper is
invalidated (see ObjectWrapperLifeCycle).
checkIn in interface ObjectPool<T,E extends java.lang.Exception>object - the object to be returnedE - if passivating the object failed (see ObjectLifeCycle.passivate(Object))E extends java.lang.Exceptionpublic void checkIn(T origObject, boolean indicateError) throws E extends java.lang.Exception
invalid. However, it's up to the
pool and its configuration what to do with this information. Other than
that, see checkIn(Object).origObject - the object to be returnedindicateError - this allows the caller to indicate that the object
didn't work as expected; this this will cause the pool to validate
the object if it wasn't already doing it anywayE - if passivating the object failed (see ObjectLifeCycle.passivate(Object))java.lang.IllegalArgumentException - if the given object is not managed by this
pooljava.lang.IllegalStateException - if the pool is already terminatedE extends java.lang.Exceptionpublic void awaitTermination()
throws java.lang.InterruptedException
java.lang.InterruptedException - if the current thread is interruptedpublic boolean awaitTermination(long timeout)
throws java.lang.InterruptedException
timeout - the time to wait for the termination of the object pooljava.lang.InterruptedException - if the current thread is interruptedpublic boolean isShutdown()
ObjectPooltrue if either ObjectPool.shutdown() or
ObjectPool.forceShutdown() have been called on this pool. I.e. it's either
completely shut down (i.e. terminated) or still in the process of shutting
down.isShutdown in interface ObjectPool<T,E extends java.lang.Exception>public boolean isTerminated()
ObjectPooltrue if this pool is completely shut down and has ceased
all activities.isTerminated in interface ObjectPool<T,E extends java.lang.Exception>public void shutdown()
ObjectPoolScheduled operations like the monitoring of expired leases may still continue.
If the pool is already shutting down, this method does not have any additional effects.
shutdown in interface ObjectPool<T,E extends java.lang.Exception>public void forceShutdown()
ObjectPoolThis method may be called even if the pool is already shutting down.
forceShutdown in interface ObjectPool<T,E extends java.lang.Exception>public void forceShutdown(long delay)
This method may be called even if the pool is already shutting down. Any previous delay should be reset to the new value.
delay - the delay in milliseconds before the pool will be forcefully
shut downprotected void finalize()
throws java.lang.Throwable
finalize in class java.lang.Objectjava.lang.Throwable