当前位置: 首页>>代码示例>>Java>>正文


Java ObjectPool.returnObject方法代码示例

本文整理汇总了Java中org.apache.commons.pool.ObjectPool.returnObject方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectPool.returnObject方法的具体用法?Java ObjectPool.returnObject怎么用?Java ObjectPool.returnObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.pool.ObjectPool的用法示例。


在下文中一共展示了ObjectPool.returnObject方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: releaseJdbcConnection

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
public void releaseJdbcConnection(JdbcConnectionDescriptor jcd, Connection con)
        throws LookupException
{
    final ObjectPool op = (ObjectPool) poolMap.get(jcd.getPBKey());
    try
    {
        /* mkalen: NB - according to the Commons Pool API we should _not_ perform
         * any additional checks here since we will then break testOnX semantics
         *
         * To enable Connection validation on releaseJdbcConnection,
         * set a validation query and specify testOnRelease=true
         *
         * Destruction of pooled objects is performed by the actual Commons Pool
         * ObjectPool implementation when the object factory's validateObject method
         * returns false. See ConPoolFactory#validateObject.
         */
        op.returnObject(con);
    }
    catch (Exception e)
    {
        throw new LookupException(e);
    }
}
 
开发者ID:KualiCo,项目名称:ojb,代码行数:24,代码来源:ConnectionFactoryPooledImpl.java

示例2: testPoolWithNullFactory

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * @deprecated - to be removed in pool 2.0
 */
public void testPoolWithNullFactory() throws Exception {
    ObjectPool pool = new StackObjectPool(10);
    for(int i=0;i<10;i++) {
        pool.returnObject(new Integer(i));
    }
    for(int j=0;j<3;j++) {
        Integer[] borrowed = new Integer[10];
        BitSet found = new BitSet();
        for(int i=0;i<10;i++) {
            borrowed[i] = (Integer)(pool.borrowObject());
            assertNotNull(borrowed);
            assertTrue(!found.get(borrowed[i].intValue()));
            found.set(borrowed[i].intValue());
        }
        for(int i=0;i<10;i++) {
            pool.returnObject(borrowed[i]);
        }
    }
    pool.invalidateObject(pool.borrowObject());
    pool.invalidateObject(pool.borrowObject());
    pool.clear();        
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:26,代码来源:TestStackObjectPool.java

示例3: testBorrowReturnWithSometimesInvalidObjects

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * Verifies that validation and passivation failures returning objects are handled
 * properly - instances destroyed and not returned to the pool, but no exceptions propagated.
 */
public void testBorrowReturnWithSometimesInvalidObjects() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory, 20);

    Object[] obj = new Object[10];
    for(int i=0;i<10;i++) {
        obj[i] = pool.borrowObject();
        assertEquals("Each time we borrow, get one more active.", i+1, pool.getNumActive());
        
    }
    
    factory.setValidateSelectively(true);  // Even numbers fail validation
    factory.setPassivateSelectively(true); // Multiples of 3 fail passivation

    for(int i=0;i<10;i++) {
        pool.returnObject(obj[i]);
        assertEquals("Each time we return, get one less active.", 9-i, pool.getNumActive());
    }
    // 0,2,4,6,8 fail validation, 3, 9 fail passivation - 3 left.
    assertEquals(3,pool.getNumIdle());
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:26,代码来源:TestStackObjectPool.java

示例4: testExceptionOnDestroy

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * Verifies that exceptions thrown by factory destroy are swallowed
 * by both addObject and returnObject.
 */
public void testExceptionOnDestroy() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory, 2);
    factory.setThrowOnDestroy(true);
    for (int i = 0; i < 3; i++) {
        pool.addObject(); // Third one will destroy, exception should be swallowed
    }
    assertEquals(2, pool.getNumIdle());
    
    Object[] objects = new Object[3];
    for (int i = 0; i < 3; i++) {
        objects[i] = pool.borrowObject();
    }
    for (int i = 0; i < 3; i++) {
        pool.returnObject(objects[i]); // Third triggers destroy
    } 
    assertEquals(2, pool.getNumIdle());
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:23,代码来源:TestStackObjectPool.java

示例5: testExceptionOnPassivate

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * Verifies that addObject propagates exceptions thrown by
 * factory passivate, but returnObject swallows these.
 */
public void testExceptionOnPassivate() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory, 2);
    factory.setThrowOnPassivate(true);
    
    // addObject propagates
    try {
        pool.addObject();
        fail("Expecting IntegerFactoryException");
    } catch (IntegerFactoryException ex) {
        assertEquals("passivateObject", ex.getType());
        assertEquals(0, ex.getValue());
    }
    assertEquals(0, pool.getNumIdle());
    
    // returnObject swallows 
    Object obj = pool.borrowObject();
    pool.returnObject(obj);
    assertEquals(0, pool.getNumIdle());
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:25,代码来源:TestStackObjectPool.java

示例6: process

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
public void process() throws Exception
  {
      Object queueElement = this.queue.take();

if (!(queueElement instanceof ListenerRequest))
      {
          throw new IllegalArgumentException("Expected ListenerRequest but found " + queueElement.getClass().getName());
      }

ListenerRequest request = (ListenerRequest) queueElement;

try
{
	processRequest(request);
}
finally
{
	ObjectPool pool = request.getBorrowedFromPool();
	
       if (pool != null)
       {
       	pool.returnObject(request);
       }
}
  }
 
开发者ID:coova,项目名称:jradius,代码行数:26,代码来源:Processor.java

示例7: returnObject

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * Returns the given object to the pool.
 *
 * @param pool   The pool to where return the object to.
 * @param object The object to be returned to the pool.
 */
private void returnObject(ObjectPool<Classifier> pool, Classifier object) {
    try {
        pool.returnObject(object);
    } catch (Exception e) {
        logger.error("Could not return object to pool", e);
    }
}
 
开发者ID:feedzai,项目名称:fos-weka,代码行数:14,代码来源:WekaThreadSafeScorerPool.java

示例8: testIdleCap

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
public void testIdleCap() throws Exception {
    ObjectPool pool = makeEmptyPool(8);
    Object[] active = new Object[100];
    for(int i=0;i<100;i++) {
        active[i] = pool.borrowObject();
    }
    assertEquals(100,pool.getNumActive());
    assertEquals(0,pool.getNumIdle());
    for(int i=0;i<100;i++) {
        pool.returnObject(active[i]);
        assertEquals(99 - i,pool.getNumActive());
        assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle());
    }
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:15,代码来源:TestStackObjectPool.java

示例9: testSetFactory

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * @deprecated - to be removed in pool 2.0
 */
public void testSetFactory() throws Exception {
    ObjectPool pool = new StackObjectPool();
    try {
        pool.borrowObject();
        fail("Expected NoSuchElementException");
    } catch(NoSuchElementException e) {
        // expected
    }
    pool.setFactory(new SimpleFactory());
    Object obj = pool.borrowObject();
    assertNotNull(obj);
    pool.returnObject(obj);
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:17,代码来源:TestStackObjectPool.java

示例10: testBorrowWithSometimesInvalidObjects

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * Verifies that validation failures when borrowing newly created instances
 * from the pool result in NoSuchElementExceptions and passivation failures
 * result in instances not being returned to the pool.
 */
public void testBorrowWithSometimesInvalidObjects() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    factory.setValidateSelectively(true);  // Even numbers fail validation
    factory.setPassivateSelectively(true); // Multiples of 3 fail passivation
    ObjectPool pool = new StackObjectPool(factory, 20);
    Object[] obj = new Object[10];
    for(int i=0;i<10;i++) {
        Object object = null;
        int k = 0;
        while (object == null && k < 100) { // bound not really needed
            try {
                k++;
                object = pool.borrowObject();
                if (((Integer) object).intValue() % 2 == 0) {
                    fail("Expecting NoSuchElementException");
                } else {
                    obj[i] = object; 
                }
            } catch (NoSuchElementException ex) {
                // Should fail for evens
            }
        }
        assertEquals("Each time we borrow, get one more active.", i+1, pool.getNumActive());
    }
    // 1,3,5,...,19 pass validation, get checked out
    for(int i=0;i<10;i++) {
        pool.returnObject(obj[i]);
        assertEquals("Each time we return, get one less active.", 9-i, pool.getNumActive());
    }
    // 3, 9, 15 fail passivation.  
    assertEquals(7,pool.getNumIdle());
    assertEquals(new Integer(19), pool.borrowObject());
    assertEquals(new Integer(17), pool.borrowObject());
    assertEquals(new Integer(13), pool.borrowObject());
    assertEquals(new Integer(11), pool.borrowObject());
    assertEquals(new Integer(7), pool.borrowObject());
    assertEquals(new Integer(5), pool.borrowObject());
    assertEquals(new Integer(1), pool.borrowObject());     
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:45,代码来源:TestStackObjectPool.java

示例11: testReturnObjectDiscardOrder

import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
 * Verifies that when returning objects cause maxSleeping exceeded, oldest instances
 * are destroyed to make room for returning objects.
 */
public void testReturnObjectDiscardOrder() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory, 3);

    // borrow more objects than the pool can hold
    Integer i0 = (Integer)pool.borrowObject();
    Integer i1 = (Integer)pool.borrowObject();
    Integer i2 = (Integer)pool.borrowObject();
    Integer i3 = (Integer)pool.borrowObject();

    // tests
    // return as many as the pool will hold.
    pool.returnObject(i0);
    pool.returnObject(i1);
    pool.returnObject(i2);

    // the pool should now be full.
    assertEquals("No returned objects should have been destroyed yet.", 0,  factory.getDestroyed().size());

    // cause the pool to discard a stale object.
    pool.returnObject(i3);
    assertEquals("One object should have been destroyed.", 1, factory.getDestroyed().size());

    // check to see what object was destroyed
    Integer d = (Integer)factory.getDestroyed().get(0);
    assertEquals("Destoryed object should be the stalest object.", i0, d);
}
 
开发者ID:WhiteBearSolutions,项目名称:WBSAirback,代码行数:32,代码来源:TestStackObjectPool.java


注:本文中的org.apache.commons.pool.ObjectPool.returnObject方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。