/** * Create a <code>SoftReferenceObjectPool</code> with the specified factory and initial idle object count. * * @param factory object factory to use. * @param initSize initial size to attempt to prefill the pool. * @throws Exception when there is a problem prefilling the pool. * @throws IllegalArgumentException when <code>factory</code> is <code>null</code>. * @deprecated because this is a SoftReference pool, prefilled idle obejects may be garbage collected before they are used. * To be removed in Pool 2.0. */ @Deprecated public SoftReferenceObjectPool(PoolableObjectFactory<T> factory, int initSize) throws Exception, IllegalArgumentException { if (factory == null) { throw new IllegalArgumentException("factory required to prefill the pool."); } _pool = new ArrayList<SoftReference<T>>(initSize); _factory = factory; PoolUtils.prefill(this, initSize); }
/** * Adapt a <code>KeyedObjectPool</code> instance to work where an <code>ObjectPool</code> is needed. This is the * equivalent of calling {@link #adapt(KeyedObjectPool, Object) PoolUtils.adapt(aKeyedObjectPool, new Object())}. * * @param <V> the type of object * @param keyedPool the {@link KeyedObjectPool} to delegate to. * @return an {@link ObjectPool} that delegates to <code>keyedPool</code> with an internal key. * @throws IllegalArgumentException when <code>keyedPool</code> is <code>null</code>. * @see #adapt(KeyedObjectPool, Object) * @since Pool 1.3 */ public static <V> ObjectPool<V> adapt(final KeyedObjectPool<Object, V> keyedPool) throws IllegalArgumentException { return adapt(keyedPool, new Object()); }
while (iter.hasNext()) { final K key = iter.next(); final TimerTask task = checkMinIdle(keyedPool, key, minIdle, period); tasks.put(key, task);
/** * Returns a pool that adaptively decreases it's size when idle objects are no longer needed. * This is intended as an always thread-safe alternative to using an idle object evictor * provided by many pool implementations. This is also an effective way to shrink FIFO ordered * pools that experience load spikes. * * @param <T> the type of object * @param pool the ObjectPool to be decorated so it shrinks it's idle count when possible. * @return a pool that adaptively decreases it's size when idle objects are no longer needed. * @see #erodingPool(ObjectPool, float) * @since Pool 1.4 */ public static <T> ObjectPool<T> erodingPool(final ObjectPool<T> pool) { return erodingPool(pool, 1f); }
/** * 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 {@link ObjectPool#addObject()} then no more checks will be performed. * * @param <T> the type of object * @param pool the pool to check periodically. * @param minIdle if the {@link ObjectPool#getNumIdle()} is less than this then add an idle object. * @param period the frequency to check the number of idle objects in a pool, see * {@link Timer#schedule(TimerTask, long, long)}. * @return the {@link TimerTask} that will periodically check the pools idle object count. * @throws IllegalArgumentException when <code>pool</code> is <code>null</code> or * when <code>minIdle</code> is negative or when <code>period</code> isn't * valid for {@link Timer#schedule(TimerTask, long, long)}. * @since Pool 1.3 */ public static <T> TimerTask checkMinIdle(final ObjectPool<T> pool, final int minIdle, final long period) throws IllegalArgumentException { if (pool == null) { throw new IllegalArgumentException("keyedPool must not be null."); } if (minIdle < 0) { throw new IllegalArgumentException("minIdle must be non-negative."); } final TimerTask task = new ObjectPoolMinIdleTimerTask<T>(pool, minIdle); getMinIdleTimer().schedule(task, 0L, period); return task; }
PoolUtils.checkRethrow(t); try { _factory.destroyObject(key,obj); } catch (Throwable t2) { PoolUtils.checkRethrow(t2);
/** * Returns a pool that adaptively decreases it's size when idle objects are no longer needed. * This is intended as an always thread-safe alternative to using an idle object evictor * provided by many pool implementations. This is also an effective way to shrink FIFO ordered * pools that experience load spikes. * * @param <K> the type of key * @param <V> the type of object * @param keyedPool the KeyedObjectPool to be decorated so it shrinks it's idle count when * possible. * @return a pool that adaptively decreases it's size when idle objects are no longer needed. * @see #erodingPool(KeyedObjectPool, float) * @see #erodingPool(KeyedObjectPool, float, boolean) * @since Pool 1.4 */ public static <K, V> KeyedObjectPool<K, V> erodingPool(final KeyedObjectPool<K, V> keyedPool) { return erodingPool(keyedPool, 1f); }
getMinIdleTimer().schedule(task, 0L, period); return task;
PoolUtils.checkRethrow(t); try { _factory.destroyObject(obj); } catch (Throwable t2) { PoolUtils.checkRethrow(t2);
/** * Adapt a <code>KeyedPoolableObjectFactory</code> instance to work where a <code>PoolableObjectFactory</code> is * needed. This method is the equivalent of calling * {@link #adapt(KeyedPoolableObjectFactory, Object) PoolUtils.adapt(aKeyedPoolableObjectFactory, new Object())}. * * @param <V> the type of object * @param keyedFactory the {@link KeyedPoolableObjectFactory} to delegate to. * @return a {@link PoolableObjectFactory} that delegates to <code>keyedFactory</code> with an internal key. * @throws IllegalArgumentException when <code>keyedFactory</code> is <code>null</code>. * @see #adapt(KeyedPoolableObjectFactory, Object) * @since Pool 1.3 */ public static <V> PoolableObjectFactory<V> adapt(final KeyedPoolableObjectFactory<Object, V> keyedFactory) throws IllegalArgumentException { return adapt(keyedFactory, new Object()); }
/** * Call <code>addObject(Object)</code> on <code>keyedPool</code> with each key in <code>keys</code> for * <code>count</code> number of times. This has the same effect as calling * {@link #prefill(KeyedObjectPool, Object, int)} for each key in the <code>keys</code> collection. * * @param <K> the type of key * @param <V> the type of object * @param keyedPool the keyedPool to prefill. * @param keys {@link Collection} of keys to add objects for. * @param count the number of idle objects to add for each <code>key</code>. * @throws Exception when {@link KeyedObjectPool#addObject(Object)} fails. * @throws IllegalArgumentException when <code>keyedPool</code>, <code>keys</code>, or * any value in <code>keys</code> is <code>null</code>. * @see #prefill(KeyedObjectPool, Object, int) * @since Pool 1.3 */ public static <K, V> void prefill(final KeyedObjectPool<K, V> keyedPool, final Collection<? extends K> keys, final int count) throws Exception, IllegalArgumentException { if (keys == null) { throw new IllegalArgumentException("keys must not be null."); } final Iterator<? extends K> iter = keys.iterator(); while (iter.hasNext()) { prefill(keyedPool, iter.next(), count); } }
/** * Returns a pool that adaptively decreases it's size when idle objects are no longer needed. * This is intended as an always thread-safe alternative to using an idle object evictor * provided by many pool implementations. This is also an effective way to shrink FIFO ordered * pools that experience load spikes. * * <p> * The factor parameter provides a mechanism to tweak the rate at which the pool tries to shrink * it's size. Values between 0 and 1 cause the pool to try to shrink it's size more often. * Values greater than 1 cause the pool to less frequently try to shrink it's size. * </p> * * @param <K> the type of key * @param <V> the type of object * @param keyedPool the KeyedObjectPool to be decorated so it shrinks it's idle count when * possible. * @param factor a positive value to scale the rate at which the pool tries to reduce it's size. * If 0 < factor < 1 then the pool shrinks more aggressively. * If 1 < factor then the pool shrinks less aggressively. * @return a pool that adaptively decreases it's size when idle objects are no longer needed. * @see #erodingPool(KeyedObjectPool, float, boolean) * @since Pool 1.4 */ public static <K, V> KeyedObjectPool<K, V> erodingPool(final KeyedObjectPool<K, V> keyedPool, final float factor) { return erodingPool(keyedPool, factor, false); }
/** * 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 {@link ObjectPool#addObject()} then no more checks will be performed. * * @param pool the pool to check periodically. * @param minIdle if the {@link ObjectPool#getNumIdle()} is less than this then add an idle object. * @param period the frequency to check the number of idle objects in a pool, see * {@link Timer#schedule(TimerTask, long, long)}. * @return the {@link TimerTask} that will periodically check the pools idle object count. * @throws IllegalArgumentException when <code>pool</code> is <code>null</code> or * when <code>minIdle</code> is negative or when <code>period</code> isn't * valid for {@link Timer#schedule(TimerTask, long, long)}. * @since Pool 1.3 */ public static TimerTask checkMinIdle(final ObjectPool pool, final int minIdle, final long period) throws IllegalArgumentException { if (pool == null) { throw new IllegalArgumentException("keyedPool must not be null."); } if (minIdle < 0) { throw new IllegalArgumentException("minIdle must be non-negative."); } final TimerTask task = new ObjectPoolMinIdleTimerTask(pool, minIdle); getMinIdleTimer().schedule(task, 0L, period); return task; }
PoolUtils.checkRethrow(t); try { _factory.destroyObject(obj); } catch (Throwable t2) { PoolUtils.checkRethrow(t2);
/** * Periodically check the idle object count for each key in the <code>Collection</code> <code>keys</code> in the keyedPool. * At most one idle object will be added per period. * * @param keyedPool the keyedPool to check periodically. * @param keys a collection of keys to check the idle object count. * @param minIdle if the {@link KeyedObjectPool#getNumIdle(Object)} is less than this then add an idle object. * @param period the frequency to check the number of idle objects in a keyedPool, see * {@link Timer#schedule(TimerTask, long, long)}. * @return a {@link Map} of key and {@link TimerTask} pairs that will periodically check the pools idle object count. * @throws IllegalArgumentException when <code>keyedPool</code>, <code>keys</code>, or any of the values in the * collection is <code>null</code> or when <code>minIdle</code> is negative or when <code>period</code> isn't * valid for {@link Timer#schedule(TimerTask, long, long)}. * @see #checkMinIdle(KeyedObjectPool, Object, int, long) * @since Pool 1.3 */ public static Map checkMinIdle(final KeyedObjectPool keyedPool, final Collection keys, final int minIdle, final long period) throws IllegalArgumentException { if (keys == null) { throw new IllegalArgumentException("keys must not be null."); } final Map tasks = new HashMap(keys.size()); final Iterator iter = keys.iterator(); while (iter.hasNext()) { final Object key = iter.next(); final TimerTask task = checkMinIdle(keyedPool, key, minIdle, period); tasks.put(key, task); } return tasks; }
/** * Sets the PoolableObjectFactory for the pool. * * @param factory new PoolableObjectFactory * @deprecated to be removed in version 2.0 */ @Deprecated public void setFactory(final PoolableObjectFactory<V> factory) throws IllegalStateException, UnsupportedOperationException { keyedPool.setFactory(adapt(factory)); }
/** * Create a <code>SoftReferenceObjectPool</code> with the specified factory and initial idle object count. * * @param factory object factory to use. * @param initSize initial size to attempt to prefill the pool. * @throws Exception when there is a problem prefilling the pool. * @throws IllegalArgumentException when <code>factory</code> is <code>null</code>. * @deprecated because this is a SoftReference pool, prefilled idle obejects may be garbage collected before they are used. * To be removed in Pool 3.0. */ public SoftReferenceObjectPool(PoolableObjectFactory factory, int initSize) throws Exception, IllegalArgumentException { if (factory == null) { throw new IllegalArgumentException("factory required to prefill the pool."); } _pool = new ArrayList(initSize); _factory = factory; PoolUtils.prefill(this, initSize); }
/** * Returns a pool that adaptively decreases it's size when idle objects are no longer needed. * This is intended as an always thread-safe alternative to using an idle object evictor * provided by many pool implementations. This is also an effective way to shrink FIFO ordered * pools that experience load spikes. * * @param pool the ObjectPool to be decorated so it shrinks it's idle count when possible. * @return a pool that adaptively decreases it's size when idle objects are no longer needed. * @see #erodingPool(ObjectPool, float) * @since Pool 1.4 */ public static ObjectPool erodingPool(final ObjectPool pool) { return erodingPool(pool, 1f); }
/** * 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 {@link ObjectPool#addObject()} then no more checks will be performed. * * @param <T> the type of object * @param pool the pool to check periodically. * @param minIdle if the {@link ObjectPool#getNumIdle()} is less than this then add an idle object. * @param period the frequency to check the number of idle objects in a pool, see * {@link Timer#schedule(TimerTask, long, long)}. * @return the {@link TimerTask} that will periodically check the pools idle object count. * @throws IllegalArgumentException when <code>pool</code> is <code>null</code> or * when <code>minIdle</code> is negative or when <code>period</code> isn't * valid for {@link Timer#schedule(TimerTask, long, long)}. * @since Pool 1.3 */ public static <T> TimerTask checkMinIdle(final ObjectPool<T> pool, final int minIdle, final long period) throws IllegalArgumentException { if (pool == null) { throw new IllegalArgumentException("keyedPool must not be null."); } if (minIdle < 0) { throw new IllegalArgumentException("minIdle must be non-negative."); } final TimerTask task = new ObjectPoolMinIdleTimerTask<T>(pool, minIdle); getMinIdleTimer().schedule(task, 0L, period); return task; }
PoolUtils.checkRethrow(e); PoolUtils.checkRethrow(e2);