use of org.apache.commons.pool2.KeyedPooledObjectFactory in project karaf by apache.
the class PooledConnectionFactory method initConnectionsPool.
public void initConnectionsPool() {
if (this.connectionsPool == null) {
this.connectionsPool = new GenericKeyedObjectPool<>(new KeyedPooledObjectFactory<ConnectionKey, ConnectionPool>() {
@Override
public void activateObject(ConnectionKey key, PooledObject<ConnectionPool> connection) throws Exception {
}
@Override
public void destroyObject(ConnectionKey key, PooledObject<ConnectionPool> connection) throws Exception {
try {
if (LOG.isTraceEnabled()) {
LOG.trace("Destroying connection: {}", connection);
}
connection.getObject().close();
} catch (Exception e) {
LOG.warn("Close connection failed for connection: " + connection + ". This exception will be ignored.", e);
}
}
@Override
public PooledObject<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 new DefaultPooledObject<>(connection);
}
@Override
public void passivateObject(ConnectionKey key, PooledObject<ConnectionPool> connection) throws Exception {
}
@Override
public boolean validateObject(ConnectionKey key, PooledObject<ConnectionPool> connection) {
if (connection != null && connection.getObject() != null && connection.getObject().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.setMaxIdlePerKey(1);
// We always want our validate method to control when idle objects are evicted.
this.connectionsPool.setTestOnBorrow(true);
this.connectionsPool.setTestWhileIdle(true);
}
}
Aggregations