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

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

Introduction

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

Prototype

public void setMaxTotal(int maxTotal) 

Source Link

Document

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

Usage

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.
 *//* w w  w .j  a  va  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;
}