本文整理汇总了Java中org.apache.commons.pool.ObjectPool.borrowObject方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectPool.borrowObject方法的具体用法?Java ObjectPool.borrowObject怎么用?Java ObjectPool.borrowObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.pool.ObjectPool
的用法示例。
在下文中一共展示了ObjectPool.borrowObject方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: score
import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
* The the given <code>Object[]</code> with this scorer (thread safe!).
* <p/>
* The score in bound by the configuration <code>minScore</code> and <code>maxScore</code>.
*
*
* @param scorable the scorable data to score
* @return the score value already bound the configuration range
* @throws FOSException when classification was not possible
*/
@Override
public double[] score(Object[] scorable) throws FOSException {
/* the pool can change while this is processing (reload) so assign a local variable */
final ObjectPool<Classifier> localPool = pool;
Classifier classifier = null;
try {
classifier = localPool.borrowObject();
return WekaUtils.score(classifier, scorable, instanceSetters, instances, attributes);
} catch (Exception e) {
throw new FOSException(e);
} finally {
returnObject(localPool, classifier);
}
}
示例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();
}
示例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());
}
示例4: 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());
}
示例5: 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());
}
示例6: 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());
}
示例7: 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());
}
}
示例8: testBorrowFromEmptyPoolWithNullFactory
import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
* @deprecated - to be removed in pool 2.0
*/
public void testBorrowFromEmptyPoolWithNullFactory() throws Exception {
ObjectPool pool = new StackObjectPool();
try {
pool.borrowObject();
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
}
示例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);
}
示例10: testCantResetFactoryWithActiveObjects
import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
* @deprecated - to be removed in pool 2.0
*/
public void testCantResetFactoryWithActiveObjects() throws Exception {
ObjectPool pool = new StackObjectPool();
pool.setFactory(new SimpleFactory());
Object obj = pool.borrowObject();
assertNotNull(obj);
try {
pool.setFactory(new SimpleFactory());
fail("Expected IllegalStateException");
} catch(IllegalStateException e) {
// expected
}
}
示例11: 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());
}
示例12: 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);
}
示例13: testMakeNull
import org.apache.commons.pool.ObjectPool; //导入方法依赖的package包/类
/**
* Verifies NoSuchElementException when the factory returns a null object in borrowObject
*/
public void testMakeNull() throws Exception {
SelectiveFactory factory = new SelectiveFactory();
ObjectPool pool = new StackObjectPool(factory);
factory.setMakeNull(true);
try {
pool.borrowObject();
fail("Expecting NoSuchElementException");
} catch (NoSuchElementException ex) {
// Expected
}
}