Example usage for org.apache.commons.pool2.impl GenericKeyedObjectPoolConfig setTestOnBorrow

List of usage examples for org.apache.commons.pool2.impl GenericKeyedObjectPoolConfig setTestOnBorrow

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericKeyedObjectPoolConfig setTestOnBorrow.

Prototype

public void setTestOnBorrow(boolean testOnBorrow) 

Source Link

Document

Set the value for the testOnBorrow configuration attribute for pools created with this configuration instance.

Usage

From source file:io.github.hiant.ConnectionPool.java

public static ConnectionPool getInstance() {
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setTestOnBorrow(true);
    return new ConnectionPool(new ConnectionFactory(), config);
}

From source file:com.vmware.identity.idm.server.provider.LdapConnectionPool.java

private GenericKeyedObjectPoolConfig getGenericKeyedObjectPoolConfig(String tenantName) {
    LdapConnectionPoolConfig config = configManager.getConfiguration(tenantName);
    GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();

    poolConfig.setTestOnBorrow(config.getTestOnBorrow());
    poolConfig.setTimeBetweenEvictionRunsMillis(config.getEvictionInterval());
    poolConfig.setMinEvictableIdleTimeMillis(config.getIdleTime());
    poolConfig.setMaxTotal(config.getMaxConnections());
    poolConfig.setMaxTotalPerKey(config.getMaxConnectionsPerKey());
    poolConfig.setMaxWaitMillis(config.getMaxWaitMilis());

    return poolConfig;
}

From source file:net.sheehantech.cherry.pool.KeyedPooledPushClient.java

@Override
public void init() throws ConnectionFailedException {
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    if (maxTotal != null)
        config.setMaxTotalPerKey(maxTotal);
    if (maxIdle != null)
        config.setMaxIdlePerKey(maxIdle);
    if (minIdle != null)
        config.setMinIdlePerKey(minIdle);
    config.setTestOnBorrow(true);
    config.setTestWhileIdle(true);/*from  w ww .jav  a 2  s. c o  m*/
    config.setBlockWhenExhausted(true);
    config.setMaxTotalPerKey(maxTotal);
    pool = new GenericKeyedObjectPool<K, PooledPushSocket>(
            new KeyedPooledPushSocketFactory(socketFactories, gateways, ports), config);
    try {
        for (K k : sslContexts.keySet())
            pool.preparePool(k);
    } catch (Exception e) {
        throw (new ConnectionFailedException(e));
    }
    logger.debug("Started new push socket pool with {} sockets", pool.getNumIdle());

}

From source file:com.quancheng.saluki.core.grpc.GrpcEngine.java

private void initChannelPool() {
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setMaxTotal(1000);/*from ww w  . j  ava 2 s  .  c o  m*/
    config.setMaxTotalPerKey(3);
    config.setBlockWhenExhausted(true);
    config.setMinIdlePerKey(3);
    config.setMaxWaitMillis(10);
    config.setNumTestsPerEvictionRun(Integer.MAX_VALUE);
    config.setTestOnBorrow(false);
    config.setTestOnReturn(false);
    config.setTestWhileIdle(false);
    config.setTimeBetweenEvictionRunsMillis(1 * 60000L);
    config.setMinEvictableIdleTimeMillis(10 * 60000L);
    config.setTestWhileIdle(false);
    this.channelPool = new GenericKeyedObjectPool<GrpcURL, Channel>(new GrpcChannelFactory(), config);
}

From source file:com.google.devtools.build.lib.worker.WorkerModule.java

@Override
public void beforeCommand(Command command, CommandEnvironment env) {
    this.env = env;
    env.getEventBus().register(this);

    if (workers == null) {
        Path logDir = env.getRuntime().getOutputBase().getRelative("worker-logs");
        try {/*  www  .  j a  va  2s. com*/
            logDir.createDirectory();
        } catch (IOException e) {
            env.getReporter().handle(Event.error("Could not create directory for worker logs: " + logDir));
        }

        GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();

        // It's better to re-use a worker as often as possible and keep it hot, in order to profit
        // from JIT optimizations as much as possible.
        config.setLifo(true);

        // Check for & deal with idle workers every 5 seconds.
        config.setTimeBetweenEvictionRunsMillis(5 * 1000);

        // Always test the liveliness of worker processes.
        config.setTestOnBorrow(true);
        config.setTestOnCreate(true);
        config.setTestOnReturn(true);
        config.setTestWhileIdle(true);

        // Don't limit the total number of worker processes, as otherwise the pool might be full of
        // e.g. Java workers and could never accommodate another request for a different kind of
        // worker.
        config.setMaxTotal(-1);

        workers = new WorkerPool(new WorkerFactory(), config);
        workers.setReporter(env.getReporter());
        workers.setLogDirectory(logDir);
    }
}

From source file:org.apache.jmeter.visualizers.backend.graphite.AbstractGraphiteMetricsSender.java

/**
 * Create a new keyed pool of {@link SocketOutputStream}s using a
 * {@link SocketOutputStreamPoolFactory}. The keys for the pool are
 * {@link SocketConnectionInfos} instances.
 *
 * @return GenericKeyedObjectPool the newly generated pool
 *///  w ww .  j  a v a2  s. co  m
protected GenericKeyedObjectPool<SocketConnectionInfos, SocketOutputStream> createSocketOutputStreamPool() {
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setTestOnBorrow(true);
    config.setTestWhileIdle(true);
    config.setMaxTotalPerKey(-1);
    config.setMaxTotal(-1);
    config.setMaxIdlePerKey(-1);
    config.setMinEvictableIdleTimeMillis(TimeUnit.MINUTES.toMillis(3));
    config.setTimeBetweenEvictionRunsMillis(TimeUnit.MINUTES.toMillis(3));

    return new GenericKeyedObjectPool<>(
            new SocketOutputStreamPoolFactory(SOCKET_CONNECT_TIMEOUT_MS, SOCKET_TIMEOUT), config);
}

From source file:org.jmxtrans.embedded.output.GraphitePickleWriter.java

/**
 * Load settings, initialize the {@link SocketWriter} pool and test the connection to the graphite server.
 *
 * a {@link Logger#warn(String)} message is emitted if the connection to the graphite server fails.
 *///from  w w  w. j  a  v a 2 s .c  o m
@Override
public void start() {
    int port = getIntSetting(SETTING_PORT, DEFAULT_GRAPHITE_SERVER_PORT);
    String host = getStringSetting(SETTING_HOST);
    graphiteServerHostAndPort = new HostAndPort(host, port);

    logger.info("Start Graphite Pickle writer connected to '{}'...", graphiteServerHostAndPort);

    metricPathPrefix = getStringSetting(SETTING_NAME_PREFIX, DEFAULT_NAME_PREFIX);
    metricPathPrefix = getStrategy().resolveExpression(metricPathPrefix);
    if (!metricPathPrefix.isEmpty() && !metricPathPrefix.endsWith(".")) {
        metricPathPrefix = metricPathPrefix + ".";
    }

    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setTestOnBorrow(getBooleanSetting("pool.testOnBorrow", true));
    config.setTestWhileIdle(getBooleanSetting("pool.testWhileIdle", true));
    config.setMaxTotal(getIntSetting("pool.maxActive", -1));
    config.setMaxIdlePerKey(getIntSetting("pool.maxIdle", -1));
    config.setMinEvictableIdleTimeMillis(
            getLongSetting("pool.minEvictableIdleTimeMillis", TimeUnit.MINUTES.toMillis(5)));
    config.setTimeBetweenEvictionRunsMillis(
            getLongSetting("pool.timeBetweenEvictionRunsMillis", TimeUnit.MINUTES.toMillis(5)));
    config.setJmxNameBase(
            "org.jmxtrans.embedded:type=GenericKeyedObjectPool,writer=GraphitePickleWriter,name=");
    config.setJmxNamePrefix(graphiteServerHostAndPort.getHost() + "_" + graphiteServerHostAndPort.getPort());

    int socketConnectTimeoutInMillis = getIntSetting("graphite.socketConnectTimeoutInMillis",
            SocketOutputStreamPoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT_IN_MILLIS);

    socketOutputStreamPool = new GenericKeyedObjectPool<HostAndPort, SocketOutputStream>(
            new SocketOutputStreamPoolFactory(socketConnectTimeoutInMillis), config);

    if (isEnabled()) {
        try {
            SocketOutputStream socketOutputStream = socketOutputStreamPool
                    .borrowObject(graphiteServerHostAndPort);
            socketOutputStreamPool.returnObject(graphiteServerHostAndPort, socketOutputStream);
        } catch (Exception e) {
            logger.warn("Test Connection: FAILURE to connect to Graphite server '{}'",
                    graphiteServerHostAndPort, e);
        }
    }
    try {
        Class.forName("org.python.modules.cPickle");
    } catch (ClassNotFoundException e) {
        throw new EmbeddedJmxTransException("jython librarie is required by the " + getClass().getSimpleName()
                + " but is not found in the classpath. Please add org.python:jython:2.5.3+ to the classpath.");
    }
}

From source file:org.jmxtrans.embedded.output.GraphiteWriter.java

/**
 * Load settings, initialize the {@link SocketWriter} pool and test the connection to the graphite server.
 *
 * a {@link Logger#warn(String)} message is emitted if the connection to the graphite server fails.
 *//*from w w w . ja  v a 2 s.  c  o  m*/
@Override
public void start() {
    int port = getIntSetting(SETTING_PORT, DEFAULT_GRAPHITE_SERVER_PORT);
    String host = getStringSetting(SETTING_HOST);
    graphiteServerHostAndPort = new HostAndPort(host, port);

    logger.info("Start Graphite writer connected to '{}'...", graphiteServerHostAndPort);

    metricPathPrefix = getStringSetting(SETTING_NAME_PREFIX, DEFAULT_NAME_PREFIX);
    metricPathPrefix = getStrategy().resolveExpression(metricPathPrefix);
    if (!metricPathPrefix.isEmpty() && !metricPathPrefix.endsWith(".")) {
        metricPathPrefix = metricPathPrefix + ".";
    }

    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setTestOnBorrow(getBooleanSetting("pool.testOnBorrow", true));
    config.setTestWhileIdle(getBooleanSetting("pool.testWhileIdle", true));
    config.setMaxTotal(getIntSetting("pool.maxActive", -1));
    config.setMaxIdlePerKey(getIntSetting("pool.maxIdle", -1));
    config.setMinEvictableIdleTimeMillis(
            getLongSetting("pool.minEvictableIdleTimeMillis", TimeUnit.MINUTES.toMillis(5)));
    config.setTimeBetweenEvictionRunsMillis(
            getLongSetting("pool.timeBetweenEvictionRunsMillis", TimeUnit.MINUTES.toMillis(5)));
    config.setJmxNameBase("org.jmxtrans.embedded:type=GenericKeyedObjectPool,writer=GraphiteWriter,name=");
    config.setJmxNamePrefix(graphiteServerHostAndPort.getHost() + "_" + graphiteServerHostAndPort.getPort());

    int socketConnectTimeoutInMillis = getIntSetting("graphite.socketConnectTimeoutInMillis",
            SocketWriterPoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT_IN_MILLIS);

    String protocol = getStringSetting(SETTING_PROTOCOL, null);
    if (protocol != null && protocol.equalsIgnoreCase(PROTOCOL_UDP)) {
        socketWriterPool = new GenericKeyedObjectPool<HostAndPort, SocketWriter>(
                new UDPSocketWriterPoolFactory(StandardCharsets.UTF_8), config);
    } else {
        if (protocol == null) {
            // protocol not specified, use default one
            logger.info("Protocol unspecified, default protocol '{}' will be used.", PROTOCOL_TCP);
        } else if (PROTOCOL_TCP.equalsIgnoreCase(protocol) == false) {
            // unknown or protocol, use default one
            logger.warn("Unknown protocol specified '{}', default protocol '{}' will be used instead.",
                    protocol, PROTOCOL_TCP);
        }
        socketWriterPool = new GenericKeyedObjectPool<HostAndPort, SocketWriter>(
                new SocketWriterPoolFactory(StandardCharsets.UTF_8, socketConnectTimeoutInMillis), config);
    }

    if (isEnabled()) {
        try {
            SocketWriter socketWriter = socketWriterPool.borrowObject(graphiteServerHostAndPort);
            socketWriterPool.returnObject(graphiteServerHostAndPort, socketWriter);
        } catch (Exception e) {
            logger.warn("Test Connection: FAILURE to connect to Graphite server '{}'",
                    graphiteServerHostAndPort, e);
        }
    }
}

From source file:org.springframework.ldap.pool2.factory.PooledContextSource.java

private GenericKeyedObjectPoolConfig getConfig(PoolConfig poolConfig) {
    GenericKeyedObjectPoolConfig objectPoolConfig = new GenericKeyedObjectPoolConfig();

    objectPoolConfig.setMaxTotalPerKey(poolConfig.getMaxTotalPerKey());
    objectPoolConfig.setMaxTotal(poolConfig.getMaxTotal());

    objectPoolConfig.setMaxIdlePerKey(poolConfig.getMaxIdlePerKey());
    objectPoolConfig.setMinIdlePerKey(poolConfig.getMinIdlePerKey());

    objectPoolConfig.setTestWhileIdle(poolConfig.isTestWhileIdle());
    objectPoolConfig.setTestOnReturn(poolConfig.isTestOnReturn());
    objectPoolConfig.setTestOnCreate(poolConfig.isTestOnCreate());
    objectPoolConfig.setTestOnBorrow(poolConfig.isTestOnBorrow());

    objectPoolConfig.setTimeBetweenEvictionRunsMillis(poolConfig.getTimeBetweenEvictionRunsMillis());
    objectPoolConfig.setEvictionPolicyClassName(poolConfig.getEvictionPolicyClassName());
    objectPoolConfig.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis());
    objectPoolConfig.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun());
    objectPoolConfig.setSoftMinEvictableIdleTimeMillis(poolConfig.getSoftMinEvictableIdleTimeMillis());

    objectPoolConfig.setJmxEnabled(poolConfig.isJmxEnabled());
    objectPoolConfig.setJmxNameBase(poolConfig.getJmxNameBase());
    objectPoolConfig.setJmxNamePrefix(poolConfig.getJmxNamePrefix());

    objectPoolConfig.setMaxWaitMillis(poolConfig.getMaxWaitMillis());

    objectPoolConfig.setFairness(poolConfig.isFairness());
    objectPoolConfig.setBlockWhenExhausted(poolConfig.isBlockWhenExhausted());
    objectPoolConfig.setLifo(poolConfig.isLifo());

    return objectPoolConfig;
}