热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

org.apache.commons.pool2.impl.GenericKeyedObjectPool.borrowObject()方法的使用及代码示例

本文整理了Java中org.apache.commons.pool2.impl.GenericKeyedObjectPool.borrowObject()方法的一些代码示例,展示了GenericKey

本文整理了Java中org.apache.commons.pool2.impl.GenericKeyedObjectPool.borrowObject()方法的一些代码示例,展示了GenericKeyedObjectPool.borrowObject()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GenericKeyedObjectPool.borrowObject()方法的具体详情如下:
包路径:org.apache.commons.pool2.impl.GenericKeyedObjectPool
类名称:GenericKeyedObjectPool
方法名:borrowObject

GenericKeyedObjectPool.borrowObject介绍

[英]Equivalent to #borrowObject(Object,long)(key, #getMaxWaitMillis()).
[中]相当于#borrowObject(Object,long)(key, #getMaxWaitMillis())

代码示例

代码示例来源:origin: apache/kylin

if (getConfig().isQueryPreparedStatementCacheEnable() && prepareSqlRequest.isEnableStatementCache()) {
try {
preparedCOntext= preparedContextPool.borrowObject(preparedContextKey);
borrowPrepareCOntext= true;
} catch (NoSuchElementException noElementException) {

代码示例来源:origin: org.apache.commons/commons-pool2

/**
* Equivalent to {@link #borrowObject(Object, long) borrowObject}(key,
* {@link #getMaxWaitMillis()})
.
*


* {@inheritDoc}
*/
@Override
public T borrowObject(final K key) throws Exception {
return borrowObject(key, getMaxWaitMillis());
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testMaxTotalPerKey() throws Exception {
gkoPool.setMaxTotalPerKey(3);
gkoPool.setBlockWhenExhausted(false);
gkoPool.borrowObject("");
gkoPool.borrowObject("");
gkoPool.borrowObject("");
try {
gkoPool.borrowObject("");
fail("Expected NoSuchElementException");
} catch(final NoSuchElementException e) {
// expected
}
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Override
public Exception call() {
try {
signal.release();
final Object object3 = pool.borrowObject(Integer.valueOf(1));
pool.returnObject(Integer.valueOf(1), object3);
signal.release();
} catch (final Exception e) {
return e;
} catch (final Throwable e) {
return new Exception(e);
}
return null;
}
});

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testFIFO() throws Exception {
gkoPool.setLifo(false);
final String key = "key";
gkoPool.addObject(key); // "key0"
gkoPool.addObject(key); // "key1"
gkoPool.addObject(key); // "key2"
assertEquals("Oldest", "key0", gkoPool.borrowObject(key));
assertEquals("Middle", "key1", gkoPool.borrowObject(key));
assertEquals("Youngest", "key2", gkoPool.borrowObject(key));
final String s = gkoPool.borrowObject(key);
assertEquals("new-3", "key3", s);
gkoPool.returnObject(key, s);
assertEquals("returned", s, gkoPool.borrowObject(key));
assertEquals("new-4", "key4", gkoPool.borrowObject(key));
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testLIFO() throws Exception {
gkoPool.setLifo(true);
final String key = "key";
gkoPool.addObject(key); // "key0"
gkoPool.addObject(key); // "key1"
gkoPool.addObject(key); // "key2"
assertEquals("Youngest", "key2", gkoPool.borrowObject(key));
assertEquals("Middle", "key1", gkoPool.borrowObject(key));
assertEquals("Oldest", "key0", gkoPool.borrowObject(key));
final String s = gkoPool.borrowObject(key);
assertEquals("new-3", "key3", s);
gkoPool.returnObject(key, s);
assertEquals("returned", s, gkoPool.borrowObject(key));
assertEquals("new-4", "key4", gkoPool.borrowObject(key));
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testBlockedKeyDoesNotBlockPool() throws Exception {
gkoPool.setBlockWhenExhausted(true);
gkoPool.setMaxWaitMillis(5000);
gkoPool.setMaxTotalPerKey(1);
gkoPool.setMaxTotal(-1);
gkoPool.borrowObject("one");
final long start = System.currentTimeMillis();
// Needs to be in a separate thread as this will block
final Runnable simple = new SimpleTestThread<>(gkoPool, "one");
(new Thread(simple)).start();
// This should be almost instant. If it isn't it means this thread got
// stuck behind the thread created above which is bad.
// Give other thread a chance to start
Thread.sleep(1000);
gkoPool.borrowObject("two");
final long end = System.currentTimeMillis();
// If it fails it will be more than 4000ms (5000 less the 1000 sleep)
// If it passes it should be almost instant
// Use 3000ms as the threshold - should avoid timing issues on most
// (all? platforms)
assertTrue ("Elapsed time: "+(end-start)+" should be less than 4000",(end-start) <4000);
}

代码示例来源:origin: org.apache.commons/commons-pool2

/**
* Verifies that when a borrowed object is mutated in a way that does not
* preserve equality and hashcode, the pool can recognized it on return.
*
* JIRA: POOL-284
*/
@Test
public void testMutable() throws Exception {
final HashSetFactory factory = new HashSetFactory();
try (final GenericKeyedObjectPool> pool = new GenericKeyedObjectPool<>(factory,
new GenericKeyedObjectPoolConfig())) {
final HashSet s1 = pool.borrowObject("a");
final HashSet s2 = pool.borrowObject("a");
s1.add("One");
s2.add("One");
pool.returnObject("a", s1);
pool.returnObject("a", s2);
}
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testMaxTotalPerKeyZero() throws Exception {
gkoPool.setMaxTotalPerKey(0);
gkoPool.setBlockWhenExhausted(false);
try {
gkoPool.borrowObject("a");
fail("Expected NoSuchElementException");
} catch(final NoSuchElementException e) {
// expected
}
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testMaxTotalZero() throws Exception {
gkoPool.setMaxTotal(0);
gkoPool.setBlockWhenExhausted(false);
try {
gkoPool.borrowObject("a");
fail("Expected NoSuchElementException");
} catch(final NoSuchElementException e) {
// expected
}
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test
public void testValidationOnCreateOnly() throws Exception {
simpleFactory.enableValidation = true;
gkoPool.setMaxTotal(1);
gkoPool.setTestOnCreate(true);
gkoPool.setTestOnBorrow(false);
gkoPool.setTestOnReturn(false);
gkoPool.setTestWhileIdle(false);
final String o1 = gkoPool.borrowObject("KEY");
Assert.assertEquals("KEY0", o1);
final Timer t = new Timer();
t.schedule(
new TimerTask() {
@Override
public void run() {
gkoPool.returnObject("KEY", o1);
}
}, 3000);
final String o2 = gkoPool.borrowObject("KEY");
Assert.assertEquals("KEY0", o2);
Assert.assertEquals(1, simpleFactory.validateCounter);
}

代码示例来源:origin: org.apache.commons/commons-pool2

/**
* Verifies that when a factory's makeObject produces instances that are not
* discernible by equals, the pool can handle them.
*
* JIRA: POOL-283
*/
@Test
public void testEqualsIndiscernible() throws Exception {
final HashSetFactory factory = new HashSetFactory();
try (final GenericKeyedObjectPool> pool = new GenericKeyedObjectPool<>(factory,
new GenericKeyedObjectPoolConfig())) {
final HashSet s1 = pool.borrowObject("a");
final HashSet s2 = pool.borrowObject("a");
pool.returnObject("a", s1);
pool.returnObject("a", s2);
}
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testNegativeMaxTotalPerKey() throws Exception {
gkoPool.setMaxTotalPerKey(-1);
gkoPool.setBlockWhenExhausted(false);
final String obj = gkoPool.borrowObject("");
assertEquals("0",obj);
gkoPool.returnObject("",obj);
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testExceptionOnPassivateDuringReturn() throws Exception {
final String obj = gkoPool.borrowObject("one");
simpleFactory.setThrowExceptionOnPassivate(true);
gkoPool.returnObject("one", obj);
assertEquals(0,gkoPool.getNumIdle());
gkoPool.close();
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testExceptionOnDestroyDuringReturn() throws Exception {
simpleFactory.setThrowExceptionOnDestroy(true);
simpleFactory.setValidationEnabled(true);
gkoPool.setTestOnReturn(true);
final String obj1 = gkoPool.borrowObject("one");
gkoPool.borrowObject("one");
simpleFactory.setValid(false); // Make validation fail
gkoPool.returnObject("one", obj1);
assertEquals(1, gkoPool.getNumActive("one"));
assertEquals(0, gkoPool.getNumIdle("one"));
assertEquals(1, gkoPool.getNumActive());
assertEquals(0, gkoPool.getNumIdle());
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testMinIdle() throws Exception {
gkoPool.setMaxIdlePerKey(500);
gkoPool.setMinIdlePerKey(5);
gkoPool.setMaxTotalPerKey(10);
gkoPool.setNumTestsPerEvictionRun(0);
gkoPool.setMinEvictableIdleTimeMillis(50L);
gkoPool.setTimeBetweenEvictionRunsMillis(100L);
gkoPool.setTestWhileIdle(true);
//Generate a random key
final String key = "A";
gkoPool.preparePool(key);
try { Thread.sleep(150L); } catch(final InterruptedException e) { }
assertTrue("Should be 5 idle, found " + gkoPool.getNumIdle(),gkoPool.getNumIdle() == 5);
final String[] active = new String[5];
active[0] = gkoPool.borrowObject(key);
try { Thread.sleep(150L); } catch(final InterruptedException e) { }
assertTrue("Should be 5 idle, found " + gkoPool.getNumIdle(),gkoPool.getNumIdle() == 5);
for(int i=1 ; i<5 ; i++) {
active[i] = gkoPool.borrowObject(key);
}
try { Thread.sleep(150L); } catch(final InterruptedException e) { }
assertTrue("Should be 5 idle, found " + gkoPool.getNumIdle(),gkoPool.getNumIdle() == 5);
for(int i=0 ; i<5 ; i++) {
gkoPool.returnObject(key, active[i]);
}
try { Thread.sleep(150L); } catch(final InterruptedException e) { }
assertTrue("Should be 10 idle, found " + gkoPool.getNumIdle(),gkoPool.getNumIdle() == 10);
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testExceptionOnDestroyDuringBorrow() throws Exception {
simpleFactory.setThrowExceptionOnDestroy(true);
simpleFactory.setValidationEnabled(true);
gkoPool.setTestOnBorrow(true);
gkoPool.borrowObject("one");
simpleFactory.setValid(false); // Make validation fail on next borrow attempt
try {
gkoPool.borrowObject("one");
fail("Expecting NoSuchElementException");
} catch (final NoSuchElementException ex) {
// expected
}
assertEquals(1, gkoPool.getNumActive("one"));
assertEquals(0, gkoPool.getNumIdle("one"));
assertEquals(1, gkoPool.getNumActive());
assertEquals(0, gkoPool.getNumIdle());
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test(timeout=60000)
public void testMinIdleNoPreparePool() throws Exception {
gkoPool.setMaxIdlePerKey(500);
gkoPool.setMinIdlePerKey(5);
gkoPool.setMaxTotalPerKey(10);
gkoPool.setNumTestsPerEvictionRun(0);
gkoPool.setMinEvictableIdleTimeMillis(50L);
gkoPool.setTimeBetweenEvictionRunsMillis(100L);
gkoPool.setTestWhileIdle(true);
//Generate a random key
final String key = "A";
try { Thread.sleep(150L); } catch(final InterruptedException e) { }
assertTrue("Should be 0 idle, found " + gkoPool.getNumIdle(),gkoPool.getNumIdle() == 0);
final Object active = gkoPool.borrowObject(key);
assertNotNull(active);
try { Thread.sleep(150L); } catch(final InterruptedException e) { }
assertTrue("Should be 5 idle, found " + gkoPool.getNumIdle(),gkoPool.getNumIdle() == 5);
}

代码示例来源:origin: org.apache.commons/commons-pool2

@Test
public void testClientWaitStats() throws Exception {
final SimpleFactory factory = new SimpleFactory<>();
// Give makeObject a little latency
factory.setMakeLatency(200);
try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory,
new GenericKeyedObjectPoolConfig())) {
final String s = pool.borrowObject("one");
// First borrow waits on create, so wait time should be at least 200 ms
// Allow 100ms error in clock times
Assert.assertTrue(pool.getMaxBorrowWaitTimeMillis() >= 100);
Assert.assertTrue(pool.getMeanBorrowWaitTimeMillis() >= 100);
pool.returnObject("one", s);
pool.borrowObject("one");
// Second borrow does not have to wait on create, average should be about 100
Assert.assertTrue(pool.getMaxBorrowWaitTimeMillis() > 100);
Assert.assertTrue(pool.getMeanBorrowWaitTimeMillis() <200);
Assert.assertTrue(pool.getMeanBorrowWaitTimeMillis() > 20);
}
}

代码示例来源:origin: org.apache.commons/commons-pool2

/**
* Verifies that threads that get parked waiting for keys not in use
* when the pool is at maxTotal eventually get served.
*
* @throws Exception May occur in some failure modes
*/
@Test(timeout=60000)
public void testLivenessPerKey() throws Exception {
gkoPool.setMaxIdlePerKey(3);
gkoPool.setMaxTotal(3);
gkoPool.setMaxTotalPerKey(3);
gkoPool.setMaxWaitMillis(3000); // Really a timeout for the test
// Check out and briefly hold 3 "1"s
final WaitingTestThread t1 = new WaitingTestThread(gkoPool, "1", 100);
final WaitingTestThread t2 = new WaitingTestThread(gkoPool, "1", 100);
final WaitingTestThread t3 = new WaitingTestThread(gkoPool, "1", 100);
t1.start();
t2.start();
t3.start();
// Try to get a "2" while all capacity is in use.
// Thread will park waiting on empty queue. Verify it gets served.
gkoPool.borrowObject("2");
}

推荐阅读
author-avatar
掩不住的心
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有