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

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

Introduction

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

Prototype

public GenericKeyedObjectPoolConfig() 

Source Link

Document

Create a new configuration with default settings.

Usage

From source file:com.streamsets.datacollector.stagelibrary.ClassLoaderStageLibraryTask.java

@Override
public void initTask() {
    super.initTask();
    stageClassLoaders = runtimeInfo.getStageLibraryClassLoaders();
    if (!stageClassLoaders.isEmpty()) {
        resolveClassLoaderMethods(stageClassLoaders.get(0));
    }// w  w w .j a  v  a 2s  .  c  o m
    json = ObjectMapperFactory.get();
    stageList = new ArrayList<>();
    stageMap = new HashMap<>();
    loadStages();
    stageList = ImmutableList.copyOf(stageList);
    stageMap = ImmutableMap.copyOf(stageMap);

    // localization cache for definitions
    localizedStageList = CacheBuilder.newBuilder().build(new CacheLoader<Locale, List<StageDefinition>>() {
        @Override
        public List<StageDefinition> load(Locale key) throws Exception {
            List<StageDefinition> list = new ArrayList<>();
            for (StageDefinition stage : stageList) {
                list.add(stage.localize());
            }
            return list;
        }
    });
    validateStageVersions(stageList);

    // initializing the list of targets that can be used for error handling
    ErrorHandlingChooserValues.setErrorHandlingOptions(this);

    // initializing the list of targets that can be used as aggregating sink
    StatsTargetChooserValues.setStatsTargetOptions(this);

    // initializing the pool of private stage classloaders
    GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    poolConfig.setJmxEnabled(false);
    poolConfig.setMaxTotal(
            configuration.get(MAX_PRIVATE_STAGE_CLASS_LOADERS_KEY, MAX_PRIVATE_STAGE_CLASS_LOADERS_DEFAULT));
    poolConfig.setMinEvictableIdleTimeMillis(-1);
    poolConfig.setNumTestsPerEvictionRun(0);
    poolConfig.setMaxIdlePerKey(-1);
    poolConfig.setMinIdlePerKey(0);
    poolConfig.setMaxTotalPerKey(-1);
    poolConfig.setBlockWhenExhausted(false);
    poolConfig.setMaxWaitMillis(0);
    privateClassLoaderPool = new GenericKeyedObjectPool<>(new ClassLoaderFactory(stageClassLoaders),
            poolConfig);
}

From source file:JDBCPool.dbcp.demo.sourcecode.PoolableConnectionFactory.java

/**
 * ?PoolDefaultPooledObject??Connection//from w w w .ja va  2  s . co m
 */
@Override
public PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = _connFactory.createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }

    try {
        initializeConnection(conn); //?SQL?SQL
    } catch (SQLException sqle) {
        // Make sure the connection is closed
        try {
            conn.close();
        } catch (SQLException ignore) {
            // ignore
        }
        // Rethrow original exception so it is visible to caller
        throw sqle;
    }

    long connIndex = connectionIndex.getAndIncrement();

    if (poolStatements) {
        conn = new PoolingConnection(conn);
        GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(maxOpenPreparedStatements);
        if (dataSourceJmxName != null) {
            StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
            base.append(Constants.JMX_CONNECTION_BASE_EXT);
            base.append(Long.toString(connIndex));
            config.setJmxNameBase(base.toString());
            config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
        } else {
            config.setJmxEnabled(false);
        }
        KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>(
                (PoolingConnection) conn, config);
        ((PoolingConnection) conn).setStatementPool(stmtPool);
        ((PoolingConnection) conn).setCacheState(_cacheState);
    }

    // Register this connection with JMX
    ObjectName connJmxName;
    if (dataSourceJmxName == null) {
        connJmxName = null;
    } else {
        connJmxName = new ObjectName(
                dataSourceJmxName.toString() + Constants.JMX_CONNECTION_BASE_EXT + connIndex);
    }

    PoolableConnection pc = new PoolableConnection(conn, _pool, connJmxName, _disconnectionSqlCodes,
            _fastFailValidation);

    return new DefaultPooledObject<>(pc);
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.MMXPlugin.java

public void initializeAPNSConnectionPool() {

    MMXConfiguration configuration = MMXConfiguration.getConfiguration();
    int maxObjectsPerKey = configuration.getInt(MMXConfigKeys.APNS_POOL_MAX_CONNECTIONS_PER_APP,
            MMXServerConstants.APNS_POOL_MAX_CONNECTIONS_PER_APP);
    int maxIdleObjectsPerKey = configuration.getInt(MMXConfigKeys.APNS_POOL_MAX_IDLE_CONNECTIONS_PER_APP,
            MMXServerConstants.APNS_POOL_MAX_IDLE_CONNECTIONS_PER_APP);
    int ttlForIdleObjectsInMinutes = configuration.getInt(MMXConfigKeys.APNS_POOL_IDLE_TTL_MINUTES,
            MMXServerConstants.APNS_POOL_IDLE_TTL_MINUTES);
    int maxTotal = configuration.getInt(MMXConfigKeys.APNS_POOL_MAX_TOTAL_CONNECTIONS,
            MMXServerConstants.APNS_POOL_MAX_TOTAL_CONNECTIONS);

    Log.info(/*from w  w  w. java  2 s  .  co  m*/
            "Configuring APNS Connection pool with following values: maxObjects:{}, maxObjectsPerKey:{}, "
                    + "maxIdleObjectsPerKey:{}, ttlForIdleObjectsInMinutes:{}",
            maxTotal, maxObjectsPerKey, maxIdleObjectsPerKey, ttlForIdleObjectsInMinutes);

    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setMaxTotalPerKey(maxObjectsPerKey);
    config.setMaxTotal(maxTotal);
    config.setMaxIdlePerKey(maxIdleObjectsPerKey);
    config.setMinEvictableIdleTimeMillis(ttlForIdleObjectsInMinutes * 60 * 1000L);

    APNSConnectionPoolImpl.initialize(config);
}

From source file:org.apache.activemq.jms.pool.ConnectionPool.java

public ConnectionPool(Connection connection) {
    final GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    poolConfig.setJmxEnabled(false);/*w w  w .  j  a  v a2 s.c o m*/
    this.connection = wrap(connection);

    // Create our internal Pool of session instances.
    this.sessionPool = new GenericKeyedObjectPool<SessionKey, SessionHolder>(
            new KeyedPooledObjectFactory<SessionKey, SessionHolder>() {
                @Override
                public PooledObject<SessionHolder> makeObject(SessionKey sessionKey) throws Exception {

                    return new DefaultPooledObject<SessionHolder>(new SessionHolder(makeSession(sessionKey)));
                }

                @Override
                public void destroyObject(SessionKey sessionKey, PooledObject<SessionHolder> pooledObject)
                        throws Exception {
                    ((SessionHolder) pooledObject.getObject()).close();
                }

                @Override
                public boolean validateObject(SessionKey sessionKey, PooledObject<SessionHolder> pooledObject) {
                    return true;
                }

                @Override
                public void activateObject(SessionKey sessionKey, PooledObject<SessionHolder> pooledObject)
                        throws Exception {
                }

                @Override
                public void passivateObject(SessionKey sessionKey, PooledObject<SessionHolder> pooledObject)
                        throws Exception {
                }
            }, poolConfig);
}

From source file:org.apache.activemq.jms.pool.PooledConnectionFactory.java

public void initConnectionsPool() {
    if (this.connectionsPool == null) {
        final GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
        poolConfig.setJmxEnabled(false);
        this.connectionsPool = new GenericKeyedObjectPool<ConnectionKey, ConnectionPool>(
                new KeyedPooledObjectFactory<ConnectionKey, ConnectionPool>() {
                    @Override// w ww  . j av a  2 s.  co m
                    public PooledObject<ConnectionPool> makeObject(ConnectionKey connectionKey)
                            throws Exception {
                        Connection delegate = createConnection(connectionKey);

                        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());
                        connection.setReconnectOnException(isReconnectOnException());

                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Created new connection: {}", connection);
                        }

                        PooledConnectionFactory.this.mostRecentlyCreated.set(connection);

                        return new DefaultPooledObject<ConnectionPool>(connection);
                    }

                    @Override
                    public void destroyObject(ConnectionKey connectionKey,
                            PooledObject<ConnectionPool> pooledObject) throws Exception {
                        ConnectionPool connection = pooledObject.getObject();
                        try {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Destroying connection: {}", connection);
                            }
                            connection.close();
                        } catch (Exception e) {
                            LOG.warn("Close connection failed for connection: " + connection
                                    + ". This exception will be ignored.", e);
                        }
                    }

                    @Override
                    public boolean validateObject(ConnectionKey connectionKey,
                            PooledObject<ConnectionPool> pooledObject) {
                        ConnectionPool connection = pooledObject.getObject();
                        if (connection != null && connection.expiredCheck()) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Connection has expired: {} and will be destroyed", connection);
                            }

                            return false;
                        }

                        return true;
                    }

                    @Override
                    public void activateObject(ConnectionKey connectionKey,
                            PooledObject<ConnectionPool> pooledObject) throws Exception {
                    }

                    @Override
                    public void passivateObject(ConnectionKey connectionKey,
                            PooledObject<ConnectionPool> pooledObject) throws Exception {
                    }

                }, poolConfig);

        // Set max idle (not max active) since our connections always idle in the pool.
        this.connectionsPool.setMaxIdlePerKey(1);
        this.connectionsPool.setLifo(false);

        // We always want our validate method to control when idle objects are evicted.
        this.connectionsPool.setTestOnBorrow(true);
        this.connectionsPool.setTestWhileIdle(true);
    }
}

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
 *//* www  . j  av  a  2  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.apache.sentry.core.common.transport.SentryTransportPool.java

/**
 * Configure transport pool.// w w w  . j a v a 2s. c  o  m
 * <p>
 * The pool accepts the following configuration:
 * <ul>
 *   <li>Maximum total number of objects in the pool</li>
 *   <li>Minimum number of idle objects</li>
 *   <li>Maximum number of idle objects</li>
 *   <li>Minimum time before the object is evicted</li>
 *   <li>Interval between evictions</li>
 * </ul>
 * @param conf Configuration
 * @param transportConfig Configuration interface
 * @param transportFactory Transport factory used to produce transports
 */
public SentryTransportPool(Configuration conf, SentryClientTransportConfigInterface transportConfig,
        TransportFactory transportFactory) {

    // This isn't thread-safe, but we don't care - it is only used
    // for debugging when running tests - normal apps use a single pool
    poolId++;
    id = poolId;

    this.transportFactory = transportFactory;
    doLoadBalancing = transportConfig.isLoadBalancingEnabled(conf);
    isPoolEnabled = transportConfig.isTransportPoolEnabled(conf);

    // Get list of server addresses
    String hostsAndPortsStr = transportConfig.getSentryServerRpcAddress(conf);
    int serverPort = transportConfig.getServerRpcPort(conf);
    LOGGER.info("Creating pool for {} with default port {}", hostsAndPortsStr, serverPort);
    String[] hostsAndPortsStrArr = hostsAndPortsStr.split(",");
    Preconditions.checkArgument(hostsAndPortsStrArr.length > 0, "At least one server should be specified");

    endpoints = new ArrayList<>(hostsAndPortsStrArr.length);
    for (String addr : hostsAndPortsStrArr) {
        HostAndPort endpoint = ThriftUtil.parseAddress(addr, serverPort);
        LOGGER.info("Adding endpoint {}", endpoint);
        endpoints.add(endpoint);
    }

    if (!isPoolEnabled) {
        pool = null;
        LOGGER.info("Connection pooling is disabled");
        return;
    }

    LOGGER.info("Connection pooling is enabled");
    // Set pool configuration based on Configuration settings
    GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();

    // Don't limit maximum number of objects in the pool
    poolConfig.setMaxTotal(-1);

    poolConfig.setMinIdlePerKey(transportConfig.getPoolMinIdle(conf));
    poolConfig.setMaxIdlePerKey(transportConfig.getPoolMaxIdle(conf));

    // Do not block when pool is exhausted, throw exception instead
    poolConfig.setBlockWhenExhausted(false);
    poolConfig.setTestOnReturn(true);

    // No limit for total objects in the pool
    poolConfig.setMaxTotalPerKey(transportConfig.getPoolMaxTotal(conf));
    poolConfig.setMinEvictableIdleTimeMillis(transportConfig.getMinEvictableTimeSec(conf));
    poolConfig.setTimeBetweenEvictionRunsMillis(transportConfig.getTimeBetweenEvictionRunsSec(conf));

    // Create object pool
    pool = new GenericKeyedObjectPool<>(new PoolFactory(this.transportFactory, id), poolConfig);
}

From source file:org.dspace.event.EventServiceImpl.java

private void initPool() {

    if (dispatcherPool == null) {

        // TODO EVENT Some of these pool configuration
        // parameters can live in dspace.cfg or a
        // separate configuration file

        // TODO EVENT Eviction parameters should be set

        poolConfig = new GenericKeyedObjectPoolConfig();
        poolConfig.setMaxTotalPerKey(100);
        poolConfig.setMaxIdlePerKey(5);/*from  ww w. j a  v  a2  s  .  c om*/
        poolConfig.setMaxTotal(100);

        try {
            dispatcherFactory = new DispatcherPoolFactory();

            dispatcherPool = PoolUtils
                    .synchronizedPool(new GenericKeyedObjectPool(dispatcherFactory, poolConfig));

            enumerateConsumers();

        } catch (Exception e) {
            log.error("Could not initialize EventService dispatcher pool", e);
        }

    }
}

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.
 *//*  ww 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.
 *//* 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);
        }
    }
}