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 its 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 keyedPool * the KeyedObjectPool to be decorated so it shrinks its idle * count when possible. * @param <K> the type of the pool key * @param <V> the type of pool entries * @return a pool that adaptively decreases its size when idle objects are * no longer needed. * @see #erodingPool(KeyedObjectPool, float) * @see #erodingPool(KeyedObjectPool, float, boolean) */ public static <K, V> KeyedObjectPool<K, V> erodingPool( final KeyedObjectPool<K, V> keyedPool) { return erodingPool(keyedPool, 1f); }
prefill(keyedPool, iter.next(), count);
PoolUtils.checkRethrow(t); try { destroy(ref); } catch (final Throwable t2) { PoolUtils.checkRethrow(t2);
/** * Constructor for OmimServices. Takes OMIM API keys from molgenis.properties and puts them in a pool. * * @param apiKeys * list of OMIM API keys from molgenis.properties */ @Autowired public OmimService(@Value("#{'${omim_key:@null}'.split(',')}") List<String> apiKeys) { // initialize a pool for OMIM keys to circulate them and protect them from over-use PooledOmimKeyFactory keyFactory = new PooledOmimKeyFactory(apiKeys); PooledObjectFactory<String> syncFactory = PoolUtils.synchronizedPooledFactory(keyFactory); GenericObjectPool<String> genericPool = new GenericObjectPool<String>(syncFactory); genericPool.setBlockWhenExhausted(true); genericPool.setLifo(false); genericPool.setMaxTotal(apiKeys.size()); pool = PoolUtils.synchronizedPool(genericPool); }
@Test public void testSynchronizedPoolObjectPool() throws Exception { try { PoolUtils.synchronizedPool((ObjectPool<Object>) null); fail("PoolUtils.synchronizedPool(ObjectPool) must not allow a null pool."); } catch (final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final ObjectPool<Object> op = createProxy(ObjectPool.class, calledMethods)) { final ObjectPool<Object> sop = PoolUtils.synchronizedPool(op); final List<String> expectedMethods = invokeEveryMethod(sop); assertEquals(expectedMethods, calledMethods); // TODO: Anyone feel motivated to construct a test that verifies proper synchronization? } }
getMinIdleTimer().schedule(task, 0L, period); return task;
@Test public void testSynchronizedPoolableFactoryPoolableObjectFactory() throws Exception { try { PoolUtils.synchronizedPooledFactory((PooledObjectFactory<Object>)null); fail("PoolUtils.synchronizedPoolableFactory(PoolableObjectFactory) must not allow a null factory."); } catch(final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); @SuppressWarnings("unchecked") final PooledObjectFactory<Object> pof = createProxy(PooledObjectFactory.class, calledMethods); final PooledObjectFactory<Object> spof = PoolUtils.synchronizedPooledFactory(pof); final List<String> expectedMethods = invokeEveryMethod(spof); assertEquals(expectedMethods, calledMethods); // TODO: Anyone feel motivated to construct a test that verifies proper synchronization? }
@Test public void testJavaBeanInstantiation() { Assert.assertNotNull(new PoolUtils()); }
@Test public void testSynchronizedPoolableFactoryKeyedPoolableObjectFactory() throws Exception { try { PoolUtils.synchronizedKeyedPooledFactory((KeyedPooledObjectFactory<Object,Object>)null); fail("PoolUtils.synchronizedPoolableFactory(KeyedPoolableObjectFactory) must not allow a null factory."); } catch(final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); @SuppressWarnings("unchecked") final KeyedPooledObjectFactory<Object,Object> kpof = createProxy(KeyedPooledObjectFactory.class, calledMethods); final KeyedPooledObjectFactory<Object,Object> skpof = PoolUtils.synchronizedKeyedPooledFactory(kpof); final List<String> expectedMethods = invokeEveryMethod(skpof); assertEquals(expectedMethods, calledMethods); // TODO: Anyone feel motivated to construct a test that verifies proper synchronization? }
@Test public void testCheckRethrow() { try { PoolUtils.checkRethrow(new Exception()); } catch (final Throwable t) { fail("PoolUtils.checkRethrow(Throwable) must rethrow only ThreadDeath and VirtualMachineError."); } try { PoolUtils.checkRethrow(new ThreadDeath()); fail("PoolUtils.checkRethrow(Throwable) must rethrow ThreadDeath."); } catch (final ThreadDeath td) { // expected } catch (final Throwable t) { fail("PoolUtils.checkRethrow(Throwable) must rethrow only ThreadDeath and VirtualMachineError."); } try { PoolUtils.checkRethrow(new InternalError()); // InternalError extends VirtualMachineError fail("PoolUtils.checkRethrow(Throwable) must rethrow VirtualMachineError."); } catch (final VirtualMachineError td) { // expected } catch (final Throwable t) { fail("PoolUtils.checkRethrow(Throwable) must rethrow only ThreadDeath and VirtualMachineError."); } }
@Test public void testSynchronizedPoolKeyedObjectPool() throws Exception { try { PoolUtils.synchronizedPool((KeyedObjectPool<Object, Object>) null); fail("PoolUtils.synchronizedPool(KeyedObjectPool) must not allow a null pool."); } catch (final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final KeyedObjectPool<Object, Object> kop = createProxy(KeyedObjectPool.class, calledMethods)) { final KeyedObjectPool<Object, Object> skop = PoolUtils.synchronizedPool(kop); final List<String> expectedMethods = invokeEveryMethod(skop); assertEquals(expectedMethods, calledMethods); } // TODO: Anyone feel motivated to construct a test that verifies proper synchronization? }
getMinIdleTimer().schedule(task, 0L, period); return task;
@Test public void testGetFactoryType_PoolUtilsSynchronizedNullPooledFactory() { try (final GenericObjectPool<String> pool = new GenericObjectPool<>( PoolUtils.synchronizedPooledFactory(createNullPooledObjectFactory()))) { Assert.assertNotNull((pool.getFactoryType())); } }
/** * Returns a pool that adaptively decreases its 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 its idle count * when possible. * @param <T> the type of objects in the pool * @return a pool that adaptively decreases its size when idle objects are * no longer needed. * @see #erodingPool(ObjectPool, float) */ public static <T> ObjectPool<T> erodingPool(final ObjectPool<T> pool) { return erodingPool(pool, 1f); }
PoolUtils.checkRethrow(t); swallowException(new Exception(t));
@Test public void testPrefillObjectPool() throws Exception { try { PoolUtils.prefill(null, 1); fail("PoolUtils.prefill(ObjectPool,int) must not allow null pool."); } catch (final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final ObjectPool<Object> pool = createProxy(ObjectPool.class, calledMethods)) { PoolUtils.prefill(pool, 0); final List<String> expectedMethods = new ArrayList<>(); assertEquals(expectedMethods, calledMethods); calledMethods.clear(); PoolUtils.prefill(pool, 3); for (int i = 0; i < 3; i++) { expectedMethods.add("addObject"); } assertEquals(expectedMethods, calledMethods); } }
@Test public void testCheckMinIdleKeyedObjectPoolKeysNulls() throws Exception { try (@SuppressWarnings("unchecked") final KeyedObjectPool<Object,Object> pool = createProxy(KeyedObjectPool.class, (List<String>)null)) { PoolUtils.checkMinIdle(pool, (Collection<?>) null, 1, 1); fail("PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long) must not accept null keys."); } catch (final IllegalArgumentException iae) { // expected } try (@SuppressWarnings("unchecked") final KeyedObjectPool<Object,Object> pool = createProxy(KeyedObjectPool.class, (List<String>)null)) { PoolUtils.checkMinIdle(pool, (Collection<?>) Collections.emptyList(), 1, 1); } catch (final IllegalArgumentException iae) { fail("PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long) must accept empty lists."); } }
private void initPool() { if (dispatcherPool == null) { // TODO EVENT Some of these pool configuration // parameters can live in dspace.cfg or a // separate configuration file // TODO EVENT Eviction parameters should be set poolConfig = new GenericKeyedObjectPoolConfig(); poolConfig.setMaxTotalPerKey(100); poolConfig.setMaxIdlePerKey(5); poolConfig.setMaxTotal(100); try { dispatcherFactory = new DispatcherPoolFactory(); dispatcherPool = PoolUtils .synchronizedPool(new GenericKeyedObjectPool( dispatcherFactory, poolConfig)); enumerateConsumers(); } catch (Exception e) { log.error("Could not initialize EventService dispatcher pool", e); } } }
getMinIdleTimer().schedule(task, 0L, period); return task;