Home » commons-pool-1.4-src » org.apache.commons » pool » [javadoc | source]
    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   
   18   package org.apache.commons.pool;
   19   
   20   import java.util.NoSuchElementException;
   21   
   22   /**
   23    * A pooling interface.
   24    * <p>
   25    * <code>ObjectPool</code> defines a trivially simple pooling interface. The only 
   26    * required methods are {@link #borrowObject borrowObject}, {@link #returnObject returnObject}
   27    * and {@link #invalidateObject invalidateObject}.
   28    * </p>
   29    * <p>
   30    * Example of use:
   31    * <pre style="border:solid thin; padding: 1ex;"
   32    * > Object obj = <code style="color:#00C">null</code>;
   33    *
   34    * <code style="color:#00C">try</code> {
   35    *     obj = pool.borrowObject();
   36    *     <code style="color:#0C0">//...use the object...</code>
   37    * } <code style="color:#00C">catch</code>(Exception e) {
   38    *     <code style="color:#0C0">// invalidate the object</code>
   39    *     pool.invalidateObject(obj);
   40    *     <code style="color:#0C0">// do not return the object to the pool twice</code>
   41    *     obj = <code style="color:#00C">null</code>;
   42    * } <code style="color:#00C">finally</code> {
   43    *     <code style="color:#0C0">// make sure the object is returned to the pool</code>
   44    *     <code style="color:#00C">if</code>(<code style="color:#00C">null</code> != obj) {
   45    *         pool.returnObject(obj);
   46    *    }
   47    * }</pre>
   48    * </p>
   49    *
   50    * <p>See {@link BaseObjectPool} for a simple base implementation.</p>
   51    *
   52    * @author Rodney Waldhoff
   53    * @author Sandy McArthur
   54    * @version $Revision: 480413 $ $Date: 2006-11-28 22:16:05 -0700 (Tue, 28 Nov 2006) $
   55    * @see PoolableObjectFactory
   56    * @see ObjectPoolFactory
   57    * @see KeyedObjectPool
   58    * @see BaseObjectPool
   59    * @since Pool 1.0
   60    */
   61   public interface ObjectPool {
   62       /**
   63        * Obtains an instance from this pool.
   64        * <p>
   65        * Instances returned from this method will have been either newly created with
   66        * {@link PoolableObjectFactory#makeObject makeObject} or will be a previously idle object and
   67        * have been activated with {@link PoolableObjectFactory#activateObject activateObject} and
   68        * then validated with {@link PoolableObjectFactory#validateObject validateObject}.
   69        * </p>
   70        * <p>
   71        * By contract, clients <strong>must</strong> return the borrowed instance using
   72        * {@link #returnObject returnObject}, {@link #invalidateObject invalidateObject}, or a related method
   73        * as defined in an implementation or sub-interface.
   74        * </p>
   75        * <p>
   76        * The behaviour of this method when the pool has been exhausted
   77        * is not strictly specified (although it may be specified by implementations).
   78        * Older versions of this method would return <code>null</code> to indicate exhaustion,
   79        * newer versions are encouraged to throw a {@link NoSuchElementException}.
   80        * </p>
   81        *
   82        * @return an instance from this pool.
   83        * @throws IllegalStateException after {@link #close close} has been called on this pool.
   84        * @throws Exception when {@link PoolableObjectFactory#makeObject makeObject} throws an exception.
   85        * @throws NoSuchElementException when the pool is exhausted and cannot or will not return another instance.
   86        */
   87       Object borrowObject() throws Exception, NoSuchElementException, IllegalStateException;
   88   
   89       /**
   90        * Return an instance to the pool.
   91        * By contract, <code>obj</code> <strong>must</strong> have been obtained
   92        * using {@link #borrowObject() borrowObject}
   93        * or a related method as defined in an implementation
   94        * or sub-interface.
   95        *
   96        * @param obj a {@link #borrowObject borrowed} instance to be returned.
   97        * @throws Exception <b>deprecated</b>: as of Pool 2.0 pool implementations should swallow
   98        * exceptions that occur when a poolable object is returned. For future source compatability
   99        * implementations of this method should not even declare that they throw any exception.
  100        */
  101       void returnObject(Object obj) throws Exception;
  102   
  103       /**
  104        * Invalidates an object from the pool
  105        * By contract, <code>obj</code> <strong>must</strong> have been obtained
  106        * using {@link #borrowObject borrowObject}
  107        * or a related method as defined in an implementation
  108        * or sub-interface.
  109        * <p>
  110        * This method should be used when an object that has been borrowed
  111        * is determined (due to an exception or other problem) to be invalid.
  112        * </p>
  113        *
  114        * @param obj a {@link #borrowObject borrowed} instance to be disposed.
  115        * @throws Exception <b>deprecated</b>: as of Pool 2.0 pool implementations should swallow
  116        * exceptions that occur when a poolable object is returned. For future source compatability
  117        * implementations of this method should not even declare that they throw any exception.
  118        */
  119       void invalidateObject(Object obj) throws Exception;
  120   
  121       /**
  122        * Create an object using the {@link PoolableObjectFactory factory} or other
  123        * implementation dependent mechanism, passivate it, and then place it in the idle object pool.
  124        * <code>addObject</code> is useful for "pre-loading" a pool with idle objects.
  125        * (Optional operation).
  126        *
  127        * @throws Exception when {@link PoolableObjectFactory#makeObject} fails.
  128        * @throws IllegalStateException after {@link #close} has been called on this pool.
  129        * @throws UnsupportedOperationException when this pool cannot add new idle objects.
  130        */
  131       void addObject() throws Exception, IllegalStateException, UnsupportedOperationException;
  132   
  133       /**
  134        * Return the number of instances
  135        * currently idle in this pool (optional operation).
  136        * This may be considered an approximation of the number
  137        * of objects that can be {@link #borrowObject borrowed}
  138        * without creating any new instances.
  139        * Returns a negative value if this information is not available.
  140        *
  141        * @return the number of instances currently idle in this pool or a negative value if unsupported
  142        * @throws UnsupportedOperationException <strong>deprecated</strong>: if this implementation does not support the operation
  143        */
  144       int getNumIdle() throws UnsupportedOperationException;
  145   
  146       /**
  147        * Return the number of instances
  148        * currently borrowed from this pool
  149        * (optional operation).
  150        * Returns a negative value if this information is not available.
  151        *
  152        * @return the number of instances currently borrowed from this pool or a negative value if unsupported
  153        * @throws UnsupportedOperationException <strong>deprecated</strong>: if this implementation does not support the operation
  154        */
  155       int getNumActive() throws UnsupportedOperationException;
  156   
  157       /**
  158        * Clears any objects sitting idle in the pool, releasing any
  159        * associated resources (optional operation).
  160        * Idle objects cleared must be {@link PoolableObjectFactory#destroyObject(Object) destroyed}.
  161        *
  162        * @throws UnsupportedOperationException if this implementation does not support the operation
  163        */
  164       void clear() throws Exception, UnsupportedOperationException;
  165   
  166       /**
  167        * Close this pool, and free any resources associated with it.
  168        * <p>
  169        * Calling {@link #addObject} or {@link #borrowObject} after invoking
  170        * this method on a pool will cause them to throw an
  171        * {@link IllegalStateException}.
  172        * </p>
  173        *
  174        * @throws Exception <strong>deprecated</strong>: implementations should silently fail if not all resources can be freed.
  175        */
  176       void close() throws Exception;
  177   
  178       /**
  179        * Sets the {@link PoolableObjectFactory factory} this pool uses
  180        * to create new instances (optional operation). Trying to change
  181        * the <code>factory</code> after a pool has been used will frequently
  182        * throw an {@link UnsupportedOperationException}. It is up to the pool
  183        * implementation to determine when it is acceptable to call this method.
  184        *
  185        * @param factory the {@link PoolableObjectFactory} used to create new instances.
  186        * @throws IllegalStateException when the factory cannot be set at this time
  187        * @throws UnsupportedOperationException if this implementation does not support the operation
  188        */
  189       void setFactory(PoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException;
  190   }

Save This Page
Home » commons-pool-1.4-src » org.apache.commons » pool » [javadoc | source]