Search in sources :

Example 1 with KeyedPooledObjectFactory

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);
    }
}
Also used : DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) KeyedPooledObjectFactory(org.apache.commons.pool2.KeyedPooledObjectFactory) JMSException(javax.jms.JMSException)

Aggregations

Connection (javax.jms.Connection)1 JMSException (javax.jms.JMSException)1 QueueConnection (javax.jms.QueueConnection)1 TopicConnection (javax.jms.TopicConnection)1 KeyedPooledObjectFactory (org.apache.commons.pool2.KeyedPooledObjectFactory)1 PooledObject (org.apache.commons.pool2.PooledObject)1 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)1