PooledObject.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.PrintWriter;
  19. import java.time.Duration;
  20. import java.time.Instant;
  21. import java.util.Deque;

  22. /**
  23.  * Defines the wrapper that is used to track the additional information, such as
  24.  * state, for the pooled objects.
  25.  * <p>
  26.  * Implementations of this class are required to be thread-safe.
  27.  * </p>
  28.  *
  29.  * @param <T> the type of object in the pool.
  30.  * @since 2.0
  31.  */
  32. public interface PooledObject<T> extends Comparable<PooledObject<T>> {

  33.     /**
  34.      * Tests whether the given PooledObject is null <em>or</em> contains a null.
  35.      *
  36.      * @param pooledObject the PooledObject to test.
  37.      * @return whether the given PooledObject is null <em>or</em> contains a null.
  38.      * @since 2.12.0
  39.      */
  40.     static boolean isNull(final PooledObject<?> pooledObject) {
  41.         return pooledObject == null || pooledObject.getObject() == null;
  42.     }

  43.     /**
  44.      * Allocates the object.
  45.      *
  46.      * @return {@code true} if the original state was {@link PooledObjectState#IDLE IDLE}
  47.      */
  48.     boolean allocate();

  49.     /**
  50.      * Orders instances based on idle time - i.e. the length of time since the
  51.      * instance was returned to the pool. Used by the GKOP idle object evictor.
  52.      * <p>
  53.      * Note: This class has a natural ordering that is inconsistent with
  54.      *       equals if distinct objects have the same identity hash code.
  55.      * </p>
  56.      * <p>
  57.      * {@inheritDoc}
  58.      * </p>
  59.      */
  60.     @Override
  61.     int compareTo(PooledObject<T> other);

  62.     /**
  63.      * Deallocates the object and sets it {@link PooledObjectState#IDLE IDLE}
  64.      * if it is currently {@link PooledObjectState#ALLOCATED ALLOCATED}.
  65.      *
  66.      * @return {@code true} if the state was {@link PooledObjectState#ALLOCATED ALLOCATED}.
  67.      */
  68.     boolean deallocate();

  69.     /**
  70.      * Notifies the object that the eviction test has ended.
  71.      *
  72.      * @param idleQueue The queue of idle objects to which the object should be
  73.      *                  returned.
  74.      * @return  Currently not used.
  75.      */
  76.     boolean endEvictionTest(Deque<PooledObject<T>> idleQueue);

  77.     @Override
  78.     boolean equals(Object obj);

  79.     /**
  80.      * Gets the amount of time this object last spent in the active state (it may still be active in which case
  81.      * subsequent calls will return an increased value).
  82.      *
  83.      * @return The duration last spent in the active state.
  84.      * @since 2.11.0
  85.      */
  86.     default Duration getActiveDuration() {
  87.         // Take copies to avoid threading issues
  88.         final Instant lastReturnInstant = getLastReturnInstant();
  89.         final Instant lastBorrowInstant = getLastBorrowInstant();
  90.         // @formatter:off
  91.         return lastReturnInstant.isAfter(lastBorrowInstant) ?
  92.                 Duration.between(lastBorrowInstant, lastReturnInstant) :
  93.                 Duration.between(lastBorrowInstant, Instant.now());
  94.         // @formatter:on
  95.     }

  96.     /**
  97.      * Gets the amount of time this object last spent in the active state (it may still be active in which case
  98.      * subsequent calls will return an increased value).
  99.      *
  100.      * @return The duration last spent in the active state.
  101.      * @since 2.10.0
  102.      * @deprecated Use {@link #getActiveDuration()}.
  103.      */
  104.     @Deprecated
  105.     default Duration getActiveTime() {
  106.         return getActiveDuration();
  107.     }

  108.     /**
  109.      * Gets the amount of time in milliseconds this object last spent in the
  110.      * active state (it may still be active in which case subsequent calls will
  111.      * return an increased value).
  112.      *
  113.      * @return The time in milliseconds last spent in the active state.
  114.      * @deprecated Use {@link #getActiveTime()} which offers the best precision.
  115.      */
  116.     @Deprecated
  117.     long getActiveTimeMillis();

  118.     /**
  119.      * Gets the number of times this object has been borrowed.
  120.      *
  121.      * @return -1 by default for implementations prior to release 2.7.0.
  122.      * @since 2.7.0
  123.      */
  124.     default long getBorrowedCount() {
  125.         return -1;
  126.     }

  127.     /**
  128.      * Gets the time (using the same basis as {@link Instant#now()}) that this object was created.
  129.      *
  130.      * @return The creation time for the wrapped object.
  131.      * @since 2.11.0
  132.      */
  133.     default Instant getCreateInstant() {
  134.         return Instant.ofEpochMilli(getCreateTime());
  135.     }

  136.     /**
  137.      * Gets the time (using the same basis as
  138.      * {@link java.time.Clock#instant()}) that this object was created.
  139.      *
  140.      * @return The creation time for the wrapped object.
  141.      * @deprecated Use {@link #getCreateInstant()} which offers the best precision.
  142.      */
  143.     @Deprecated
  144.     long getCreateTime();

  145.     /**
  146.      * Gets the duration since this object was created (using {@link Instant#now()}).
  147.      *
  148.      * @return The duration since this object was created.
  149.      * @since 2.12.0
  150.      */
  151.     default Duration getFullDuration() {
  152.         return Duration.between(getCreateInstant(), Instant.now());
  153.     }

  154.     /**
  155.      * Gets the amount of time that this object last spend in the
  156.      * idle state (it may still be idle in which case subsequent calls will
  157.      * return an increased value).
  158.      *
  159.      * @return The amount of time in last spent in the idle state.
  160.      * @since 2.11.0
  161.      */
  162.     default Duration getIdleDuration() {
  163.         return Duration.ofMillis(getIdleTimeMillis());
  164.     }

  165.     /**
  166.      * Gets the amount of time that this object last spend in the
  167.      * idle state (it may still be idle in which case subsequent calls will
  168.      * return an increased value).
  169.      *
  170.      * @return The amount of time in last spent in the idle state.
  171.      * @since 2.10.0
  172.      * @deprecated Use {@link #getIdleDuration()}.
  173.      */
  174.     @Deprecated
  175.     default Duration getIdleTime() {
  176.         return Duration.ofMillis(getIdleTimeMillis());
  177.     }

  178.     /**
  179.      * Gets the amount of time in milliseconds that this object last spend in the
  180.      * idle state (it may still be idle in which case subsequent calls will
  181.      * return an increased value).
  182.      *
  183.      * @return The time in milliseconds last spent in the idle state.
  184.      * @deprecated Use {@link #getIdleTime()} which offers the best precision.
  185.      */
  186.     @Deprecated
  187.     long getIdleTimeMillis();

  188.     /**
  189.      * Gets the time the wrapped object was last borrowed.
  190.      *
  191.      * @return The time the object was last borrowed.
  192.      * @since 2.11.0
  193.      */
  194.     default Instant getLastBorrowInstant() {
  195.         return Instant.ofEpochMilli(getLastBorrowTime());
  196.     }

  197.     /**
  198.      * Gets the time the wrapped object was last borrowed.
  199.      *
  200.      * @return The time the object was last borrowed.
  201.      * @deprecated Use {@link #getLastBorrowInstant()} which offers the best precision.
  202.      */
  203.     @Deprecated
  204.     long getLastBorrowTime();

  205.     /**
  206.      * Gets the time the wrapped object was last borrowed.
  207.      *
  208.      * @return The time the object was last borrowed.
  209.      * @since 2.11.0
  210.      */
  211.     default Instant getLastReturnInstant() {
  212.         return Instant.ofEpochMilli(getLastReturnTime());
  213.     }

  214.     /**
  215.      * Gets the time the wrapped object was last returned.
  216.      *
  217.      * @return The time the object was last returned.
  218.      * @deprecated Use {@link #getLastReturnInstant()} which offers the best precision.
  219.      */
  220.     @Deprecated
  221.     long getLastReturnTime();

  222.     /**
  223.      * Gets an estimate of the last time this object was used. If the class of the pooled object implements
  224.      * {@link TrackedUse}, what is returned is the maximum of {@link TrackedUse#getLastUsedInstant()} and
  225.      * {@link #getLastBorrowTime()}; otherwise this method gives the same value as {@link #getLastBorrowTime()}.
  226.      *
  227.      * @return the last time this object was used
  228.      * @since 2.11.0
  229.      */
  230.     default Instant getLastUsedInstant() {
  231.         return Instant.ofEpochMilli(getLastUsedTime());
  232.     }

  233.     /**
  234.      * Gets an estimate of the last time this object was used.  If the class
  235.      * of the pooled object implements {@link TrackedUse}, what is returned is
  236.      * the maximum of {@link TrackedUse#getLastUsedInstant()} and
  237.      * {@link #getLastBorrowTime()}; otherwise this method gives the same
  238.      * value as {@link #getLastBorrowTime()}.
  239.      *
  240.      * @return the last time this object was used.
  241.      * @deprecated Use {@link #getLastUsedInstant()} which offers the best precision.
  242.      */
  243.     @Deprecated
  244.     long getLastUsedTime();

  245.     /**
  246.      * Gets the underlying object that is wrapped by this instance of
  247.      * {@link PooledObject}.
  248.      *
  249.      * @return The wrapped object.
  250.      */
  251.     T getObject();

  252.     /**
  253.      * Gets the state of this object.
  254.      *
  255.      * @return state
  256.      */
  257.     PooledObjectState getState();

  258.     @Override
  259.     int hashCode();

  260.     /**
  261.      * Sets the state to {@link PooledObjectState#INVALID INVALID}.
  262.      */
  263.     void invalidate();

  264.     /**
  265.      * Marks the pooled object as abandoned.
  266.      */
  267.     void markAbandoned();

  268.     /**
  269.      * Marks the object as returning to the pool.
  270.      */
  271.     void markReturning();

  272.     /**
  273.      * Prints the stack trace of the code that borrowed this pooled object and
  274.      * the stack trace of the last code to use this object (if available) to
  275.      * the supplied writer.
  276.      *
  277.      * @param   writer  The destination for the debug output.
  278.      */
  279.     void printStackTrace(PrintWriter writer);

  280.     /**
  281.      * Sets whether to use abandoned object tracking. If this is true the
  282.      * implementation will need to record the stack trace of the last caller to
  283.      * borrow this object.
  284.      *
  285.      * @param   logAbandoned    The new configuration setting for abandoned
  286.      *                          object tracking.
  287.      */
  288.     void setLogAbandoned(boolean logAbandoned);

  289.     /**
  290.      * Sets the stack trace generation strategy based on whether or not fully detailed stack traces are required.
  291.      * When set to false, abandoned logs may only include caller class information rather than method names, line
  292.      * numbers, and other normal metadata available in a full stack trace.
  293.      *
  294.      * @param requireFullStackTrace the new configuration setting for abandoned object logging.
  295.      * @since 2.7.0
  296.      */
  297.     default void setRequireFullStackTrace(final boolean requireFullStackTrace) {
  298.         // noop
  299.     }

  300.     /**
  301.      * Attempts to place the pooled object in the
  302.      * {@link PooledObjectState#EVICTION} state.
  303.      *
  304.      * @return {@code true} if the object was placed in the
  305.      *         {@link PooledObjectState#EVICTION} state otherwise
  306.      *         {@code false}.
  307.      */
  308.     boolean startEvictionTest();

  309.     /**
  310.      * Gets a String form of the wrapper for debug purposes. The format is
  311.      * not fixed and may change at any time.
  312.      *
  313.      * {@inheritDoc}
  314.      */
  315.     @Override
  316.     String toString();

  317.     /**
  318.      * Records the current stack trace as the last time the object was used.
  319.      */
  320.     void use();

  321. }