    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.pool2;
    018
    019import java.io.Closeable;
    020import java.util.NoSuchElementException;
    021
    022/**
    023 * A pooling simple interface.
    024 * <p>
    025 * Example of use:
    026 * </p>
    027 * <pre style="border:solid thin; padding: 1ex;"
    028 * > Object obj = <code style="color:#00C">null</code>;
    029 *
    030 * <code style="color:#00C">try</code> {
    031 *     obj = pool.borrowObject();
    032 *     <code style="color:#00C">try</code> {
    033 *         <code style="color:#0C0">//...use the object...</code>
    034 *     } <code style="color:#00C">catch</code>(Exception e) {
    035 *         <code style="color:#0C0">// invalidate the object</code>
    036 *         pool.invalidateObject(obj);
    037 *         <code style="color:#0C0">// do not return the object to the pool twice</code>
    038 *         obj = <code style="color:#00C">null</code>;
    039 *     } <code style="color:#00C">finally</code> {
    040 *         <code style="color:#0C0">// make sure the object is returned to the pool</code>
    041 *         <code style="color:#00C">if</code>(<code style="color:#00C">null</code> != obj) {
    042 *             pool.returnObject(obj);
    043 *        }
    044 *     }
    045 * } <code style="color:#00C">catch</code>(Exception e) {
    046 *       <code style="color:#0C0">// failed to borrow an object</code>
    047 * }</pre>
    048 * <p>
    049 * See {@link BaseObjectPool} for a simple base implementation.
    050 * </p>
    051 *
    052 * @param <T> Type of element pooled in this pool.
    053 *
    054 * @see PooledObjectFactory
    055 * @see KeyedObjectPool
    056 * @see BaseObjectPool
    057 *
    058 * @since 2.0
    059 */
    060public interface ObjectPool<T> extends Closeable {
    061
    062    /**
    063     * Creates an object using the {@link PooledObjectFactory factory} or other
    064     * implementation dependent mechanism, passivate it, and then place it in
    065     * the idle object pool. {@code addObject} is useful for "pre-loading"
    066     * a pool with idle objects. (Optional operation).
    067     *
    068     * @throws Exception
    069     *              when {@link PooledObjectFactory#makeObject} fails.
    070     * @throws IllegalStateException
    071     *              after {@link #close} has been called on this pool.
    072     * @throws UnsupportedOperationException
    073     *              when this pool cannot add new idle objects.
    074     */
    075    void addObject() throws Exception, IllegalStateException,
    076            UnsupportedOperationException;
    077
    078    /**
    079     * Calls {@link ObjectPool#addObject()} {@code count}
    080     * number of times.
    081     *
    082     * @param count
    083     *            the number of idle objects to add.
    084     * @throws Exception
    085     *             when {@link ObjectPool#addObject()} fails.
    086     * @since 2.8.0
    087     */
    088    default void addObjects(final int count) throws Exception {
    089        for (int i = 0; i < count; i++) {
    090            addObject();
    091        }
    092    }
    093
    094    /**
    095     * Obtains an instance from this pool.
    096     * <p>
    097     * Instances returned from this method will have been either newly created
    098     * with {@link PooledObjectFactory#makeObject} or will be a previously
    099     * idle object and have been activated with
    100     * {@link PooledObjectFactory#activateObject} and then validated with
    101     * {@link PooledObjectFactory#validateObject}.
    102     * </p>
    103     * <p>
    104     * By contract, clients <strong>must</strong> return the borrowed instance
    105     * using {@link #returnObject}, {@link #invalidateObject}, or a related
    106     * method as defined in an implementation or sub-interface.
    107     * </p>
    108     * <p>
    109     * The behavior of this method when the pool has been exhausted
    110     * is not strictly specified (although it may be specified by
    111     * implementations).
    112     * </p>
    113     *
    114     * @return an instance from this pool.
    115     *
    116     * @throws IllegalStateException
    117     *              after {@link #close close} has been called on this pool.
    118     * @throws Exception
    119     *              when {@link PooledObjectFactory#makeObject} throws an
    120     *              exception.
    121     * @throws NoSuchElementException
    122     *              when the pool is exhausted and cannot or will not return
    123     *              another instance.
    124     */
    125    T borrowObject() throws Exception, NoSuchElementException,
    126            IllegalStateException;
    127
    128    /**
    129     * Clears any objects sitting idle in the pool, releasing any associated
    130     * resources (optional operation). Idle objects cleared must be
    131     * {@link PooledObjectFactory#destroyObject(PooledObject)}.
    132     *
    133     * @throws UnsupportedOperationException
    134     *              if this implementation does not support the operation
    135     *
    136     * @throws Exception if the pool cannot be cleared
    137     */
    138    void clear() throws Exception, UnsupportedOperationException;
    139
    140    /**
    141     * Closes this pool, and free any resources associated with it.
    142     * <p>
    143     * Calling {@link #addObject} or {@link #borrowObject} after invoking this
    144     * method on a pool will cause them to throw an {@link IllegalStateException}.
    145     * </p>
    146     * <p>
    147     * Implementations should silently fail if not all resources can be freed.
    148     * </p>
    149     */
    150    @Override
    151    void close();
    152
    153    /**
    154     * Returns the number of instances currently borrowed from this pool. Returns
    155     * a negative value if this information is not available.
    156     * @return the number of instances currently borrowed from this pool.
    157     */
    158    int getNumActive();
    159
    160    /**
    161     * Returns the number of instances currently idle in this pool. This may be
    162     * considered an approximation of the number of objects that can be
    163     * {@link #borrowObject borrowed} without creating any new instances.
    164     * Returns a negative value if this information is not available.
    165     * @return the number of instances currently idle in this pool.
    166     */
    167    int getNumIdle();
    168
    169    /**
    170     * Invalidates an object from the pool.
    171     * <p>
    172     * By contract, {@code obj} <strong>must</strong> have been obtained
    173     * using {@link #borrowObject} or a related method as defined in an
    174     * implementation or sub-interface.
    175     * </p>
    176     * <p>
    177     * This method should be used when an object that has been borrowed is
    178     * determined (due to an exception or other problem) to be invalid.
    179     * </p>
    180     *
    181     * @param obj a {@link #borrowObject borrowed} instance to be disposed.
    182     *
    183     * @throws Exception if the instance cannot be invalidated
    184     */
    185    void invalidateObject(T obj) throws Exception;
    186
    187    /**
    188     * Returns an instance to the pool. By contract, {@code obj}
    189     * <strong>must</strong> have been obtained using {@link #borrowObject()} or
    190     * a related method as defined in an implementation or sub-interface.
    191     *
    192     * @param obj a {@link #borrowObject borrowed} instance to be returned.
    193     *
    194     * @throws IllegalStateException
    195     *              if an attempt is made to return an object to the pool that
    196     *              is in any state other than allocated (i.e. borrowed).
    197     *              Attempting to return an object more than once or attempting
    198     *              to return an object that was never borrowed from the pool
    199     *              will trigger this exception.
    200     *
    201     * @throws Exception if an instance cannot be returned to the pool
    202     */
    203    void returnObject(T obj) throws Exception;
    204}