KeyedPooledObjectFactory.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. /**
  19.  * An interface defining life-cycle methods for
  20.  * instances to be served by a {@link KeyedObjectPool}.
  21.  * <p>
  22.  * By contract, when an {@link KeyedObjectPool}
  23.  * delegates to a {@link KeyedPooledObjectFactory},
  24.  * </p>
  25.  * <ol>
  26.  *  <li>
  27.  *   {@link #makeObject} is called whenever a new instance is needed.
  28.  *  </li>
  29.  *  <li>
  30.  *   {@link #activateObject} is invoked on every instance that has been
  31.  *   {@link #passivateObject passivated} before it is
  32.  *   {@link KeyedObjectPool#borrowObject borrowed} from the pool.
  33.  *  </li>
  34.  *  <li>
  35.  *   {@link #validateObject} may be invoked on {@link #activateObject activated}
  36.  *   instances to make sure they can be
  37.  *   {@link KeyedObjectPool#borrowObject borrowed} from the pool.
  38.  *   {@code validateObject} may also be used to test an
  39.  *   instance being {@link KeyedObjectPool#returnObject returned} to the pool
  40.  *   before it is {@link #passivateObject passivated}. It will only be invoked
  41.  *   on an activated instance.
  42.  *  </li>
  43.  *  <li>
  44.  *   {@link #passivateObject passivateObject}
  45.  *   is invoked on every instance when it is returned to the pool.
  46.  *  </li>
  47.  *  <li>
  48.  *   {@link #destroyObject destroyObject}
  49.  *   is invoked on every instance when it is being "dropped" from the
  50.  *   pool (whether due to the response from {@code validateObject},
  51.  *   or for reasons specific to the pool implementation.) There is no
  52.  *   guarantee that the instance being destroyed will
  53.  *   be considered active, passive or in a generally consistent state.
  54.  *  </li>
  55.  * </ol>
  56.  * {@link KeyedPooledObjectFactory} must be thread-safe. The only promise
  57.  * an {@link KeyedObjectPool} makes is that the same instance of an object will
  58.  * not be passed to more than one method of a
  59.  * {@code KeyedPooledObjectFactory} at a time.
  60.  * <p>
  61.  * While clients of a {@link KeyedObjectPool} borrow and return instances of
  62.  * the underlying value type V, the factory methods act on instances of
  63.  * {@link PooledObject PooledObject&lt;V&gt;}.  These are the object wrappers that
  64.  * pools use to track and maintain state informations about the objects that
  65.  * they manage.
  66.  * </p>
  67.  *
  68.  * @see KeyedObjectPool
  69.  * @see BaseKeyedPooledObjectFactory
  70.  * @param <K> The type of keys managed by this factory.
  71.  * @param <V> Type of element managed by this factory.
  72.  * @since 2.0
  73.  */
  74. public interface KeyedPooledObjectFactory<K, V> {

  75.     /**
  76.      * Reinitializes an instance to be returned by the pool.
  77.      *
  78.      * @param key the key used when selecting the object
  79.      * @param p a {@code PooledObject} wrapping the instance to be activated
  80.      * @throws Exception if there is a problem activating {@code obj},
  81.      *    this exception may be swallowed by the pool.
  82.      *
  83.      * @see #destroyObject
  84.      */
  85.     void activateObject(K key, PooledObject<V> p) throws Exception;

  86.     /**
  87.      * Destroys an instance no longer needed by the pool.
  88.      * <p>
  89.      * It is important for implementations of this method to be aware that there
  90.      * is no guarantee about what state {@code obj} will be in and the
  91.      * implementation should be prepared to handle unexpected errors.
  92.      * </p>
  93.      * <p>
  94.      * Also, an implementation must take in to consideration that instances lost
  95.      * to the garbage collector may never be destroyed.
  96.      * </p>
  97.      *
  98.      * @param key the key used when selecting the instance
  99.      * @param p a {@code PooledObject} wrapping the instance to be destroyed
  100.      * @throws Exception should be avoided as it may be swallowed by
  101.      *    the pool implementation.
  102.      *
  103.      * @see #validateObject
  104.      * @see KeyedObjectPool#invalidateObject
  105.      */
  106.     void destroyObject(K key, PooledObject<V> p) throws Exception;

  107.     /**
  108.      * Destroys an instance no longer needed by the pool, using the provided {@link DestroyMode}.
  109.      *
  110.      * @param key the key used when selecting the instance
  111.      * @param p a {@code PooledObject} wrapping the instance to be destroyed
  112.      * @param destroyMode DestroyMode providing context to the factory
  113.      * @throws Exception should be avoided as it may be swallowed by
  114.      *    the pool implementation.
  115.      *
  116.      * @see #validateObject
  117.      * @see KeyedObjectPool#invalidateObject
  118.      * @see #destroyObject(Object, PooledObject)
  119.      * @see DestroyMode
  120.      * @since 2.9.0
  121.      */
  122.     default void destroyObject(final K key, final PooledObject<V> p, final DestroyMode destroyMode) throws Exception {
  123.         destroyObject(key, p);
  124.     }

  125.     /**
  126.      * Creates an instance that can be served by the pool and
  127.      * wrap it in a {@link PooledObject} to be managed by the pool.
  128.      *
  129.      * @param key the key used when constructing the object
  130.      * @return a {@code PooledObject} wrapping an instance that can
  131.      * be served by the pool.
  132.      *
  133.      * @throws Exception if there is a problem creating a new instance,
  134.      *    this will be propagated to the code requesting an object.
  135.      */
  136.     PooledObject<V> makeObject(K key) throws Exception;

  137.     /**
  138.      * Uninitializes an instance to be returned to the idle object pool.
  139.      *
  140.      * @param key the key used when selecting the object
  141.      * @param p a {@code PooledObject} wrapping the instance to be passivated
  142.      * @throws Exception if there is a problem passivating {@code obj},
  143.      *    this exception may be swallowed by the pool.
  144.      *
  145.      * @see #destroyObject
  146.      */
  147.     void passivateObject(K key, PooledObject<V> p) throws Exception;

  148.     /**
  149.      * Ensures that the instance is safe to be returned by the pool.
  150.      *
  151.      * @param key the key used when selecting the object
  152.      * @param p a {@code PooledObject} wrapping the instance to be validated
  153.      * @return {@code false} if {@code obj} is not valid and should
  154.      *         be dropped from the pool, {@code true} otherwise.
  155.      */
  156.     boolean validateObject(K key, PooledObject<V> p);
  157. }