    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 *
    055 * @see PooledObjectFactory
    056 * @see KeyedObjectPool
    057 * @see BaseObjectPool
    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;
    076
    077    /**
    078     * Calls {@link ObjectPool#addObject()} {@code count}
    079     * number of times.
    080     *
    081     * @param count
    082     *            the number of idle objects to add.
    083     * @throws Exception See {@link ObjectPool#addObject()}.
    084     * @since 2.8.0
    085     */
    086    default void addObjects(final int count) throws Exception {
    087        for (int i = 0; i < count; i++) {
    088            addObject();
    089        }
    090    }
    091
    092    /**
    093     * Borrows an instance from this pool.
    094     * <p>
    095     * Instances returned from this method will have been either newly created
    096     * with {@link PooledObjectFactory#makeObject} or will be a previously
    097     * idle object and have been activated with
    098     * {@link PooledObjectFactory#activateObject} and then validated with
    099     * {@link PooledObjectFactory#validateObject}.
    100     * </p>
    101     * <p>
    102     * By contract, clients <strong>must</strong> return the borrowed instance
    103     * using {@link #returnObject}, {@link #invalidateObject}, or a related
    104     * method as defined in an implementation or sub-interface.
    105     * </p>
    106     * <p>
    107     * The behavior of this method when the pool has been exhausted
    108     * is not strictly specified (although it may be specified by
    109     * implementations).
    110     * </p>
    111     *
    112     * @return an instance from this pool.
    113     * @throws IllegalStateException
    114     *              after {@link #close close} has been called on this pool.
    115     * @throws Exception
    116     *              when {@link PooledObjectFactory#makeObject} throws an
    117     *              exception.
    118     * @throws NoSuchElementException
    119     *              when the pool is exhausted and cannot or will not return
    120     *              another instance.
    121     */
    122    T borrowObject() throws Exception;
    123
    124    /**
    125     * Clears any objects sitting idle in the pool, releasing any associated
    126     * resources (optional operation). Idle objects cleared must be
    127     * {@link PooledObjectFactory#destroyObject(PooledObject)}.
    128     *
    129     * @throws UnsupportedOperationException
    130     *              if this implementation does not support the operation
    131     *
    132     * @throws Exception if the pool cannot be cleared
    133     */
    134    void clear() throws Exception;
    135
    136    /**
    137     * Closes this pool, and free any resources associated with it.
    138     * <p>
    139     * Calling {@link #addObject} or {@link #borrowObject} after invoking this
    140     * method on a pool will cause them to throw an {@link IllegalStateException}.
    141     * </p>
    142     * <p>
    143     * Implementations should silently fail if not all resources can be freed.
    144     * </p>
    145     */
    146    @Override
    147    void close();
    148
    149    /**
    150     * Gets the number of instances currently borrowed from this pool. Returns
    151     * a negative value if this information is not available.
    152     * @return the number of instances currently borrowed from this pool.
    153     */
    154    int getNumActive();
    155
    156    /**
    157     * Gets the number of instances currently idle in this pool. This may be
    158     * considered an approximation of the number of objects that can be
    159     * {@link #borrowObject borrowed} without creating any new instances.
    160     * Returns a negative value if this information is not available.
    161     * @return the number of instances currently idle in this pool.
    162     */
    163    int getNumIdle();
    164
    165    /**
    166     * Invalidates an object from the pool.
    167     * <p>
    168     * By contract, {@code obj} <strong>must</strong> have been obtained
    169     * using {@link #borrowObject} or a related method as defined in an
    170     * implementation or sub-interface.
    171     * </p>
    172     * <p>
    173     * This method should be used when an object that has been borrowed is
    174     * determined (due to an exception or other problem) to be invalid.
    175     * </p>
    176     *
    177     * @param obj a {@link #borrowObject borrowed} instance to be disposed.
    178     * @throws Exception if the instance cannot be invalidated
    179     */
    180    void invalidateObject(T obj) throws Exception;
    181
    182    /**
    183     * Invalidates an object from the pool, using the provided
    184     * {@link DestroyMode}
    185     * <p>
    186     * By contract, {@code obj} <strong>must</strong> have been obtained
    187     * using {@link #borrowObject} or a related method as defined in an
    188     * implementation or sub-interface.
    189     * </p>
    190     * <p>
    191     * This method should be used when an object that has been borrowed is
    192     * determined (due to an exception or other problem) to be invalid.
    193     * </p>
    194     *
    195     * @param obj a {@link #borrowObject borrowed} instance to be disposed.
    196     * @param destroyMode destroy activation context provided to the factory
    197     * @throws Exception if the instance cannot be invalidated
    198     * @since 2.9.0
    199     */
    200    default void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception {
    201        invalidateObject(obj);
    202    }
    203
    204    /**
    205     * Returns an instance to the pool. By contract, {@code obj}
    206     * <strong>must</strong> have been obtained using {@link #borrowObject()} or
    207     * a related method as defined in an implementation or sub-interface.
    208     *
    209     * @param obj a {@link #borrowObject borrowed} instance to be returned.
    210     * @throws IllegalStateException
    211     *              if an attempt is made to return an object to the pool that
    212     *              is in any state other than allocated (i.e. borrowed).
    213     *              Attempting to return an object more than once or attempting
    214     *              to return an object that was never borrowed from the pool
    215     *              will trigger this exception.
    216     *
    217     * @throws Exception if an instance cannot be returned to the pool
    218     */
    219    void returnObject(T obj) throws Exception;
    220
    221}