@Test public void referenceIdentityByDefault() throws Exception { CommonsPool2TargetSource targetSource = new CommonsPool2TargetSource(); targetSource.setMaxWait(1); prepareTargetSource(targetSource); Object first = targetSource.getTarget(); Object second = targetSource.getTarget(); assertTrue(first instanceof SerializablePerson); assertTrue(second instanceof SerializablePerson); assertEquals(first, second); targetSource.releaseTarget(first); targetSource.releaseTarget(second); }
/** * Subclasses can override this if they want to return a specific Commons pool. * They should apply any configuration properties to the pool here. * <p>Default is a GenericObjectPool instance with the given pool size. * @return an empty Commons {@code ObjectPool}. * @see GenericObjectPool * @see #setMaxSize */ protected ObjectPool createObjectPool() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(getMaxSize()); config.setMaxIdle(getMaxIdle()); config.setMinIdle(getMinIdle()); config.setMaxWaitMillis(getMaxWait()); config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis()); config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis()); config.setBlockWhenExhausted(isBlockWhenExhausted()); return new GenericObjectPool(this, config); }
@Override @Nullable protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (beanName.startsWith(PREFIX_COMMONS_POOL)) { CommonsPool2TargetSource cpts = new CommonsPool2TargetSource(); cpts.setMaxSize(25); return cpts; } else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) { return new ThreadLocalTargetSource(); } else if (beanName.startsWith(PREFIX_PROTOTYPE)) { return new PrototypeTargetSource(); } else { // No match. Don't create a custom target source. return null; } }
@Bean public CommonsPool2TargetSource poolTargetSource() { CommonsPool2TargetSource pool = new CommonsPool2TargetSource(); pool.setMaxSize(3); pool.setTargetBeanName("myObjectTarget"); return pool; }
@Test public void testSetWhenExhaustedAction() { CommonsPool2TargetSource targetSource = new CommonsPool2TargetSource(); targetSource.setBlockWhenExhausted(true); assertEquals(true, targetSource.isBlockWhenExhausted()); }
@Test public void testHitMaxSizeLoadedFromContext() throws Exception { Advised person = (Advised) beanFactory.getBean("maxSizePooledPerson"); CommonsPool2TargetSource targetSource = (CommonsPool2TargetSource) person.getTargetSource(); int maxSize = targetSource.getMaxSize(); Object[] pooledInstances = new Object[maxSize]; for (int x = 0; x < maxSize; x++) { Object instance = targetSource.getTarget(); assertNotNull(instance); pooledInstances[x] = instance; } // should be at maximum now try { targetSource.getTarget(); fail("Should throw NoSuchElementException"); } catch (NoSuchElementException ex) { // desired } // lets now release an object and try to acquire a new one targetSource.releaseTarget(pooledInstances[9]); pooledInstances[9] = targetSource.getTarget(); // release all objects for (int i = 0; i < pooledInstances.length; i++) { System.out.println(i); targetSource.releaseTarget(pooledInstances[i]); } }
/** * Create a CommonsPoolTargetSource with default settings. * Default maximum size of the pool is 8. * @see #setMaxSize * @see GenericObjectPoolConfig#setMaxTotal */ public CommonsPool2TargetSource() { setMaxSize(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL); }
/** * Creates and holds an ObjectPool instance. * @see #createObjectPool() */ @Override protected final void createPool() { logger.debug("Creating Commons object pool"); this.pool = createObjectPool(); }
@Override public void destroyObject(PooledObject<Object> p) throws Exception { destroyPrototypeInstance(p.getObject()); }
/** * Create a CommonsPoolTargetSource with default settings. * Default maximum size of the pool is 8. * @see #setMaxSize * @see GenericObjectPoolConfig#setMaxTotal */ public CommonsPool2TargetSource() { setMaxSize(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL); }
/** * Creates and holds an ObjectPool instance. * @see #createObjectPool() */ @Override protected final void createPool() { logger.debug("Creating Commons object pool"); this.pool = createObjectPool(); }
@Override public void destroyObject(PooledObject<Object> p) throws Exception { destroyPrototypeInstance(p.getObject()); }
/** * Subclasses can override this if they want to return a specific Commons pool. * They should apply any configuration properties to the pool here. * <p>Default is a GenericObjectPool instance with the given pool size. * @return an empty Commons {@code ObjectPool}. * @see GenericObjectPool * @see #setMaxSize */ protected ObjectPool createObjectPool() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(getMaxSize()); config.setMaxIdle(getMaxIdle()); config.setMinIdle(getMinIdle()); config.setMaxWaitMillis(getMaxWait()); config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis()); config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis()); config.setBlockWhenExhausted(isBlockWhenExhausted()); return new GenericObjectPool(this, config); }
@Test public void testHitMaxSize() throws Exception { int maxSize = 10; CommonsPool2TargetSource targetSource = new CommonsPool2TargetSource(); targetSource.setMaxSize(maxSize); targetSource.setMaxWait(1); prepareTargetSource(targetSource); Object[] pooledInstances = new Object[maxSize]; for (int x = 0; x < maxSize; x++) { Object instance = targetSource.getTarget(); assertNotNull(instance); pooledInstances[x] = instance; } // should be at maximum now try { targetSource.getTarget(); fail("Should throw NoSuchElementException"); } catch (NoSuchElementException ex) { // desired } // lets now release an object and try to acquire a new one targetSource.releaseTarget(pooledInstances[9]); pooledInstances[9] = targetSource.getTarget(); // release all objects for (int i = 0; i < pooledInstances.length; i++) { targetSource.releaseTarget(pooledInstances[i]); } }
@Override @Nullable protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (beanName.startsWith(PREFIX_COMMONS_POOL)) { CommonsPool2TargetSource cpts = new CommonsPool2TargetSource(); cpts.setMaxSize(25); return cpts; } else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) { return new ThreadLocalTargetSource(); } else if (beanName.startsWith(PREFIX_PROTOTYPE)) { return new PrototypeTargetSource(); } else { // No match. Don't create a custom target source. return null; } }
/** * Create a CommonsPoolTargetSource with default settings. * Default maximum size of the pool is 8. * @see #setMaxSize * @see GenericObjectPoolConfig#setMaxTotal */ public CommonsPool2TargetSource() { setMaxSize(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL); }
/** * Creates and holds an ObjectPool instance. * @see #createObjectPool() */ @Override protected final void createPool() { logger.debug("Creating Commons object pool"); this.pool = createObjectPool(); }
@Override public void destroyObject(PooledObject<Object> p) throws Exception { destroyPrototypeInstance(p.getObject()); }
/** * Subclasses can override this if they want to return a specific Commons pool. * They should apply any configuration properties to the pool here. * <p>Default is a GenericObjectPool instance with the given pool size. * @return an empty Commons {@code ObjectPool}. * @see GenericObjectPool * @see #setMaxSize */ protected ObjectPool createObjectPool() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(getMaxSize()); config.setMaxIdle(getMaxIdle()); config.setMinIdle(getMinIdle()); config.setMaxWaitMillis(getMaxWait()); config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis()); config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis()); config.setBlockWhenExhausted(isBlockWhenExhausted()); return new GenericObjectPool(this, config); }
@Override protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (beanName.startsWith(PREFIX_COMMONS_POOL)) { CommonsPool2TargetSource cpts = new CommonsPool2TargetSource(); cpts.setMaxSize(25); return cpts; } else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) { return new ThreadLocalTargetSource(); } else if (beanName.startsWith(PREFIX_PROTOTYPE)) { return new PrototypeTargetSource(); } else { // No match. Don't create a custom target source. return null; } }