KeyedObjectPool

A "keyed" pooling interface. <p> A keyed pool maintains a pool of instances for each key value. </p> <p> Example of use: </p> <pre style="border:solid thin; padding: 1ex;" > Object obj = <code style="color:#00C">null</code>; Object key = <code style="color:#C00">"Key"</code>;

<code style="color:#00C">try</code> { obj = pool.borrowObject(key); <code style="color:#0C0">//...use the object...</code> } <code style="color:#00C">catch</code>(Exception e) { <code style="color:#0C0">// invalidate the object</code> pool.invalidateObject(key, obj); <code style="color:#0C0">// do not return the object to the pool twice</code> obj = <code style="color:#00C">null</code>; } <code style="color:#00C">finally</code> { <code style="color:#0C0">// make sure the object is returned to the pool</code> <code style="color:#00C">if</code>(<code style="color:#00C">null</code> != obj) { pool.returnObject(key, obj); } }</pre> <p> {@link KeyedObjectPool} implementations <i>may</i> 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}). </p> <p> See {@link hunt.pool.impl.GenericKeyedObjectPool GenericKeyedObjectPool} for an implementation. </p>

@param <K> The type of keys maintained by this pool. @param <V> Type of element pooled in this pool.

@see KeyedPooledObjectFactory @see ObjectPool @see hunt.pool.impl.GenericKeyedObjectPool GenericKeyedObjectPool

Members

Functions

addObject
void addObject(K key)

Create an object using the {@link KeyedPooledObjectFactory factory} or other implementation dependent mechanism, passivate it, and then place it in the idle object pool. <code>addObject</code> is useful for "pre-loading" a pool with idle objects (Optional operation).

borrowObject
V borrowObject(K key)

Obtains an instance from this pool for the specified <code>key</code>. <p> 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}. </p> <p> By contract, clients <strong>must</strong> 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</code> that is {@link Object#equals equivalent} to the one used to borrow the instance in the first place. </p> <p> The behaviour of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations). </p>

clear
void clear()

Clears the pool, removing all pooled instances (optional operation).

clear
void clear(K key)

Clears the specified pool, removing all pooled instances corresponding to the given <code>key</code> (optional operation).

close
void close()

Close this pool, and free any resources associated with it. <p> Calling {@link #addObject addObject} or {@link #borrowObject borrowObject} after invoking this method on a pool will cause them to throw an {@link IllegalStateException}. </p> <p> Implementations should silently fail if not all resources can be freed. </p>

getNumActive
int getNumActive(K key)

Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the given <code>key</code>. Returns a negative value if this information is not available.

getNumActive
int getNumActive()

Returns the total number of instances currently borrowed from this pool but not yet returned. Returns a negative value if this information is not available. @return the total number of instances currently borrowed from this pool but not yet returned.

getNumIdle
int getNumIdle(K key)

Returns the number of instances corresponding to the given <code>key</code> currently idle in this pool. Returns a negative value if this information is not available.

getNumIdle
int getNumIdle()

Returns the total number of instances currently idle in this pool. Returns a negative value if this information is not available. @return the total number of instances currently idle in this pool.

invalidateObject
void invalidateObject(K key, V obj)

Invalidates an object from the pool. <p> By contract, <code>obj</code> <strong>must</strong> have been obtained using {@link #borrowObject borrowObject} or a related method as defined in an implementation or sub-interface using a <code>key</code> that is equivalent to the one used to borrow the <code>Object</code> in the first place. </p> <p> This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid. </p>

returnObject
void returnObject(K key, V obj)

Return an instance to the pool. By contract, <code>obj</code> <strong>must</strong> have been obtained using {@link #borrowObject borrowObject} or a related method as defined in an implementation or sub-interface using a <code>key</code> that is equivalent to the one used to borrow the instance in the first place.

Meta