Class PoolUtils


  • public final class PoolUtils
    extends java.lang.Object
    This class consists exclusively of static methods that operate on or return ObjectPool or KeyedObjectPool related interfaces.
    Since:
    2.0
    • Constructor Detail

      • PoolUtils

        public PoolUtils()
        PoolUtils instances should NOT be constructed in standard programming. Instead, the class should be used procedurally: PoolUtils.adapt(aPool);. This constructor is public to permit tools that require a JavaBean instance to operate.
    • Method Detail

      • checkRethrow

        public static void checkRethrow​(java.lang.Throwable t)
        Should the supplied Throwable be re-thrown (eg if it is an instance of one of the Throwables that should never be swallowed). Used by the pool error handling for operations that throw exceptions that normally need to be ignored.
        Parameters:
        t - The Throwable to check
        Throws:
        java.lang.ThreadDeath - if that is passed in
        java.lang.VirtualMachineError - if that is passed in
      • checkMinIdle

        public static <T> java.util.TimerTask checkMinIdle​(ObjectPool<T> pool,
                                                           int minIdle,
                                                           long period)
                                                    throws java.lang.IllegalArgumentException
        Periodically check the idle object count for the pool. At most one idle object will be added per period. If there is an exception when calling ObjectPool.addObject() then no more checks will be performed.
        Type Parameters:
        T - the type of objects in the pool
        Parameters:
        pool - the pool to check periodically.
        minIdle - if the ObjectPool.getNumIdle() is less than this then add an idle object.
        period - the frequency to check the number of idle objects in a pool, see Timer.schedule(TimerTask, long, long).
        Returns:
        the TimerTask that will periodically check the pools idle object count.
        Throws:
        java.lang.IllegalArgumentException - when pool is null or when minIdle is negative or when period isn't valid for Timer.schedule(TimerTask, long, long)
      • checkMinIdle

        public static <K,​V> java.util.TimerTask checkMinIdle​(KeyedObjectPool<K,​V> keyedPool,
                                                                   K key,
                                                                   int minIdle,
                                                                   long period)
                                                            throws java.lang.IllegalArgumentException
        Periodically check the idle object count for the key in the keyedPool. At most one idle object will be added per period. If there is an exception when calling KeyedObjectPool.addObject(Object) then no more checks for that key will be performed.
        Type Parameters:
        K - the type of the pool key
        V - the type of pool entries
        Parameters:
        keyedPool - the keyedPool to check periodically.
        key - the key to check the idle count of.
        minIdle - if the KeyedObjectPool.getNumIdle(Object) is less than this then add an idle object.
        period - the frequency to check the number of idle objects in a keyedPool, see Timer.schedule(TimerTask, long, long).
        Returns:
        the TimerTask that will periodically check the pools idle object count.
        Throws:
        java.lang.IllegalArgumentException - when keyedPool, key is null or when minIdle is negative or when period isn't valid for Timer.schedule(TimerTask, long, long).
      • checkMinIdle

        public static <K,​V> java.util.Map<K,​java.util.TimerTask> checkMinIdle​(KeyedObjectPool<K,​V> keyedPool,
                                                                                          java.util.Collection<K> keys,
                                                                                          int minIdle,
                                                                                          long period)
                                                                                   throws java.lang.IllegalArgumentException
        Periodically check the idle object count for each key in the Collection keys in the keyedPool. At most one idle object will be added per period.
        Type Parameters:
        K - the type of the pool key
        V - the type of pool entries
        Parameters:
        keyedPool - the keyedPool to check periodically.
        keys - a collection of keys to check the idle object count.
        minIdle - if the KeyedObjectPool.getNumIdle(Object) is less than this then add an idle object.
        period - the frequency to check the number of idle objects in a keyedPool, see Timer.schedule(TimerTask, long, long).
        Returns:
        a Map of key and TimerTask pairs that will periodically check the pools idle object count.
        Throws:
        java.lang.IllegalArgumentException - when keyedPool, keys, or any of the values in the collection is null or when minIdle is negative or when period isn't valid for Timer.schedule(TimerTask, long, long).
        See Also:
        checkMinIdle(KeyedObjectPool, Object, int, long)
      • prefill

        @Deprecated
        public static <T> void prefill​(ObjectPool<T> pool,
                                       int count)
                                throws java.lang.Exception,
                                       java.lang.IllegalArgumentException
        Deprecated.
        Calls ObjectPool.addObject() on pool count number of times.
        Type Parameters:
        T - the type of objects in the pool
        Parameters:
        pool - the pool to prefill.
        count - the number of idle objects to add.
        Throws:
        java.lang.Exception - when ObjectPool.addObject() fails.
        java.lang.IllegalArgumentException - when pool is null.
      • prefill

        @Deprecated
        public static <K,​V> void prefill​(KeyedObjectPool<K,​V> keyedPool,
                                               K key,
                                               int count)
                                        throws java.lang.Exception,
                                               java.lang.IllegalArgumentException
        Calls KeyedObjectPool.addObject(Object) on keyedPool with key count number of times.
        Type Parameters:
        K - the type of the pool key
        V - the type of pool entries
        Parameters:
        keyedPool - the keyedPool to prefill.
        key - the key to add objects for.
        count - the number of idle objects to add for key.
        Throws:
        java.lang.Exception - when KeyedObjectPool.addObject(Object) fails.
        java.lang.IllegalArgumentException - when keyedPool or key is null.
      • synchronizedPooledFactory

        public static <T> PooledObjectFactory<T> synchronizedPooledFactory​(PooledObjectFactory<T> factory)
        Returns a synchronized (thread-safe) PooledObjectFactory backed by the specified PooledObjectFactory.
        Type Parameters:
        T - the type of objects in the pool
        Parameters:
        factory - the PooledObjectFactory to be "wrapped" in a synchronized PooledObjectFactory.
        Returns:
        a synchronized view of the specified PooledObjectFactory.
      • synchronizedKeyedPooledFactory

        public static <K,​V> KeyedPooledObjectFactory<K,​V> synchronizedKeyedPooledFactory​(KeyedPooledObjectFactory<K,​V> keyedFactory)
        Returns a synchronized (thread-safe) KeyedPooledObjectFactory backed by the specified KeyedPoolableObjectFactory.
        Type Parameters:
        K - the type of the pool key
        V - the type of pool entries
        Parameters:
        keyedFactory - the KeyedPooledObjectFactory to be "wrapped" in a synchronized KeyedPooledObjectFactory.
        Returns:
        a synchronized view of the specified KeyedPooledObjectFactory.