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

  71.   /**
  72.    * Reinitializes an instance to be returned by the pool.
  73.    *
  74.    * @param p a {@code PooledObject} wrapping the instance to be activated
  75.    * @throws Exception if there is a problem activating {@code obj},
  76.    *    this exception may be swallowed by the pool.
  77.    *
  78.    * @see #destroyObject
  79.    */
  80.   void activateObject(PooledObject<T> p) throws Exception;

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

  102.   /**
  103.    * Destroys an instance no longer needed by the pool, using the provided
  104.    * DestroyMode.
  105.    *
  106.    * @param p a {@code PooledObject} wrapping the instance to be destroyed
  107.    * @param destroyMode DestroyMode providing context to the factory
  108.    * @throws Exception should be avoided as it may be swallowed by
  109.    *    the pool implementation.
  110.    *
  111.    * @see #validateObject
  112.    * @see ObjectPool#invalidateObject
  113.    * @see #destroyObject(PooledObject)
  114.    * @see DestroyMode
  115.    * @since 2.9.0
  116.    */
  117.   default void destroyObject(final PooledObject<T> p, final DestroyMode destroyMode) throws Exception {
  118.       destroyObject(p);
  119.   }

  120.   /**
  121.    * Creates an instance that can be served by the pool and wrap it in a
  122.    * {@link PooledObject} to be managed by the pool.
  123.    *
  124.    * @return a {@code PooledObject} wrapping an instance that can be served by the pool, not null.
  125.    * @throws Exception if there is a problem creating a new instance,
  126.    *    this will be propagated to the code requesting an object.
  127.    */
  128.   PooledObject<T> makeObject() throws Exception;

  129.   /**
  130.    * Uninitializes an instance to be returned to the idle object pool.
  131.    *
  132.    * @param p a {@code PooledObject} wrapping the instance to be passivated
  133.    * @throws Exception if there is a problem passivating {@code obj},
  134.    *    this exception may be swallowed by the pool.
  135.    *
  136.    * @see #destroyObject
  137.    */
  138.   void passivateObject(PooledObject<T> p) throws Exception;

  139.   /**
  140.    * Ensures that the instance is safe to be returned by the pool.
  141.    *
  142.    * @param p a {@code PooledObject} wrapping the instance to be validated
  143.    * @return {@code false} if {@code obj} is not valid and should
  144.    *         be dropped from the pool, {@code true} otherwise.
  145.    */
  146.   boolean validateObject(PooledObject<T> p);
  147. }