All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.pool2.KeyedObjectPool Maven / Gradle / Ivy

There is a newer version: 2.12.0
Show newest version
/*
 * 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;

import java.io.Closeable;
import java.util.NoSuchElementException;

/**
 * A "keyed" pooling interface.
 * 

* A keyed pool maintains a pool of instances for each key value. *

*

* Example of use: *

*
 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 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. *

* * @param The type of keys maintained by this pool. * @param Type of element pooled in this pool. * * @see KeyedPooledObjectFactory * @see ObjectPool * @see org.apache.commons.pool2.impl.GenericKeyedObjectPool GenericKeyedObjectPool * * @since 2.0 */ public interface KeyedObjectPool extends Closeable { /** * Obtains an instance from this pool for the specified key. *

* 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 key * that is {@link Object#equals equivalent} to the one used to borrow the * instance in the first place. *

*

* The behaviour of this method when the pool has been exhausted is not * strictly specified (although it may be specified by implementations). *

* * @param key the key used to obtain the object * * @return an instance from this pool. * * @throws IllegalStateException * after {@link #close close} has been called on this pool * @throws Exception * when {@link KeyedPooledObjectFactory#makeObject * makeObject} throws an exception * @throws NoSuchElementException * when the pool is exhausted and cannot or will not return * another instance */ V borrowObject(K key) throws Exception, NoSuchElementException, IllegalStateException; /** * Return an instance to the pool. By contract, obj * must have been obtained using * {@link #borrowObject borrowObject} or a related method as defined in an * implementation or sub-interface using a key that is * equivalent to the one used to borrow the instance in the first place. * * @param key the key used to obtain the object * @param obj a {@link #borrowObject borrowed} instance to be returned. * * @throws IllegalStateException * if an attempt is made to return an object to the pool that * is in any state other than allocated (i.e. borrowed). * Attempting to return an object more than once or attempting * to return an object that was never borrowed from the pool * will trigger this exception. * * @throws Exception if an instance cannot be returned to the pool */ void returnObject(K key, V obj) throws Exception; /** * Invalidates an object from the pool. *

* By contract, obj must have been obtained * using {@link #borrowObject borrowObject} or a related method as defined * in an implementation or sub-interface using a key that is * equivalent to the one used to borrow the 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. *

* * @param key the key used to obtain the object * @param obj a {@link #borrowObject borrowed} instance to be returned. * * @throws Exception if the instance cannot be invalidated */ void invalidateObject(K key, V obj) throws Exception; /** * Create an object using the {@link KeyedPooledObjectFactory factory} or * other implementation dependent mechanism, passivate it, and then place it * in the idle object pool. addObject is useful for * "pre-loading" a pool with idle objects (Optional operation). * * @param key the key a new instance should be added to * * @throws Exception * when {@link KeyedPooledObjectFactory#makeObject} fails. * @throws IllegalStateException * after {@link #close} has been called on this pool. * @throws UnsupportedOperationException * when this pool cannot add new idle objects. */ void addObject(K key) throws Exception, IllegalStateException, UnsupportedOperationException; /** * Returns the number of instances corresponding to the given * key currently idle in this pool. Returns a negative value if * this information is not available. * * @param key the key to query * @return the number of instances corresponding to the given * key currently idle in this pool. */ int getNumIdle(K key); /** * Returns the number of instances currently borrowed from but not yet * returned to the pool corresponding to the given key. * Returns a negative value if this information is not available. * * @param key the key to query * @return the number of instances currently borrowed from but not yet * returned to the pool corresponding to the given key. = */ int getNumActive(K key); /** * 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. = */ int getNumIdle(); /** * 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. */ int getNumActive(); /** * Clears the pool, removing all pooled instances (optional operation). * * @throws UnsupportedOperationException when this implementation doesn't * support the operation * * @throws Exception if the pool cannot be cleared */ void clear() throws Exception, UnsupportedOperationException; /** * Clears the specified pool, removing all pooled instances corresponding to * the given key (optional operation). * * @param key the key to clear * * @throws UnsupportedOperationException when this implementation doesn't * support the operation * * @throws Exception if the key cannot be cleared */ void clear(K key) throws Exception, UnsupportedOperationException; /** * Close this pool, and free any resources associated with it. *

* 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. *

*/ @Override void close(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy