Example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) 

Source Link

Document

Create a new GenericObjectPool using the specified values.

Usage

From source file:com.thoughtworks.studios.shine.cruise.stage.details.LazyStageGraphLoader.java

LazyStageGraphLoader(StageResourceImporter importer, StageStorage stageStorage,
        int transformerRegistryPoolSize) {
    this.importer = importer;
    this.stageStorage = stageStorage;
    transformerRegistryPool = new GenericObjectPool(new BasePoolableObjectFactory() {
        @Override//from   ww w . j  av a 2  s .  c  om
        public Object makeObject() throws Exception {
            return new XSLTTransformerRegistry();
        }

        @Override
        public void activateObject(Object obj) throws Exception {
            XSLTTransformerRegistry registry = (XSLTTransformerRegistry) obj;
            registry.reset();
        }
    }, transformerRegistryPoolSize, GenericObjectPool.WHEN_EXHAUSTED_GROW, GenericObjectPool.DEFAULT_MAX_WAIT);
}

From source file:com.nxttxn.vramel.components.rabbitMQ.RabbitMQProducer.java

/**
 * Open connection and initialize channel pool
 *///  w  w  w .  ja  va2  s  .co  m
private void openConnectionAndChannelPool() throws Exception {
    logger.trace("Creating connection...");
    this.conn = getEndpoint().connect(executorService);
    logger.debug("Created connection: {}", conn);

    logger.trace("Creating channel pool...");
    channelPool = new GenericObjectPool<Channel>(new PoolableChannelFactory(this.conn),
            getEndpoint().getChannelPoolMaxSize(), GenericObjectPool.WHEN_EXHAUSTED_BLOCK,
            getEndpoint().getChannelPoolMaxWait());
    if (getEndpoint().isDeclare()) {
        execute(new ChannelCallback<Void>() {
            @Override
            public Void doWithChannel(Channel channel) throws Exception {
                getEndpoint().declareExchangeAndQueue(channel);
                return null;
            }
        });
    }
}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test(expected = SQLException.class)
public void testBorrow() throws Exception {
    GenericObjectPool objPool = new GenericObjectPool(new UselessLifeguard(config(), true, false),
            maxPoolSize(), GenericObjectPool.WHEN_EXHAUSTED_BLOCK, timeToWait());
    objPool.setTestOnBorrow(true);/*from w  w w . j a va  2 s  . co  m*/
    objPool.setTestWhileIdle(true);
    overrideObjectPool(objPool);
    getConnection();
}

From source file:com.adaptris.jdbc.connection.FailoverDataSource.java

private void init(Properties p) {
    if (p == null) {
        throw new RuntimeException("No Configuration available ");
    }// ww w  . jav a  2  s  .c  o  m
    for (int i = 0; i < REQUIRED_PROPERTIES.length; i++) {
        if (!p.containsKey(REQUIRED_PROPERTIES[i])) {
            throw new RuntimeException("Missing Configuration " + REQUIRED_PROPERTIES[i]);
        }
    }
    databaseConfig = new FailoverConfig(p);
    poolMaximum = Integer.parseInt(p.getProperty(POOL_MAX_SIZE, "10"));
    poolTimeToWait = Integer.parseInt(p.getProperty(POOL_TIME_TO_WAIT, "20000"));
    pool = new GenericObjectPool(new PoolAttendant(databaseConfig), poolMaximum,
            GenericObjectPool.WHEN_EXHAUSTED_BLOCK, poolTimeToWait);
    pool.setTestOnBorrow(true);
    pool.setTestWhileIdle(true);
}

From source file:org.apache.camel.component.rabbitmq.RabbitMQProducer.java

/**
 * Open connection and initialize channel pool
 *//* w ww  .  j av a 2s  .  c  om*/
private void openConnectionAndChannelPool() throws Exception {
    log.trace("Creating connection...");
    this.conn = getEndpoint().connect(executorService);
    log.debug("Created connection: {}", conn);

    log.trace("Creating channel pool...");
    channelPool = new GenericObjectPool<Channel>(new PoolableChannelFactory(this.conn),
            getEndpoint().getChannelPoolMaxSize(), GenericObjectPool.WHEN_EXHAUSTED_BLOCK,
            getEndpoint().getChannelPoolMaxWait());
    if (getEndpoint().isDeclare()) {
        execute(new ChannelCallback<Void>() {
            @Override
            public Void doWithChannel(Channel channel) throws Exception {
                getEndpoint().declareExchangeAndQueue(channel);
                return null;
            }
        });
    }
}

From source file:org.dataconservancy.dcs.util.PooledKeyDigestPathAlgorithm.java

/**
 * Initializes a pool of 20 MessageDigest objects.  If the pool runs out of objects, the pool will grow.
 *
 * @param algo//from   w w w. ja  va 2  s  .  c  om
 * @param width
 * @param depth
 * @param suffix
 */
public PooledKeyDigestPathAlgorithm(String algo, int width, int depth, String suffix) {
    super(algo, width, depth, suffix);

    this.algorithm = algo;
    this.mdPool = new GenericObjectPool<MessageDigest>(new MessageDigestPoolableObjectFactory(), POOL_MAX_SIZE,
            GenericObjectPool.WHEN_EXHAUSTED_GROW, POOL_TIMEOUT);
}

From source file:org.onexus.collection.store.h2sql.internal.H2CollectionStore.java

protected DataSource newDataSource() {

    Driver.load();/*from www .  jav a2 s  .c  o m*/

    // Initialize the DataSource with a connection pool
    ConnectionFactory connectionFactory = new H2ConnectionFactory();
    ObjectPool connectionPool = new GenericObjectPool(null, GenericObjectPool.DEFAULT_MAX_ACTIVE,
            GenericObjectPool.WHEN_EXHAUSTED_GROW, GenericObjectPool.DEFAULT_MAX_WAIT);
    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    return new PoolingDataSource(connectionPool);
}

From source file:org.onexus.collection.store.mysql.internal.MysqlCollectionStore.java

protected DataSource newDataSource() {

    try {/*from  ww w  . jav  a2s.c  o  m*/
        Class.forName(Driver.class.getName());
    } catch (Exception e) {
        LOGGER.error("Exception: " + e.getMessage());
    }

    // Config parameters
    int maxActive = 8;
    try {
        maxActive = Integer.valueOf(getPoolMaxActive().trim()).intValue();
    } catch (Exception e) {
        LOGGER.error("Malformed config parameter 'poolMaxActive'");
    }

    byte whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
    try {
        if (getPoolWhenExhausted().trim().equalsIgnoreCase("FAIL")) {
            whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        }
        if (getPoolWhenExhausted().trim().equalsIgnoreCase("GROW")) {
            whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_GROW;
        }
    } catch (Exception e) {
        LOGGER.error("Malformed config parameter 'poolWhenExhausted'");
    }

    long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
    try {
        maxWait = Long.valueOf(getPoolMaxWait().trim()).longValue();
    } catch (Exception e) {
        LOGGER.error("Malformed config parameter 'poolMaxWait'");
    }

    // Initialize the DataSource with a connection pool
    ConnectionFactory connectionFactory = new MysqlConnectionFactory();
    GenericObjectPool connectionPool = new GenericObjectPool(null, maxActive, whenExhausted, maxWait);
    connectionPool.setTestWhileIdle(true);
    connectionPool.setTimeBetweenEvictionRunsMillis(3600);
    connectionPool.setMinEvictableIdleTimeMillis(3600);

    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    poolableConnectionFactory.setValidationQuery("SELECT 1");
    return new PoolingDataSource(connectionPool);

}

From source file:org.onexus.website.api.widgets.tags.tagstore.TagStoreManager.java

public void init() {
    LOGGER.debug("Connecting to '{}' as '{}'.", database, username);

    Driver.load();/*from ww w .  j  a  v a2 s . c  o  m*/

    // Initialize the DataSource with a connection pool
    ConnectionFactory connectionFactory = new H2ConnectionFactory();
    ObjectPool connectionPool = new GenericObjectPool(null, GenericObjectPool.DEFAULT_MAX_ACTIVE,
            GenericObjectPool.WHEN_EXHAUSTED_GROW, GenericObjectPool.DEFAULT_MAX_WAIT);
    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    dataSource = new PoolingDataSource(connectionPool);

    this.tagStores = Collections.synchronizedMap(new HashMap<String, TagStore>());

}

From source file:org.openanzo.combus.endpoint.BaseServiceListener.java

public void start() throws AnzoException {
    lock.lock();/*from  w w  w . j a  va2 s .com*/
    try {
        if (maxThreadPoolSize > 1) {
            threadPool = new GenericObjectPool(new ThreadPoolFactory(), maxThreadPoolSize,
                    GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 0);
            threadPool.setMinIdle(minThreadPoolSize);
            threadPool.setMaxIdle(minThreadPoolSize);
            threadPool.setMinEvictableIdleTimeMillis(10000);
        }
        started = true;
        if (consumer != null) {
            try {
                consumer.setMessageListener(this);
                lowConsumer.setMessageListener(this);
            } catch (JMSException jmsex) {
                log.error(LogUtils.COMBUS_MARKER, "Error setting up consumers", jmsex);
                throw new AnzoException(ExceptionConstants.COMBUS.JMS_SERVICE_EXCEPTION, jmsex);
            }
        }
    } finally {
        lock.unlock();
    }
}