List of usage examples for org.apache.commons.pool KeyedPoolableObjectFactory KeyedPoolableObjectFactory
KeyedPoolableObjectFactory
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericKeyedObjectPool.java
protected KeyedObjectPool makeEmptyPool(int mincapacity) { GenericKeyedObjectPool pool = new GenericKeyedObjectPool(new KeyedPoolableObjectFactory() { HashMap map = new HashMap(); public Object makeObject(Object key) { int counter = 0; Integer Counter = (Integer) (map.get(key)); if (null != Counter) { counter = Counter.intValue(); }/*ww w .j a v a 2s.com*/ map.put(key, new Integer(counter + 1)); return String.valueOf(key) + String.valueOf(counter); } public void destroyObject(Object key, Object obj) { } public boolean validateObject(Object key, Object obj) { return true; } public void activateObject(Object key, Object obj) { } public void passivateObject(Object key, Object obj) { } }); pool.setMaxActive(mincapacity); pool.setMaxIdle(mincapacity); return pool; }
From source file:org.apache.aries.transaction.jms.internal.ConnectionPool.java
public ConnectionPool(Connection connection) { this.connection = wrap(connection); // Create our internal Pool of session instances. this.sessionPool = new GenericKeyedObjectPool<SessionKey, PooledSession>( new KeyedPoolableObjectFactory<SessionKey, PooledSession>() { @Override//w ww . ja va 2 s . c o m public void activateObject(SessionKey key, PooledSession session) throws Exception { ConnectionPool.this.loanedSessions.add(session); } @Override public void destroyObject(SessionKey key, PooledSession session) throws Exception { ConnectionPool.this.loanedSessions.remove(session); session.getInternalSession().close(); } @Override public PooledSession makeObject(SessionKey key) throws Exception { Session session = makeSession(key); return new PooledSession(key, session, sessionPool, key.isTransacted(), useAnonymousProducers); } @Override public void passivateObject(SessionKey key, PooledSession session) throws Exception { ConnectionPool.this.loanedSessions.remove(session); } @Override public boolean validateObject(SessionKey key, PooledSession session) { return true; } }); }
From source file:org.apache.aries.transaction.jms.PooledConnectionFactory.java
public void initConnectionsPool() { if (this.connectionsPool == null) { this.connectionsPool = new GenericKeyedObjectPool<ConnectionKey, ConnectionPool>( new KeyedPoolableObjectFactory<ConnectionKey, ConnectionPool>() { @Override/* ww w. j a v a2s . c om*/ public void activateObject(ConnectionKey key, ConnectionPool connection) throws Exception { } @Override public void destroyObject(ConnectionKey key, ConnectionPool connection) throws Exception { try { if (LOG.isTraceEnabled()) { LOG.trace("Destroying connection: {}", connection); } connection.close(); } catch (Exception e) { LOG.warn("Close connection failed for connection: " + connection + ". This exception will be ignored.", e); } } @Override public ConnectionPool makeObject(ConnectionKey key) throws Exception { Connection delegate = createConnection(key); ConnectionPool connection = createConnectionPool(delegate); connection.setIdleTimeout(getIdleTimeout()); connection.setExpiryTimeout(getExpiryTimeout()); connection.setMaximumActiveSessionPerConnection(getMaximumActiveSessionPerConnection()); connection.setBlockIfSessionPoolIsFull(isBlockIfSessionPoolIsFull()); if (isBlockIfSessionPoolIsFull() && getBlockIfSessionPoolIsFullTimeout() > 0) { connection.setBlockIfSessionPoolIsFullTimeout(getBlockIfSessionPoolIsFullTimeout()); } connection.setUseAnonymousProducers(isUseAnonymousProducers()); if (LOG.isTraceEnabled()) { LOG.trace("Created new connection: {}", connection); } return connection; } @Override public void passivateObject(ConnectionKey key, ConnectionPool connection) throws Exception { } @Override public boolean validateObject(ConnectionKey key, ConnectionPool connection) { if (connection != null && connection.expiredCheck()) { if (LOG.isTraceEnabled()) { LOG.trace("Connection has expired: {} and will be destroyed", connection); } return false; } return true; } }); // Set max idle (not max active) since our connections always idle in the pool. this.connectionsPool.setMaxIdle(1); // We always want our validate method to control when idle objects are evicted. this.connectionsPool.setTestOnBorrow(true); this.connectionsPool.setTestWhileIdle(true); } }
From source file:org.glassfish.grizzly.memcached.pool.BaseObjectPoolBenchmark.java
@Test public void testBenchmarking() { final int loop = 2 * 1000 * 1000; final int poolSize = 150; final KeyedPoolableObjectFactory<String, String> apacheFactory = new KeyedPoolableObjectFactory<String, String>() { private static final String VALUE_NAME = "value"; private int id; private int count; @Override/*from ww w . j a v a2 s. c o m*/ public synchronized String makeObject(String s) throws Exception { count++; return VALUE_NAME + ++id; } @Override public synchronized void destroyObject(String s, String s1) throws Exception { count--; } @Override public boolean validateObject(String s, String s1) { return true; } @Override public void activateObject(String s, String s1) throws Exception { } @Override public void passivateObject(String s, String s1) throws Exception { } }; final GenericKeyedObjectPool<String, String> apachePool = new GenericKeyedObjectPool<String, String>( apacheFactory, poolSize, (byte) 0, 0, poolSize, poolSize, poolSize, false, false, 1000 * 60 * 60, 0, 1000 * 60 * 60, false); String object = null; long startTime = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { try { object = apachePool.borrowObject("key"); } catch (Exception e) { e.printStackTrace(); } try { apachePool.returnObject("key", object); } catch (Exception e) { e.printStackTrace(); } } logger.info("apache common-pool elapse = {}", (System.currentTimeMillis() - startTime)); try { apachePool.close(); } catch (Exception ignore) { } // grizzly final PoolableObjectFactory<String, String> grizzlyFactory = new PoolableObjectFactory<String, String>() { private static final String VALUE_NAME = "value"; private int id; private int count; @Override public synchronized String createObject(String s) throws Exception { count++; return VALUE_NAME + ++id; } @Override public synchronized void destroyObject(String s, String s1) throws Exception { count--; } @Override public boolean validateObject(String s, String s1) { return true; } }; final BaseObjectPool.Builder<String, String> builder = new BaseObjectPool.Builder<String, String>( grizzlyFactory); builder.disposable(false); builder.keepAliveTimeoutInSecs(-1); builder.borrowValidation(false); builder.returnValidation(false); builder.max(poolSize); builder.min(poolSize); final ObjectPool<String, String> grizzlyPool = builder.build(); startTime = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { try { object = grizzlyPool.borrowObject("key", 100); } catch (Exception e) { e.printStackTrace(); } try { grizzlyPool.returnObject("key", object); } catch (Exception e) { e.printStackTrace(); } } logger.info("grizzly pool elapse = {}", (System.currentTimeMillis() - startTime)); grizzlyPool.destroy(); }
From source file:org.glassfish.grizzly.memcached.pool.BaseObjectPoolBenchmark.java
@Test public void testBenchmarkingInMultiThreads() { final int threadCount = 1000; final int poolSize = 150; final KeyedPoolableObjectFactory<String, String> apacheFactory = new KeyedPoolableObjectFactory<String, String>() { private static final String VALUE_NAME = "value"; private int id; private int count; @Override//w ww. j a v a 2 s .co m public synchronized String makeObject(String s) throws Exception { count++; return VALUE_NAME + ++id; } @Override public synchronized void destroyObject(String s, String s1) throws Exception { count--; } @Override public boolean validateObject(String s, String s1) { return true; } @Override public void activateObject(String s, String s1) throws Exception { } @Override public void passivateObject(String s, String s1) throws Exception { } }; final GenericKeyedObjectPool<String, String> apachePool = new GenericKeyedObjectPool<String, String>( apacheFactory, poolSize, GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, poolSize, poolSize, poolSize, false, false, 1000 * 60 * 60, 0, 1000 * 60 * 60, false); final ConcurrentLinkedQueue<String> borrowObjects = new ConcurrentLinkedQueue<String>(); final CountDownLatch startFlag = new CountDownLatch(1); final CountDownLatch finishFlag = new CountDownLatch(threadCount * 2); final AtomicInteger exceptionCnt = new AtomicInteger(); final AtomicInteger successCnt = new AtomicInteger(); for (int i = 0; i < threadCount; i++) { final Thread borrowThread = new Thread() { @Override public void run() { try { startFlag.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } for (int j = 0; j < 30; j++) { try { final String object = apachePool.borrowObject("key"); Assert.assertNotNull(object); successCnt.incrementAndGet(); Assert.assertTrue(borrowObjects.offer(object)); } catch (Exception ignore) { exceptionCnt.incrementAndGet(); } } finishFlag.countDown(); } }; borrowThread.start(); final Thread returnThread = new Thread() { @Override public void run() { try { startFlag.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } for (int j = 0; j < 30; j++) { try { final String object = borrowObjects.poll(); if (object != null) { apachePool.returnObject("key", object); } } catch (Exception e) { Assert.fail(e.getMessage()); } } finishFlag.countDown(); } }; returnThread.start(); } long startTime = System.currentTimeMillis(); startFlag.countDown(); try { finishFlag.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } logger.info("apache common-pool elapse = {}", (System.currentTimeMillis() - startTime)); try { logger.info("apache common-pool max gen-id = {}", apacheFactory.makeObject("key")); } catch (Exception ignore) { } logger.info("apache common-pool success counts = {}", successCnt.get()); logger.info("apache common-pool exception counts = {}", exceptionCnt.get()); try { apachePool.close(); } catch (Exception ignore) { } // grizzly final PoolableObjectFactory<String, String> grizzlyFactory = new PoolableObjectFactory<String, String>() { private static final String VALUE_NAME = "value"; private int id; private int count; @Override public synchronized String createObject(String s) throws Exception { count++; return VALUE_NAME + ++id; } @Override public synchronized void destroyObject(String s, String s1) throws Exception { count--; } @Override public boolean validateObject(String s, String s1) { return true; } }; final BaseObjectPool.Builder<String, String> builder = new BaseObjectPool.Builder<String, String>( grizzlyFactory); builder.disposable(false); builder.keepAliveTimeoutInSecs(-1); builder.borrowValidation(false); builder.returnValidation(false); builder.max(poolSize); builder.min(poolSize); final ObjectPool<String, String> grizzlyPool = builder.build(); final ConcurrentLinkedQueue<String> borrowObjects2 = new ConcurrentLinkedQueue<String>(); final CountDownLatch startFlag2 = new CountDownLatch(1); final CountDownLatch finishFlag2 = new CountDownLatch(threadCount * 2); final AtomicInteger exceptionCnt2 = new AtomicInteger(); final AtomicInteger successCnt2 = new AtomicInteger(); for (int i = 0; i < threadCount; i++) { final Thread borrowThread = new Thread() { @Override public void run() { try { startFlag2.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } for (int j = 0; j < 30; j++) { try { final String object = grizzlyPool.borrowObject("key", 0); Assert.assertNotNull(object); successCnt2.incrementAndGet(); Assert.assertTrue(borrowObjects2.offer(object)); } catch (Exception ignore) { exceptionCnt2.incrementAndGet(); } } finishFlag2.countDown(); } }; borrowThread.start(); final Thread returnThread = new Thread() { @Override public void run() { try { startFlag2.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } for (int j = 0; j < 30; j++) { try { final String object = borrowObjects2.poll(); if (object != null) { grizzlyPool.returnObject("key", object); } } catch (Exception e) { Assert.fail(e.getMessage()); } } finishFlag2.countDown(); } }; returnThread.start(); } startTime = System.currentTimeMillis(); startFlag2.countDown(); try { finishFlag2.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } logger.info("grizzly pool elapse = {}", (System.currentTimeMillis() - startTime)); try { logger.info("grizzly pool max gen-id = {}", grizzlyFactory.createObject("key")); } catch (Exception ignore) { } logger.info("grizzly pool success counts = {}", successCnt2.get()); logger.info("grizzly pool exception counts= {}", exceptionCnt2.get()); grizzlyPool.destroy(); }