Search in sources :

Example 31 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project fess-crawler by codelibs.

the class WebDriverClientTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    CrawlerPooledObjectFactory<CrawlerWebDriver> pooledObjectFactory = new CrawlerPooledObjectFactory<>();
    pooledObjectFactory.setComponentName("webDriver");
    pooledObjectFactory.setOnDestroyListener(p -> {
        final CrawlerWebDriver driver = p.getObject();
        driver.quit();
    });
    final StandardCrawlerContainer container = new StandardCrawlerContainer();
    container.prototype("webDriver", CrawlerWebDriver.class).singleton("mimeTypeHelper", MimeTypeHelperImpl.class).singleton("pooledObjectFactory", pooledObjectFactory).singleton("webDriverPool", new GenericObjectPool<>(pooledObjectFactory), null, pool -> {
        pool.close();
    }).<AOnClickAction>singleton("aOnClickAction", AOnClickAction.class).<FormAction>singleton("formAction", FormAction.class).<WebDriverClient>singleton("webDriverClient", WebDriverClient.class, client -> {
        AOnClickAction aOnClick = container.getComponent("aOnClickAction");
        aOnClick.setName("aOnClick");
        aOnClick.setCssQuery("a");
        client.addUrlAction(aOnClick);
        FormAction formAction = container.getComponent("formAction");
        formAction.setName("form");
        formAction.setCssQuery("form");
        client.addUrlAction(formAction);
    });
    webDriverClient = container.getComponent("webDriverClient");
}
Also used : StandardCrawlerContainer(org.codelibs.fess.crawler.container.StandardCrawlerContainer) MimeTypeHelperImpl(org.codelibs.fess.crawler.helper.impl.MimeTypeHelperImpl) Iterator(java.util.Iterator) CrawlerPooledObjectFactory(org.codelibs.fess.crawler.pool.CrawlerPooledObjectFactory) ResourceUtil(org.codelibs.core.io.ResourceUtil) PlainTestCase(org.dbflute.utflute.core.PlainTestCase) CrawlerWebDriver(org.codelibs.fess.crawler.client.http.webdriver.CrawlerWebDriver) Resource(javax.annotation.Resource) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Set(java.util.Set) File(java.io.File) AOnClickAction(org.codelibs.fess.crawler.client.http.action.AOnClickAction) SystemUtil(org.codelibs.core.lang.SystemUtil) Constants(org.codelibs.fess.crawler.Constants) RequestData(org.codelibs.fess.crawler.entity.RequestData) FormAction(org.codelibs.fess.crawler.client.http.action.FormAction) WebDriverClient(org.codelibs.fess.crawler.client.http.WebDriverClient) CrawlerWebServer(org.codelibs.fess.crawler.util.CrawlerWebServer) RequestDataBuilder(org.codelibs.fess.crawler.builder.RequestDataBuilder) InputStreamUtil(org.codelibs.core.io.InputStreamUtil) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) AOnClickAction(org.codelibs.fess.crawler.client.http.action.AOnClickAction) WebDriverClient(org.codelibs.fess.crawler.client.http.WebDriverClient) FormAction(org.codelibs.fess.crawler.client.http.action.FormAction) CrawlerWebDriver(org.codelibs.fess.crawler.client.http.webdriver.CrawlerWebDriver) StandardCrawlerContainer(org.codelibs.fess.crawler.container.StandardCrawlerContainer) CrawlerPooledObjectFactory(org.codelibs.fess.crawler.pool.CrawlerPooledObjectFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) MimeTypeHelperImpl(org.codelibs.fess.crawler.helper.impl.MimeTypeHelperImpl)

Example 32 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project athenz by yahoo.

the class DataSourceFactory method create.

static PoolableDataSource create(ConnectionFactory connectionFactory) {
    // setup our pool config object
    GenericObjectPoolConfig config = setupPoolConfig();
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // Set max lifetime of a connection in milli-secs, after which it will
    // always fail activation, passivation, and validation.
    // Value of -1 means infinite life time. The default value
    // defined in this class is 10 minutes.
    long connTtlMillis = retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TTL, MAX_TTL_CONN_MS);
    poolableConnectionFactory.setMaxConnLifetimeMillis(connTtlMillis);
    if (LOG.isInfoEnabled()) {
        LOG.info("Setting Time-To-Live interval for live connections ({}) msecs", connTtlMillis);
    }
    // set the validation query for our jdbc connector
    final String validationQuery = System.getProperty(ATHENZ_PROP_DBPOOL_VALIDATION_QUERY, MYSQL_VALIDATION_QUERY);
    poolableConnectionFactory.setValidationQuery(validationQuery);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    return new AthenzDataSource(connectionPool);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 33 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project atlasdb by palantir.

the class CassandraClientPoolingContainer method createClientPool.

/**
 * Pool size:
 *    Always keep {@link CassandraKeyValueServiceConfig#poolSize()} connections around, per host. Allow bursting
 *    up to {@link CassandraKeyValueServiceConfig#maxConnectionBurstSize()} connections per host under load.
 *
 * Borrowing from pool:
 *    On borrow, check if the connection is actually open. If it is not,
 *       immediately discard this connection from the pool, and try to take another.
 *    Borrow attempts against a fully in-use pool immediately throw a NoSuchElementException.
 *       {@code CassandraClientPool} when it sees this will:
 *          Follow an exponential backoff as a method of back pressure.
 *          Try 3 times against this host, and then give up and try against different hosts 3 additional times.
 *
 * In an asynchronous thread (using default values):
 *    Every 20-30 seconds, examine approximately a tenth of the connections in pool.
 *    Discard any connections in this tenth of the pool whose TCP connections are closed.
 *    Discard any connections in this tenth of the pool that have been idle for more than 10 minutes,
 *       while still keeping a minimum number of idle connections around for fast borrows.
 *
 * @param poolNumber number of the pool for metric registration.
 */
private GenericObjectPool<CassandraClient> createClientPool(int poolNumber) {
    CassandraClientFactory cassandraClientFactory = new CassandraClientFactory(qosClient, host, config);
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMinIdle(config.poolSize());
    poolConfig.setMaxIdle(config.maxConnectionBurstSize());
    poolConfig.setMaxTotal(config.maxConnectionBurstSize());
    // immediately throw when we try and borrow from a full pool; dealt with at higher level
    poolConfig.setBlockWhenExhausted(false);
    poolConfig.setMaxWaitMillis(config.socketTimeoutMillis());
    // this test is free/just checks a boolean and does not block; borrow is still fast
    poolConfig.setTestOnBorrow(true);
    poolConfig.setMinEvictableIdleTimeMillis(TimeUnit.MILLISECONDS.convert(config.idleConnectionTimeoutSeconds(), TimeUnit.SECONDS));
    // the randomness here is to prevent all of the pools for all of the hosts
    // evicting all at at once, which isn't great for C*.
    int timeBetweenEvictionsSeconds = config.timeBetweenConnectionEvictionRunsSeconds();
    int delta = ThreadLocalRandom.current().nextInt(Math.min(timeBetweenEvictionsSeconds / 2, 10));
    poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.MILLISECONDS.convert(timeBetweenEvictionsSeconds + delta, TimeUnit.SECONDS));
    poolConfig.setNumTestsPerEvictionRun(-(int) (1.0 / config.proportionConnectionsToCheckPerEvictionRun()));
    poolConfig.setTestWhileIdle(true);
    poolConfig.setJmxNamePrefix(CassandraLogHelper.host(host));
    GenericObjectPool<CassandraClient> pool = new GenericObjectPool<>(cassandraClientFactory, poolConfig);
    registerMetrics(pool, poolNumber);
    return pool;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 34 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project ofbiz-framework by apache.

the class DebugManagedDataSource method getConnection.

@Override
public Connection getConnection() throws SQLException {
    if (Debug.verboseOn()) {
        if (super.getPool() instanceof GenericObjectPool) {
            GenericObjectPool objectPool = (GenericObjectPool) super.getPool();
            Debug.logVerbose("Borrowing a connection from the pool; used/idle/total: " + objectPool.getNumActive() + "/" + objectPool.getNumIdle() + "/" + (objectPool.getNumActive() + objectPool.getNumIdle()) + "; min idle/max idle/max total: " + objectPool.getMinIdle() + "/" + objectPool.getMaxIdle() + "/" + objectPool.getMaxTotal(), module);
        } else {
            if (Debug.verboseOn())
                Debug.logVerbose("Borrowing a connection from the pool; used/idle/total: " + super.getPool().getNumActive() + "/" + super.getPool().getNumIdle() + "/" + (super.getPool().getNumActive() + super.getPool().getNumIdle()), module);
        }
    }
    return super.getConnection();
}
Also used : GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 35 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project ofbiz-framework by apache.

the class DebugManagedDataSource method getInfo.

public Map<String, Object> getInfo() {
    Map<String, Object> dataSourceInfo = new HashMap<String, Object>();
    dataSourceInfo.put("poolNumActive", super.getPool().getNumActive());
    dataSourceInfo.put("poolNumIdle", super.getPool().getNumIdle());
    dataSourceInfo.put("poolNumTotal", (super.getPool().getNumIdle() + super.getPool().getNumActive()));
    if (super.getPool() instanceof GenericObjectPool) {
        GenericObjectPool objectPool = (GenericObjectPool) super.getPool();
        dataSourceInfo.put("poolMaxActive", objectPool.getMaxTotal());
        dataSourceInfo.put("poolMaxIdle", objectPool.getMaxIdle());
        dataSourceInfo.put("poolMaxWait", objectPool.getMaxWaitMillis());
        dataSourceInfo.put("poolMinEvictableIdleTimeMillis", objectPool.getMinEvictableIdleTimeMillis());
        dataSourceInfo.put("poolMinIdle", objectPool.getMinIdle());
    }
    return dataSourceInfo;
}
Also used : HashMap(java.util.HashMap) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)79 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)32 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)27 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)23 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)19 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)16 Test (org.junit.jupiter.api.Test)13 Properties (java.util.Properties)11 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)9 SQLException (java.sql.SQLException)8 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)8 Connection (java.sql.Connection)7 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Bean (org.springframework.context.annotation.Bean)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 IOException (java.io.IOException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 Test (org.junit.Test)3 TimeInterval (com.adaptris.util.TimeInterval)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)2