/** * Borrows an object from the {@code ObjectPool}. */ @Override public Object getTarget() throws Exception { Assert.state(this.pool != null, "No Commons ObjectPool available"); return this.pool.borrowObject(); }
public void sendMessage() throws Exception { final SMPPSession session = sessionPool.borrowObject(); try { final String messageId = session.submitShortMessage( serviceType, sourceTon, sourceNpi, sourceAddress, destinationTon, destinationNpi, destinationAddress, esm, protocolId, priorityFlag, scheduleDeliveryTime, validityPeriod, registeredDelivery, replaceIfPresentFlag, dataCoding, smDefaultMsgId, message ); Log.debug( "Message submitted, message_id is '{}'.", messageId ); } finally { sessionPool.returnObject( session ); } } }
/** * {@inheritDoc} */ @Override public T borrowObject() throws Exception, NoSuchElementException, IllegalStateException { return pool.borrowObject(); }
/** * {@inheritDoc} */ @Override public T borrowObject() throws Exception, NoSuchElementException, IllegalStateException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { return pool.borrowObject(); } finally { writeLock.unlock(); } }
@SuppressWarnings("unchecked") @Override public T borrowObject() throws Exception, NoSuchElementException, IllegalStateException { UsageTracking<T> usageTracking = null; if (pool instanceof UsageTracking) { usageTracking = (UsageTracking<T>) pool; } final T pooledObject = pool.borrowObject(); final T proxy = proxySource.createProxy(pooledObject, usageTracking); return proxy; }
@Test public void testUsageTracking() throws Exception { final TestObject obj = pool.borrowObject(); assertNotNull(obj); // Use the object to trigger collection of last used stack trace obj.setData(DATA1); // Sleep long enough for the object to be considered abandoned Thread.sleep((ABANDONED_TIMEOUT_SECS + 2) * 1000); // Borrow another object to trigger the abandoned object processing pool.borrowObject(); final String logOutput = log.getBuffer().toString(); assertTrue(logOutput.contains("Pooled object created")); assertTrue(logOutput.contains("The last code to use this object was")); }
obj = _pool.borrowObject(); } catch(final Exception e) { _error = e;
@Test public void testBaseBorrow() throws Exception { try { _pool = makeEmptyPool(3); } catch(final UnsupportedOperationException e) { return; // skip this test if unsupported } assertEquals(getNthObject(0),_pool.borrowObject()); assertEquals(getNthObject(1),_pool.borrowObject()); assertEquals(getNthObject(2),_pool.borrowObject()); _pool.close(); }
@Test public void testBaseClosePool() throws Exception { try { _pool = makeEmptyPool(3); } catch(final UnsupportedOperationException e) { return; // skip this test if unsupported } final String obj = _pool.borrowObject(); _pool.returnObject(obj); _pool.close(); try { _pool.borrowObject(); fail("Expected IllegalStateException"); } catch(final IllegalStateException e) { // expected } }
private static List<String> invokeEveryMethod(final ObjectPool<Object> op) throws Exception { op.addObject(); op.borrowObject(); op.clear(); op.close(); op.getNumActive(); op.getNumIdle(); op.invalidateObject(new Object()); op.returnObject(new Object()); op.toString(); final List<String> expectedMethods = Arrays.asList(new String[] { "addObject", "borrowObject", "clear", "close", "getNumActive", "getNumIdle", "invalidateObject", "returnObject", "toString" }); return expectedMethods; }
@Test public void testBorrowObject() throws Exception { final TestObject obj = pool.borrowObject(); assertNotNull(obj); // Make sure proxied methods are working obj.setData(DATA1); assertEquals(DATA1, obj.getData()); pool.returnObject(obj); }
return; // skip this test if unsupported String obj0 = _pool.borrowObject(); assertEquals(getNthObject(0),obj0); String obj1 = _pool.borrowObject(); assertEquals(getNthObject(1),obj1); String obj2 = _pool.borrowObject(); assertEquals(getNthObject(2),obj2); _pool.returnObject(obj2); obj2 = _pool.borrowObject(); assertEquals(getNthObject(2),obj2); _pool.returnObject(obj1); obj1 = _pool.borrowObject(); assertEquals(getNthObject(1),obj1); _pool.returnObject(obj0); _pool.returnObject(obj2); obj2 = _pool.borrowObject(); if (isLifo()) { assertEquals(getNthObject(2),obj2); obj0 = _pool.borrowObject(); if (isLifo()) { assertEquals(getNthObject(0),obj0);
@Test(expected=IllegalStateException.class) public void testAccessAfterInvalidate() throws Exception { final TestObject obj = pool.borrowObject(); assertNotNull(obj); // Make sure proxied methods are working obj.setData(DATA1); assertEquals(DATA1, obj.getData()); pool.invalidateObject(obj); assertNotNull(obj); obj.getData(); }
@Test(expected=IllegalStateException.class) public void testAccessAfterReturn() throws Exception { final TestObject obj = pool.borrowObject(); assertNotNull(obj); // Make sure proxied methods are working obj.setData(DATA1); assertEquals(DATA1, obj.getData()); pool.returnObject(obj); assertNotNull(obj); obj.getData(); }
obj = pool.borrowObject(); clear(factory, expectedMethods); obj = pool.borrowObject(); clear(factory, expectedMethods); factory.setDestroyObjectFail(true);
@Test public void testBaseClear() throws Exception { try { _pool = makeEmptyPool(3); } catch(final UnsupportedOperationException e) { return; // skip this test if unsupported } assertEquals(0,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); final String obj0 = _pool.borrowObject(); final String obj1 = _pool.borrowObject(); assertEquals(2,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); _pool.returnObject(obj1); _pool.returnObject(obj0); assertEquals(0,_pool.getNumActive()); assertEquals(2,_pool.getNumIdle()); _pool.clear(); assertEquals(0,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); final Object obj2 = _pool.borrowObject(); assertEquals(getNthObject(2),obj2); _pool.close(); }
@Test public void testBaseInvalidateObject() throws Exception { try { _pool = makeEmptyPool(3); } catch(final UnsupportedOperationException e) { return; // skip this test if unsupported } assertEquals(0,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); final String obj0 = _pool.borrowObject(); final String obj1 = _pool.borrowObject(); assertEquals(2,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); _pool.invalidateObject(obj0); assertEquals(1,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); _pool.invalidateObject(obj1); assertEquals(0,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); _pool.close(); }
@Test public void testBaseNumActiveNumIdle() throws Exception { try { _pool = makeEmptyPool(3); } catch(final UnsupportedOperationException e) { return; // skip this test if unsupported } assertEquals(0,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); final String obj0 = _pool.borrowObject(); assertEquals(1,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); final String obj1 = _pool.borrowObject(); assertEquals(2,_pool.getNumActive()); assertEquals(0,_pool.getNumIdle()); _pool.returnObject(obj1); assertEquals(1,_pool.getNumActive()); assertEquals(1,_pool.getNumIdle()); _pool.returnObject(obj0); assertEquals(0,_pool.getNumActive()); assertEquals(2,_pool.getNumIdle()); _pool.close(); }
return; // test not supported final Object o1 = pool.borrowObject(); final Object o2 = pool.borrowObject(); pool.borrowObject(); fail("A closed pool must throw an IllegalStateException when borrowObject is called."); } catch (final IllegalStateException ise) {
@Test public void testBaseAddObject() throws Exception { try { _pool = makeEmptyPool(3); } catch(final UnsupportedOperationException e) { return; // skip this test if unsupported } try { assertEquals(0,_pool.getNumIdle()); assertEquals(0,_pool.getNumActive()); _pool.addObject(); assertEquals(1,_pool.getNumIdle()); assertEquals(0,_pool.getNumActive()); final String obj = _pool.borrowObject(); assertEquals(getNthObject(0),obj); assertEquals(0,_pool.getNumIdle()); assertEquals(1,_pool.getNumActive()); _pool.returnObject(obj); assertEquals(1,_pool.getNumIdle()); assertEquals(0,_pool.getNumActive()); } catch(final UnsupportedOperationException e) { return; // skip this test if one of those calls is unsupported } finally { _pool.close(); } }