List of usage examples for org.apache.commons.pool.impl GenericObjectPool returnObject
public void returnObject(Object obj) throws Exception
Returns an object instance to the pool.
If #getMaxIdle() maxIdle is set to a positive value and the number of idle instances has reached this value, the returning instance is destroyed.
If #getTestOnReturn() testOnReturn == true, the returning instance is validated before being returned to the idle instance pool.
From source file:TestObjectPool.java
public static void main(String args[]) throws Exception { GenericObjectPool pool = new GenericObjectPool(); pool.setFactory(new EmployeeFactory()); /*First way of initializing pool pool.setMinIdle(5);/*from w w w . j a va 2 s . com*/ pool.setTimeBetweenEvictionRunsMillis(500); Thread.currentThread().sleep(600);*/ /* second, and preferred way */ for(int i = 0; i < 5; ++i) { pool.addObject(); } // pool.setTestOnReturn(true); pool.setMinEvictableIdleTimeMillis(1000); pool.setTimeBetweenEvictionRunsMillis(600); System.err.println("Number of employees in pool: " + pool.getNumIdle()); Thread.currentThread().sleep(1500); Employee employee = (Employee)pool.borrowObject(); employee.setName("Fred Flintstone"); employee.doWork(); System.err.println("Employee: " + employee); pool.returnObject(employee); System.err.println("Number of employees in pool: " + pool.getNumIdle()); }
From source file:agiato.cassandra.data.DataManager.java
/** * @param connectionPool// w ww . jav a2s .c om * @param connector * @throws Exception */ public void returnConnection(GenericObjectPool connectionPool, Connector connector) throws Exception { connectionPool.returnObject(connector); }
From source file:agiato.cassandra.data.DataManager.java
/** * @param connector// ww w. ja v a2s . c o m * @throws Exception */ public void returnConnection(Connector connector) throws Exception { GenericObjectPool connectionPool = loadBalancer.findConnectionPool(connector.getHost()); connectionPool.returnObject(connector); }
From source file:Pool162.java
public void testWhenExhaustedBlockInterupt() throws Exception { GenericObjectPool pool = new GenericObjectPool(new SimpleFactory()); //Set this value to 1 to get the deadlock. No deadlock when it sets to //other values. pool.setMaxActive(2);//from w ww . j a v a2 s.com pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); Object obj1 = pool.borrowObject(); // Make sure one object was obtained if (obj1 == null) { throw new Exception("obj1 is null"); } // Create a separate thread to try and borrow another object WaitingTestThread wtt = new WaitingTestThread(pool, 200); wtt.start(); // Give wtt time to start Thread.sleep(200); //bug trigger #1 wtt.interrupt(); //bug trigger #1 // Give interrupt time to take effect Thread.sleep(200); // Return object to the pool pool.returnObject(obj1); Object obj2 = null; obj2 = pool.borrowObject(); if (obj2 == null) { throw new Exception("obj2 is null"); } pool.returnObject(obj2); pool.close(); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testExceptionOnPassivateDuringReturn() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericObjectPool pool = new GenericObjectPool(factory); Object obj = pool.borrowObject(); factory.setThrowExceptionOnPassivate(true); pool.returnObject(obj); assertEquals(0, pool.getNumIdle());/*from w w w . jav a 2s .co m*/ pool.close(); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testExceptionOnActivateDuringBorrow() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericObjectPool pool = new GenericObjectPool(factory); Object obj1 = pool.borrowObject(); Object obj2 = pool.borrowObject(); pool.returnObject(obj1); pool.returnObject(obj2);/*from w w w. j a v a2s .co m*/ factory.setThrowExceptionOnActivate(true); factory.setEvenValid(false); // Activation will now throw every other time // First attempt throws, but loop continues and second succeeds Object obj = pool.borrowObject(); assertEquals(1, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); pool.returnObject(obj); factory.setValid(false); // Validation will now fail on activation when borrowObject returns // an idle instance, and then when attempting to create a new instance try { obj1 = pool.borrowObject(); fail("Expecting NoSuchElementException"); } catch (NoSuchElementException ex) { // expected } assertEquals(0, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testSetFactoryWithNoActiveObjects() throws Exception { GenericObjectPool pool = new GenericObjectPool(); pool.setMaxIdle(10);// w w w . j a v a2 s . c o m pool.setFactory(new SimpleFactory()); Object obj = pool.borrowObject(); pool.returnObject(obj); assertEquals(1, pool.getNumIdle()); pool.setFactory(new SimpleFactory()); assertEquals(0, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testExceptionOnDestroyDuringReturn() throws Exception { SimpleFactory factory = new SimpleFactory(); factory.setThrowExceptionOnDestroy(true); GenericObjectPool pool = new GenericObjectPool(factory); pool.setTestOnReturn(true);// w w w . j a v a 2s .co m Object obj1 = pool.borrowObject(); pool.borrowObject(); factory.setValid(false); // Make validation fail pool.returnObject(obj1); assertEquals(1, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
private void checkEvictorVisiting(boolean lifo) throws Exception { VisitTrackerFactory factory = new VisitTrackerFactory(); GenericObjectPool pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(2);/*from w w w. j a va 2s .c o m*/ pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); for (int i = 0; i < 8; i++) { pool.addObject(); } pool.evict(); // Visit oldest 2 - 0 and 1 Object obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); // borrow, return, borrow, return // FIFO will move 0 and 1 to end // LIFO, 7 out, then in, then out, then in pool.evict(); // Should visit 2 and 3 in either case for (int i = 0; i < 8; i++) { VisitTracker tracker = (VisitTracker) pool.borrowObject(); if (tracker.getId() >= 4) { assertEquals("Unexpected instance visited " + tracker.getId(), 0, tracker.getValidateCount()); } else { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1, tracker.getValidateCount()); } } factory = new VisitTrackerFactory(); pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(3); pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); for (int i = 0; i < 8; i++) { pool.addObject(); } pool.evict(); // 0, 1, 2 pool.evict(); // 3, 4, 5 obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); // borrow, return, borrow, return // FIFO 3,4,5,6,7,0,1,2 // LIFO 7,6,5,4,3,2,1,0 // In either case, pointer should be at 6 pool.evict(); // Should hit 6,7,0 - 0 for second time for (int i = 0; i < 8; i++) { VisitTracker tracker = (VisitTracker) pool.borrowObject(); if (tracker.getId() != 0) { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1, tracker.getValidateCount()); } else { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 2, tracker.getValidateCount()); } } // Randomly generate a pools with random numTests // and make sure evictor cycles through elements appropriately int[] smallPrimes = { 2, 3, 5, 7 }; Random random = new Random(); random.setSeed(System.currentTimeMillis()); for (int i = 0; i < 4; i++) { pool.setNumTestsPerEvictionRun(smallPrimes[i]); for (int j = 0; j < 5; j++) { pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(3); pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); pool.setMaxIdle(-1); int instanceCount = 10 + random.nextInt(20); pool.setMaxActive(instanceCount); for (int k = 0; k < instanceCount; k++) { pool.addObject(); } // Execute a random number of evictor runs int runs = 10 + random.nextInt(50); for (int k = 0; k < runs; k++) { pool.evict(); } // Number of times evictor should have cycled through the pool int cycleCount = (runs * pool.getNumTestsPerEvictionRun()) / instanceCount; // Look at elements and make sure they are visited cycleCount // or cycleCount + 1 times VisitTracker tracker = null; int visitCount = 0; for (int k = 0; k < instanceCount; k++) { tracker = (VisitTracker) pool.borrowObject(); assertTrue(pool.getNumActive() <= pool.getMaxActive()); visitCount = tracker.getValidateCount(); assertTrue(visitCount >= cycleCount && visitCount <= cycleCount + 1); } } } }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testEvictionSoftMinIdle() throws Exception { GenericObjectPool pool = null; class TimeTest extends BasePoolableObjectFactory { private final long createTime; public TimeTest() { createTime = System.currentTimeMillis(); }//from www . jav a2 s. com public Object makeObject() throws Exception { return new TimeTest(); } public long getCreateTime() { return createTime; } } pool = new GenericObjectPool(new TimeTest()); pool.setMaxIdle(5); pool.setMaxActive(5); pool.setNumTestsPerEvictionRun(5); pool.setMinEvictableIdleTimeMillis(3000L); pool.setSoftMinEvictableIdleTimeMillis(1000L); pool.setMinIdle(2); Object[] active = new Object[5]; Long[] creationTime = new Long[5]; for (int i = 0; i < 5; i++) { active[i] = pool.borrowObject(); creationTime[i] = new Long(((TimeTest) active[i]).getCreateTime()); } for (int i = 0; i < 5; i++) { pool.returnObject(active[i]); } // Soft evict all but minIdle(2) Thread.sleep(1500L); pool.evict(); assertEquals("Idle count different than expected.", 2, pool.getNumIdle()); // Hard evict the rest. Thread.sleep(2000L); pool.evict(); assertEquals("Idle count different than expected.", 0, pool.getNumIdle()); }