* All operations defined here are essentially no-op's. *
** This class is immutable, and therefore thread-safe. *
* * @see KeyedPooledObjectFactory * * @param* The default implementation is a no-op. *
* * @param key the key used when selecting the object * @param p a {@code PooledObject} wrapping the instance to be activated */ @Override public void activateObject(final K key, final PooledObject* The default implementation is a no-op. *
* * @param key the key used when selecting the instance * @param p a {@code PooledObject} wrapping the instance to be destroyed */ @Override public void destroyObject(final K key, final PooledObject* The default implementation is a no-op. *
* * @param key the key used when selecting the object * @param p a {@code PooledObject} wrapping the instance to be passivated */ @Override public void passivateObject(final K key, final PooledObject* The default implementation always returns {@code true}. *
* * @param key the key used when selecting the object * @param p a {@code PooledObject} wrapping the instance to be validated * @return always {@code true} in the default implementation */ @Override public boolean validateObject(final K key, final PooledObject* This class is intended to be thread-safe. *
* * @param* This affects the behavior of {@code isClosed} and * {@code assertOpen}. *
*/ @Override public void close() { closed = true; } /** * Not supported in this base implementation. * * @return a negative value. */ @Override public int getNumActive() { return -1; } /** * Not supported in this base implementation. * * @return a negative value. */ @Override public int getNumIdle() { return -1; } @Override public abstract void invalidateObject(T obj) throws Exception; /** * Has this pool instance been closed. * * @return {@code true} when this pool has been closed. */ public final boolean isClosed() { return closed; } @Override public abstract void returnObject(T obj) throws Exception; @Override protected void toStringAppendFields(final StringBuilder builder) { builder.append("closed="); builder.append(closed); } } BasePooledObjectFactory.java 0000664 0000000 0000000 00000005672 14054251322 0034056 0 ustar 00root root 0000000 0000000 commons-pool-rel-commons-pool-2.10.0/src/main/java/org/apache/commons/pool2 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.pool2; /** * A base implementation of {@code PoolableObjectFactory}. ** All operations defined here are essentially no-op's. *
* This class is immutable, and therefore thread-safe
*
* @param This method must support concurrent, multi-threaded
* activation.
* A keyed pool maintains a pool of instances for each key value.
*
* Example of use:
*
* {@link KeyedObjectPool} implementations may choose to store at most
* one instance per key value, or may choose to maintain a pool of instances
* for each key (essentially creating a {@link java.util.Map Map} of
* {@link ObjectPool pools}).
*
* See {@link org.apache.commons.pool2.impl.GenericKeyedObjectPool
* GenericKeyedObjectPool} for an implementation.
*
* Instances returned from this method will have been either newly created
* with {@link KeyedPooledObjectFactory#makeObject makeObject} or will be
* a previously idle object and have been activated with
* {@link KeyedPooledObjectFactory#activateObject activateObject} and then
* (optionally) validated with
* {@link KeyedPooledObjectFactory#validateObject validateObject}.
*
* By contract, clients must return the borrowed object
* using {@link #returnObject returnObject},
* {@link #invalidateObject invalidateObject}, or a related method as
* defined in an implementation or sub-interface, using a {@code key}
* that is {@link Object#equals equivalent} to the one used to borrow the
* instance in the first place.
*
* The behavior of this method when the pool has been exhausted is not
* strictly specified (although it may be specified by implementations).
*
* Calling {@link #addObject addObject} or
* {@link #borrowObject borrowObject} after invoking this method on a pool
* will cause them to throw an {@link IllegalStateException}.
*
* Implementations should silently fail if not all resources can be freed.
*
* By contract, {@code obj} must have been obtained
* using {@link #borrowObject borrowObject} or a related method as defined
* in an implementation or sub-interface using a {@code key} that is
* equivalent to the one used to borrow the {@code Object} in the first
* place.
*
* This method should be used when an object that has been borrowed is
* determined (due to an exception or other problem) to be invalid.
*
* By contract, {@code obj} must have been obtained
* using {@link #borrowObject borrowObject} or a related method as defined
* in an implementation or sub-interface using a {@code key} that is
* equivalent to the one used to borrow the {@code Object} in the first
* place.
*
* This method should be used when an object that has been borrowed is
* determined (due to an exception or other problem) to be invalid.
*
* By contract, when an {@link KeyedObjectPool}
* delegates to a {@link KeyedPooledObjectFactory},
*
* While clients of a {@link KeyedObjectPool} borrow and return instances of
* the underlying value type V, the factory methods act on instances of
* {@link PooledObject PooledObject<V>}. These are the object wrappers that
* pools use to track and maintain state informations about the objects that
* they manage.
*
* It is important for implementations of this method to be aware that there
* is no guarantee about what state {@code obj} will be in and the
* implementation should be prepared to handle unexpected errors.
*
* Also, an implementation must take in to consideration that instances lost
* to the garbage collector may never be destroyed.
*
* Example of use:
*
* See {@link BaseObjectPool} for a simple base implementation.
*
* Instances returned from this method will have been either newly created
* with {@link PooledObjectFactory#makeObject} or will be a previously
* idle object and have been activated with
* {@link PooledObjectFactory#activateObject} and then validated with
* {@link PooledObjectFactory#validateObject}.
*
* By contract, clients must return the borrowed instance
* using {@link #returnObject}, {@link #invalidateObject}, or a related
* method as defined in an implementation or sub-interface.
*
* The behavior of this method when the pool has been exhausted
* is not strictly specified (although it may be specified by
* implementations).
*
* Calling {@link #addObject} or {@link #borrowObject} after invoking this
* method on a pool will cause them to throw an {@link IllegalStateException}.
*
* Implementations should silently fail if not all resources can be freed.
*
* By contract, {@code obj} must have been obtained
* using {@link #borrowObject} or a related method as defined in an
* implementation or sub-interface.
*
* This method should be used when an object that has been borrowed is
* determined (due to an exception or other problem) to be invalid.
*
* By contract, {@code obj} must have been obtained
* using {@link #borrowObject} or a related method as defined in an
* implementation or sub-interface.
*
* This method should be used when an object that has been borrowed is
* determined (due to an exception or other problem) to be invalid.
*
* Note: This should not be used on pool implementations that already
* provide proper synchronization such as the pools provided in the Commons
* Pool library. Wrapping a pool that {@link #wait() waits} for poolable
* objects to be returned before allowing another one to be borrowed with
* another layer of synchronization will cause liveliness issues or a
* deadlock.
*
* Note: This should not be used on pool implementations that already
* provide proper synchronization such as the pools provided in the Commons
* Pool library.
*
* Note: This should not be used on pool implementations that already
* provide proper synchronization such as the pools provided in the Commons
* Pool library. Wrapping a pool that {@link #wait() waits} for poolable
* objects to be returned before allowing another one to be borrowed with
* another layer of synchronization will cause liveliness issues or a
* deadlock.
*
* Note: This should not be used on pool implementations that already
* provide proper synchronization such as the pools provided in the Commons
* Pool library.
*
* The factor parameter provides a mechanism to tweak the rate at which the
* pool tries to shrink its size. Values between 0 and 1 cause the pool to
* try to shrink its size more often. Values greater than 1 cause the pool
* to less frequently try to shrink its size.
*
* The factor parameter provides a mechanism to tweak the rate at which the
* pool tries to shrink its size. Values between 0 and 1 cause the pool to
* try to shrink its size more often. Values greater than 1 cause the pool
* to less frequently try to shrink its size.
*
* The perKey parameter determines if the pool shrinks on a whole pool basis
* or a per key basis. When perKey is false, the keys do not have an effect
* on the rate at which the pool tries to shrink its size. When perKey is
* true, each key is shrunk independently.
*
* The factor parameter provides a mechanism to tweak the rate at which the
* pool tries to shrink its size. Values between 0 and 1 cause the pool to
* try to shrink its size more often. Values greater than 1 cause the pool
* to less frequently try to shrink its size.
*
* Note: This should not be used on pool implementations that already
* provide proper synchronization such as the pools provided in the Commons
* Pool library. Wrapping a pool that {@link #wait() waits} for poolable
* objects to be returned before allowing another one to be borrowed with
* another layer of synchronization will cause liveliness issues or a
* deadlock.
*
* Note: This should not be used on pool implementations that already
* provide proper synchronization such as the pools provided in the Commons
* Pool library. Wrapping a pool that {@link #wait() waits} for poolable
* objects to be returned before allowing another one to be borrowed with
* another layer of synchronization will cause liveliness issues or a
* deadlock.
*
* Implementations of this class are required to be thread-safe.
*
* Note: This class has a natural ordering that is inconsistent with
* equals if distinct objects have the same identity hash code.
*
* {@inheritDoc}
*
* {@inheritDoc}
*/
@Override
String toString();
/**
* Record the current stack trace as the last time the object was used.
*/
void use();
}
commons-pool-rel-commons-pool-2.10.0/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java0000664 0000000 0000000 00000013514 14054251322 0033334 0 ustar 00root root 0000000 0000000 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.pool2;
/**
* An interface defining life-cycle methods for instances to be served by an
* {@link ObjectPool}.
*
* By contract, when an {@link ObjectPool} delegates to a
* {@link PooledObjectFactory},
*
* While clients of a {@link KeyedObjectPool} borrow and return instances of
* the underlying value type {@code V}, the factory methods act on instances of
* {@link PooledObject PooledObject<V>}. These are the object wrappers that
* pools use to track and maintain state information about the objects that
* they manage.
*
* It is important for implementations of this method to be aware that there
* is no guarantee about what state {@code obj} will be in and the
* implementation should be prepared to handle unexpected errors.
*
* Also, an implementation must take in to consideration that instances lost
* to the garbage collector may never be destroyed.
* Flag to remove abandoned objects if they exceed the
* removeAbandonedTimeout when borrowObject is invoked. The default value is false. If set to true, abandoned objects are removed by borrowObject if
* there are fewer than 2 idle objects available in the pool and
* {@code getNumActive() > getMaxTotal() - 3} Flag to remove abandoned objects if they exceed the
* removeAbandonedTimeout when pool maintenance (the "evictor")
* runs. The default value is false. If set to true, abandoned objects are removed by the pool
* maintenance thread when it runs. This setting has no effect
* unless maintenance is enabled by setting
*{@link GenericObjectPool#getTimeBetweenEvictionRuns() timeBetweenEvictionRuns}
* to a positive number. Timeout in seconds before an abandoned object can be removed. The time of most recent use of an object is the maximum (latest) of
* {@link TrackedUse#getLastUsed()} (if this class of the object implements
* TrackedUse) and the time when the object was borrowed from the pool. The default value is 300 seconds. Timeout before an abandoned object can be removed. The time of most recent use of an object is the maximum (latest) of
* {@link TrackedUse#getLastUsed()} (if this class of the object implements
* TrackedUse) and the time when the object was borrowed from the pool. The default value is 300 seconds. Flag to remove abandoned objects if they exceed the
* removeAbandonedTimeout when borrowObject is invoked. Flag to remove abandoned objects if they exceed the
* removeAbandonedTimeout when pool maintenance runs. Sets the timeout before an abandoned object can be
* removed Setting this property has no effect if
* {@link #getRemoveAbandonedOnBorrow() removeAbandonedOnBorrow} and
* {@link #getRemoveAbandonedOnMaintenance() removeAbandonedOnMaintenance}
* are both false. Sets the timeout in seconds before an abandoned object can be
* removed Setting this property has no effect if
* {@link #getRemoveAbandonedOnBorrow() removeAbandonedOnBorrow} and
* {@link #getRemoveAbandonedOnMaintenance() removeAbandonedOnMaintenance}
* are both false. Perform {@code numTests} idle object eviction tests, evicting
* examined objects that meet the criteria for eviction. If
* {@code testWhileIdle} is true, examined objects are validated
* when visited (and removed if invalid); otherwise only objects that
* have been idle for more than {@code minEvicableIdleTimeMillis}
* are removed. Starts the evictor with the given delay. If there is an evictor
* running when this method is called, it is stopped and replaced with a
* new evictor with the specified delay. This method needs to be final, since it is called from a constructor.
* See POOL-195.
* This class is not thread-safe.
*
* This class is immutable and thread-safe.
*
* This class is intended to be thread-safe.
*
* This class is immutable and thread-safe.
*
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
*
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
*
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
*
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
*
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
*
* This class is currently implemented using {@link ScheduledThreadPoolExecutor}. This implementation may change in any
* future release. This class keeps track of how many pools are using it. If no pools are using the timer, it is
* cancelled. This prevents a thread being left running which, in application server environments, can lead to memory
* leads and/or prevent applications from shutting down or reloading cleanly.
*
* This class has package scope to prevent its inclusion in the pool public API. The class declaration below should
* *not* be changed to public.
*
* This class is intended to be thread-safe.
*
* When coupled with the appropriate {@link KeyedPooledObjectFactory},
* {@code GenericKeyedObjectPool} provides robust pooling functionality for
* keyed objects. A {@code GenericKeyedObjectPool} can be viewed as a map
* of sub-pools, keyed on the (unique) key values provided to the
* {@link #preparePool preparePool}, {@link #addObject addObject} or
* {@link #borrowObject borrowObject} methods. Each time a new key value is
* provided to one of these methods, a sub-new pool is created under the given
* key to be managed by the containing {@code GenericKeyedObjectPool.}
*
* Note that the current implementation uses a ConcurrentHashMap which uses
* equals() to compare keys.
* This means that distinct instance keys must be distinguishable using equals.
*
* Optionally, one may configure the pool to examine and possibly evict objects
* as they sit idle in the pool and to ensure that a minimum number of idle
* objects is maintained for each key. This is performed by an "idle object
* eviction" thread, which runs asynchronously. Caution should be used when
* configuring this optional feature. Eviction runs contend with client threads
* for access to objects in the pool, so if they run too frequently performance
* issues may result.
*
* Implementation note: To prevent possible deadlocks, care has been taken to
* ensure that no call to a factory method will occur within a synchronization
* block. See POOL-125 and DBCP-44 for more information.
*
* This class is intended to be thread-safe.
* Object obj =
* null
;
* Object key = "Key"
;
*
* try
{
* obj = pool.borrowObject(key);
* //...use the object...
* } catch
(Exception e) {
* // invalidate the object
* pool.invalidateObject(key, obj);
* // do not return the object to the pool twice
* obj = null
;
* } finally
{
* // make sure the object is returned to the pool
* if
(null
!= obj) {
* pool.returnObject(key, obj);
* }
* }
*
* {@link KeyedPooledObjectFactory} must be thread-safe. The only promise
* an {@link KeyedObjectPool} makes is that the same instance of an object will
* not be passed to more than one method of a
* {@code KeyedPoolableObjectFactory} at a time.
* Object obj =
* null
;
*
* try
{
* obj = pool.borrowObject();
* try
{
* //...use the object...
* } catch
(Exception e) {
* // invalidate the object
* pool.invalidateObject(obj);
* // do not return the object to the pool twice
* obj = null
;
* } finally
{
* // make sure the object is returned to the pool
* if
(null
!= obj) {
* pool.returnObject(obj);
* }
* }
* } catch
(Exception e) {
* // failed to borrow an object
* }
*
* {@link PooledObjectFactory} must be thread-safe. The only promise
* an {@link ObjectPool} makes is that the same instance of an object will not
* be passed to more than one method of a {@code PoolableObjectFactory}
* at a time.
* ceil({@link #getNumIdle}/
* abs({@link #getNumTestsPerEvictionRun}))
which means that when the
* value is {@code -n} roughly one nth of the idle objects will be
* tested per run.
*
* @return max number of objects to examine during each evictor run
*
* @see #setNumTestsPerEvictionRun
* @see #setTimeBetweenEvictionRunsMillis
*/
public final int getNumTestsPerEvictionRun() {
return numTestsPerEvictionRun;
}
/**
* The total number of objects returned to this pool over the lifetime of
* the pool. This excludes attempts to return the same object multiple
* times.
* @return the returned object count
*/
public final long getReturnedCount() {
return returnedCount.get();
}
/**
* Gets the minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction by the idle object evictor (if any -
* see {@link #setTimeBetweenEvictionRuns(Duration)}),
* with the extra condition that at least {@code minIdle} object
* instances remain in the pool. This setting is overridden by
* {@link #getMinEvictableIdleTime} (that is, if
* {@link #getMinEvictableIdleTime} is positive, then
* {@link #getSoftMinEvictableIdleTime} is ignored).
*
* @return minimum amount of time an object may sit idle in the pool before
* it is eligible for eviction if minIdle instances are available
*
* @see #setSoftMinEvictableIdleTime(Duration)
* @since 2.10.0
*/
public final Duration getSoftMinEvictableIdleTime() {
return softMinEvictableIdleTime;
}
/**
* Gets the minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction by the idle object evictor (if any -
* see {@link #setTimeBetweenEvictionRunsMillis(long)}),
* with the extra condition that at least {@code minIdle} object
* instances remain in the pool. This setting is overridden by
* {@link #getMinEvictableIdleTimeMillis} (that is, if
* {@link #getMinEvictableIdleTimeMillis} is positive, then
* {@link #getSoftMinEvictableIdleTimeMillis} is ignored).
*
* @return minimum amount of time an object may sit idle in the pool before
* it is eligible for eviction if minIdle instances are available
*
* @see #setSoftMinEvictableIdleTimeMillis
* @deprecated Use {@link #getSoftMinEvictableIdleTime()}.
*/
@Deprecated
public final long getSoftMinEvictableIdleTimeMillis() {
return softMinEvictableIdleTime.toMillis();
}
/**
* Gets the stack trace of an exception as a string.
* @param e exception to trace
* @return exception stack trace as a string
*/
private String getStackTrace(final Exception e) {
// Need the exception in string form to prevent the retention of
// references to classes in the stack trace that could trigger a memory
// leak in a container environment.
final Writer w = new StringWriter();
final PrintWriter pw = new PrintWriter(w);
e.printStackTrace(pw);
return w.toString();
}
/**
* The listener used (if any) to receive notifications of exceptions
* unavoidably swallowed by the pool.
*
* @return The listener or {@code null} for no listener
*/
public final SwallowedExceptionListener getSwallowedExceptionListener() {
return swallowedExceptionListener;
}
/**
* Gets whether objects borrowed from the pool will be validated before
* being returned from the {@code borrowObject()} method. Validation is
* performed by the {@code validateObject()} method of the factory
* associated with the pool. If the object fails to validate, it will be
* removed from the pool and destroyed, and a new attempt will be made to
* borrow an object from the pool.
*
* @return {@code true} if objects are validated before being returned
* from the {@code borrowObject()} method
*
* @see #setTestOnBorrow
*/
public final boolean getTestOnBorrow() {
return testOnBorrow;
}
/**
* Gets whether objects created for the pool will be validated before
* being returned from the {@code borrowObject()} method. Validation is
* performed by the {@code validateObject()} method of the factory
* associated with the pool. If the object fails to validate, then
* {@code borrowObject()} will fail.
*
* @return {@code true} if newly created objects are validated before
* being returned from the {@code borrowObject()} method
*
* @see #setTestOnCreate
*
* @since 2.2
*/
public final boolean getTestOnCreate() {
return testOnCreate;
}
/**
* Gets whether objects borrowed from the pool will be validated when
* they are returned to the pool via the {@code returnObject()} method.
* Validation is performed by the {@code validateObject()} method of
* the factory associated with the pool. Returning objects that fail validation
* are destroyed rather then being returned the pool.
*
* @return {@code true} if objects are validated on return to
* the pool via the {@code returnObject()} method
*
* @see #setTestOnReturn
*/
public final boolean getTestOnReturn() {
return testOnReturn;
}
/**
* Gets whether objects sitting idle in the pool will be validated by the
* idle object evictor (if any - see
* {@link #setTimeBetweenEvictionRuns(Duration)}). Validation is performed
* by the {@code validateObject()} method of the factory associated
* with the pool. If the object fails to validate, it will be removed from
* the pool and destroyed.
*
* @return {@code true} if objects will be validated by the evictor
*
* @see #setTestWhileIdle
* @see #setTimeBetweenEvictionRunsMillis
*/
public final boolean getTestWhileIdle() {
return testWhileIdle;
}
/**
* Gets the duration to sleep between runs of the idle
* object evictor thread. When non-positive, no idle object evictor thread
* will be run.
*
* @return number of milliseconds to sleep between evictor runs
*
* @see #setTimeBetweenEvictionRuns
* @since 2.10.0
*/
public final Duration getTimeBetweenEvictionRuns() {
return timeBetweenEvictionRuns;
}
/**
* Gets the number of milliseconds to sleep between runs of the idle
* object evictor thread. When non-positive, no idle object evictor thread
* will be run.
*
* @return number of milliseconds to sleep between evictor runs
*
* @see #setTimeBetweenEvictionRunsMillis
* @deprecated Use {@link #getTimeBetweenEvictionRuns()}.
*/
@Deprecated
public final long getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRuns.toMillis();
}
/**
* Has this pool instance been closed.
* @return {@code true} when this pool has been closed.
*/
public final boolean isClosed() {
return closed;
}
/**
* Registers the pool with the platform MBean server.
* The registered name will be
* {@code jmxNameBase + jmxNamePrefix + i} where i is the least
* integer greater than or equal to 1 such that the name is not already
* registered. Swallows MBeanRegistrationException, NotCompliantMBeanException
* returning null.
*
* @param config Pool configuration
* @param jmxNameBase default base JMX name for this pool
* @param jmxNamePrefix name prefix
* @return registered ObjectName, null if registration fails
*/
private ObjectName jmxRegister(final BaseObjectPoolConfigceil({@link #getNumIdle}/
* abs({@link #getNumTestsPerEvictionRun}))
which means that when the
* value is {@code -n} roughly one nth of the idle objects will be
* tested per run.
*
* @param numTestsPerEvictionRun
* max number of objects to examine during each evictor run
*
* @see #getNumTestsPerEvictionRun
* @see #setTimeBetweenEvictionRunsMillis
*/
public final void setNumTestsPerEvictionRun(final int numTestsPerEvictionRun) {
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
}
/**
* Sets the minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction by the idle object evictor (if any -
* see {@link #setTimeBetweenEvictionRuns(Duration)}),
* with the extra condition that at least {@code minIdle} object
* instances remain in the pool. This setting is overridden by
* {@link #getMinEvictableIdleTime} (that is, if
* {@link #getMinEvictableIdleTime} is positive, then
* {@link #getSoftMinEvictableIdleTime} is ignored).
*
* @param softMinEvictableIdleTime
* minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction if minIdle instances are
* available
*
* @see #getSoftMinEvictableIdleTimeMillis
* @since 2.10.0
*/
public final void setSoftMinEvictableIdleTime(final Duration softMinEvictableIdleTime) {
this.softMinEvictableIdleTime = softMinEvictableIdleTime;
}
/**
* Sets the minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction by the idle object evictor (if any -
* see {@link #setTimeBetweenEvictionRunsMillis(long)}),
* with the extra condition that at least {@code minIdle} object
* instances remain in the pool. This setting is overridden by
* {@link #getMinEvictableIdleTimeMillis} (that is, if
* {@link #getMinEvictableIdleTimeMillis} is positive, then
* {@link #getSoftMinEvictableIdleTimeMillis} is ignored).
*
* @param softMinEvictableIdleTimeMillis
* minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction if minIdle instances are
* available
*
* @see #getSoftMinEvictableIdleTimeMillis
* @deprecated Use {@link #setSoftMinEvictableIdleTime(Duration)}.
*/
@Deprecated
public final void setSoftMinEvictableIdleTimeMillis(final long softMinEvictableIdleTimeMillis) {
this.softMinEvictableIdleTime = Duration.ofMillis(softMinEvictableIdleTimeMillis);
}
/**
* The listener used (if any) to receive notifications of exceptions
* unavoidably swallowed by the pool.
*
* @param swallowedExceptionListener The listener or {@code null}
* for no listener
*/
public final void setSwallowedExceptionListener(
final SwallowedExceptionListener swallowedExceptionListener) {
this.swallowedExceptionListener = swallowedExceptionListener;
}
/**
* Sets whether objects borrowed from the pool will be validated before
* being returned from the {@code borrowObject()} method. Validation is
* performed by the {@code validateObject()} method of the factory
* associated with the pool. If the object fails to validate, it will be
* removed from the pool and destroyed, and a new attempt will be made to
* borrow an object from the pool.
*
* @param testOnBorrow {@code true} if objects should be validated
* before being returned from the
* {@code borrowObject()} method
*
* @see #getTestOnBorrow
*/
public final void setTestOnBorrow(final boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
/**
* Sets whether objects created for the pool will be validated before
* being returned from the {@code borrowObject()} method. Validation is
* performed by the {@code validateObject()} method of the factory
* associated with the pool. If the object fails to validate, then
* {@code borrowObject()} will fail.
*
* @param testOnCreate {@code true} if newly created objects should be
* validated before being returned from the
* {@code borrowObject()} method
*
* @see #getTestOnCreate
*
* @since 2.2
*/
public final void setTestOnCreate(final boolean testOnCreate) {
this.testOnCreate = testOnCreate;
}
/**
* Sets whether objects borrowed from the pool will be validated when
* they are returned to the pool via the {@code returnObject()} method.
* Validation is performed by the {@code validateObject()} method of
* the factory associated with the pool. Returning objects that fail validation
* are destroyed rather then being returned the pool.
*
* @param testOnReturn {@code true} if objects are validated on
* return to the pool via the
* {@code returnObject()} method
*
* @see #getTestOnReturn
*/
public final void setTestOnReturn(final boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
/**
* Sets whether objects sitting idle in the pool will be validated by the
* idle object evictor (if any - see
* {@link #setTimeBetweenEvictionRuns(Duration)}). Validation is performed
* by the {@code validateObject()} method of the factory associated
* with the pool. If the object fails to validate, it will be removed from
* the pool and destroyed. Note that setting this property has no effect
* unless the idle object evictor is enabled by setting
* {@code timeBetweenEvictionRunsMillis} to a positive value.
*
* @param testWhileIdle
* {@code true} so objects will be validated by the evictor
*
* @see #getTestWhileIdle
* @see #setTimeBetweenEvictionRuns
*/
public final void setTestWhileIdle(final boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
/**
* Sets the number of milliseconds to sleep between runs of the idle object evictor thread.
*
*
*
* @param timeBetweenEvictionRuns
* duration to sleep between evictor runs
*
* @see #getTimeBetweenEvictionRunsMillis
* @since 2.10.0
*/
public final void setTimeBetweenEvictionRuns(final Duration timeBetweenEvictionRuns) {
this.timeBetweenEvictionRuns = timeBetweenEvictionRuns;
startEvictor(this.timeBetweenEvictionRuns);
}
/**
* Sets the number of milliseconds to sleep between runs of the idle object evictor thread.
*
*
*
* @param timeBetweenEvictionRunsMillis
* number of milliseconds to sleep between evictor runs
*
* @see #getTimeBetweenEvictionRunsMillis
* @deprecated Use {@link #setTimeBetweenEvictionRuns(Duration)}.
*/
@Deprecated
public final void setTimeBetweenEvictionRunsMillis(final long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRuns = Duration.ofMillis(timeBetweenEvictionRunsMillis);
startEvictor(timeBetweenEvictionRuns);
}
/**
*
*
* type of objects in the pool
*/
private class ObjectDeque {
private final LinkedBlockingDeque> allObjects =
new ConcurrentHashMap<>();
/*
* Number of threads with registered interest in this key.
* register(K) increments this counter and deRegister(K) decrements it.
* Invariant: empty keyed pool will not be dropped unless numInterested
* is 0.
*/
private final AtomicLong numInterested = new AtomicLong(0);
/**
* Create a new ObjecDeque with the given fairness policy.
* @param fairness true means client threads waiting to borrow / return instances
* will be served as if waiting in a FIFO queue.
*/
public ObjectDeque(final boolean fairness) {
idleObjects = new LinkedBlockingDeque<>(fairness);
}
/**
* Obtain all the objects for the current key.
*
* @return All the objects
*/
public Map> getAllObjects() {
return allObjects;
}
/**
* Obtain the count of the number of objects created for the current
* key.
*
* @return The number of objects created for this key
*/
public AtomicInteger getCreateCount() {
return createCount;
}
/**
* Obtain the idle objects for the current key.
*
* @return The idle objects
*/
public LinkedBlockingDeque