本文整理汇总了Java中org.apache.commons.pool.ObjectPool.addObject方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectPool.addObject方法的具体用法?Java ObjectPool.addObject怎么用?Java ObjectPool.addObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.pool.ObjectPool
的用法示例。
在下文中一共展示了ObjectPool.addObject方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExceptionOnActivate
import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
* Verifies that exceptions thrown by factory activate method are not propagated to
* the caller. Objects that throw on activate are destroyed and if none succeed,
* the caller gets NoSuchElementException.
*/
public void testExceptionOnActivate() throws Exception {
SelectiveFactory factory = new SelectiveFactory();
ObjectPool pool = new StackObjectPool(factory);
pool.addObject();
pool.addObject();
factory.setThrowOnActivate(true);
try {
pool.borrowObject();
fail("Expecting NoSuchElementException");
} catch (NoSuchElementException ex) {
// expected
}
assertEquals(0, pool.getNumIdle());
assertEquals(0, pool.getNumActive());
}
示例2: 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());
}
示例3: 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());
}
示例4: testAddObject
import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
public void testAddObject() throws Exception {
assertEquals("should be zero idle", 0, pool.getNumIdle());
pool.addObject();
assertEquals("should be one idle", 1, pool.getNumIdle());
assertEquals("should be zero active", 0, pool.getNumActive());
Object obj = pool.borrowObject();
assertEquals("should be zero idle", 0, pool.getNumIdle());
assertEquals("should be one active", 1, pool.getNumActive());
pool.returnObject(obj);
assertEquals("should be one idle", 1, pool.getNumIdle());
assertEquals("should be zero active", 0, pool.getNumActive());
ObjectPool op = new GenericObjectPool();
try {
op.addObject();
fail("Expected IllegalStateException when there is no factory.");
} catch (IllegalStateException ise) {
//expected
}
op.close();
}
示例5: testInitIdleCapacityExceeded
import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
* Verifies that initIdleCapacity is not a hard limit, but maxIdle is.
*/
public void testInitIdleCapacityExceeded() throws Exception {
PoolableObjectFactory factory = new SimpleFactory();
ObjectPool pool = new StackObjectPool(factory, 2, 1);
pool.addObject();
pool.addObject();
assertEquals(2, pool.getNumIdle());
pool.close();
pool = new StackObjectPool(factory, 1, 2);
pool.addObject();
pool.addObject();
assertEquals(1, pool.getNumIdle());
}