ObjectPool.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.pool2;

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

  20. /**
  21.  * A pooling simple interface.
  22.  * <p>
  23.  * Example of use:
  24.  * </p>
  25.  * <pre style="border:solid thin; padding: 1ex;"
  26.  * > Object obj = <code style="color:#00C">null</code>;
  27.  *
  28.  * <code style="color:#00C">try</code> {
  29.  *     obj = pool.borrowObject();
  30.  *     <code style="color:#00C">try</code> {
  31.  *         <code style="color:#0C0">//...use the object...</code>
  32.  *     } <code style="color:#00C">catch</code> (Exception e) {
  33.  *         <code style="color:#0C0">// invalidate the object</code>
  34.  *         pool.invalidateObject(obj);
  35.  *         <code style="color:#0C0">// do not return the object to the pool twice</code>
  36.  *         obj = <code style="color:#00C">null</code>;
  37.  *     } <code style="color:#00C">finally</code> {
  38.  *         <code style="color:#0C0">// make sure the object is returned to the pool</code>
  39.  *         <code style="color:#00C">if</code> (<code style="color:#00C">null</code> != obj) {
  40.  *             pool.returnObject(obj);
  41.  *        }
  42.  *     }
  43.  * } <code style="color:#00C">catch</code>(Exception e) {
  44.  *       <code style="color:#0C0">// failed to borrow an object</code>
  45.  * }</pre>
  46.  * <p>
  47.  * See {@link BaseObjectPool} for a simple base implementation.
  48.  * </p>
  49.  *
  50.  * @param <T> Type of element pooled in this pool.
  51.  *
  52.  *
  53.  * @see PooledObjectFactory
  54.  * @see KeyedObjectPool
  55.  * @see BaseObjectPool
  56.  * @since 2.0
  57.  */
  58. public interface ObjectPool<T> extends Closeable {

  59.     /**
  60.      * Creates an object using the {@link PooledObjectFactory factory} or other
  61.      * implementation dependent mechanism, passivate it, and then place it in
  62.      * the idle object pool. {@code addObject} is useful for "pre-loading"
  63.      * a pool with idle objects. (Optional operation).
  64.      *
  65.      * @throws Exception
  66.      *              when {@link PooledObjectFactory#makeObject} fails.
  67.      * @throws IllegalStateException
  68.      *              after {@link #close} has been called on this pool.
  69.      * @throws UnsupportedOperationException
  70.      *              when this pool cannot add new idle objects.
  71.      */
  72.     void addObject() throws Exception;

  73.     /**
  74.      * Calls {@link ObjectPool#addObject()} {@code count}
  75.      * number of times.
  76.      *
  77.      * @param count
  78.      *            the number of idle objects to add.
  79.      * @throws Exception See {@link ObjectPool#addObject()}.
  80.      * @since 2.8.0
  81.      */
  82.     default void addObjects(final int count) throws Exception {
  83.         for (int i = 0; i < count; i++) {
  84.             addObject();
  85.         }
  86.     }

  87.     /**
  88.      * Borrows an instance from this pool.
  89.      * <p>
  90.      * Instances returned from this method will have been either newly created
  91.      * with {@link PooledObjectFactory#makeObject} or will be a previously
  92.      * idle object and have been activated with
  93.      * {@link PooledObjectFactory#activateObject} and then validated with
  94.      * {@link PooledObjectFactory#validateObject}.
  95.      * </p>
  96.      * <p>
  97.      * By contract, clients <strong>must</strong> return the borrowed instance
  98.      * using {@link #returnObject}, {@link #invalidateObject}, or a related
  99.      * method as defined in an implementation or sub-interface.
  100.      * </p>
  101.      * <p>
  102.      * The behavior of this method when the pool has been exhausted
  103.      * is not strictly specified (although it may be specified by
  104.      * implementations).
  105.      * </p>
  106.      *
  107.      * @return an instance from this pool.
  108.      * @throws IllegalStateException
  109.      *              after {@link #close close} has been called on this pool.
  110.      * @throws Exception
  111.      *              when {@link PooledObjectFactory#makeObject} throws an
  112.      *              exception.
  113.      * @throws NoSuchElementException
  114.      *              when the pool is exhausted and cannot or will not return
  115.      *              another instance.
  116.      */
  117.     T borrowObject() throws Exception;

  118.     /**
  119.      * Clears any objects sitting idle in the pool, releasing any associated
  120.      * resources (optional operation). Idle objects cleared must be
  121.      * {@link PooledObjectFactory#destroyObject(PooledObject)}.
  122.      *
  123.      * @throws UnsupportedOperationException
  124.      *              if this implementation does not support the operation
  125.      *
  126.      * @throws Exception if the pool cannot be cleared
  127.      */
  128.     void clear() throws Exception;

  129.     /**
  130.      * Closes this pool, and free any resources associated with it.
  131.      * <p>
  132.      * Calling {@link #addObject} or {@link #borrowObject} after invoking this
  133.      * method on a pool will cause them to throw an {@link IllegalStateException}.
  134.      * </p>
  135.      * <p>
  136.      * Implementations should silently fail if not all resources can be freed.
  137.      * </p>
  138.      */
  139.     @Override
  140.     void close();

  141.     /**
  142.      * Gets the number of instances currently borrowed from this pool. Returns
  143.      * a negative value if this information is not available.
  144.      * @return the number of instances currently borrowed from this pool.
  145.      */
  146.     int getNumActive();

  147.     /**
  148.      * Gets the number of instances currently idle in this pool. This may be
  149.      * considered an approximation of the number of objects that can be
  150.      * {@link #borrowObject borrowed} without creating any new instances.
  151.      * Returns a negative value if this information is not available.
  152.      * @return the number of instances currently idle in this pool.
  153.      */
  154.     int getNumIdle();

  155.     /**
  156.      * Invalidates an object from the pool.
  157.      * <p>
  158.      * By contract, {@code obj} <strong>must</strong> have been obtained
  159.      * using {@link #borrowObject} or a related method as defined in an
  160.      * implementation or sub-interface.
  161.      * </p>
  162.      * <p>
  163.      * This method should be used when an object that has been borrowed is
  164.      * determined (due to an exception or other problem) to be invalid.
  165.      * </p>
  166.      *
  167.      * @param obj a {@link #borrowObject borrowed} instance to be disposed.
  168.      * @throws Exception if the instance cannot be invalidated
  169.      */
  170.     void invalidateObject(T obj) throws Exception;

  171.     /**
  172.      * Invalidates an object from the pool, using the provided
  173.      * {@link DestroyMode}
  174.      * <p>
  175.      * By contract, {@code obj} <strong>must</strong> have been obtained
  176.      * using {@link #borrowObject} or a related method as defined in an
  177.      * implementation or sub-interface.
  178.      * </p>
  179.      * <p>
  180.      * This method should be used when an object that has been borrowed is
  181.      * determined (due to an exception or other problem) to be invalid.
  182.      * </p>
  183.      *
  184.      * @param obj a {@link #borrowObject borrowed} instance to be disposed.
  185.      * @param destroyMode destroy activation context provided to the factory
  186.      * @throws Exception if the instance cannot be invalidated
  187.      * @since 2.9.0
  188.      */
  189.     default void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception {
  190.         invalidateObject(obj);
  191.     }

  192.     /**
  193.      * Returns an instance to the pool. By contract, {@code obj}
  194.      * <strong>must</strong> have been obtained using {@link #borrowObject()} or
  195.      * a related method as defined in an implementation or sub-interface.
  196.      *
  197.      * @param obj a {@link #borrowObject borrowed} instance to be returned.
  198.      * @throws IllegalStateException
  199.      *              if an attempt is made to return an object to the pool that
  200.      *              is in any state other than allocated (i.e. borrowed).
  201.      *              Attempting to return an object more than once or attempting
  202.      *              to return an object that was never borrowed from the pool
  203.      *              will trigger this exception.
  204.      *
  205.      * @throws Exception if an instance cannot be returned to the pool
  206.      */
  207.     void returnObject(T obj) throws Exception;

  208. }