Example usage for org.apache.commons.pool2.impl GenericObjectPoolConfig setMinIdle

List of usage examples for org.apache.commons.pool2.impl GenericObjectPoolConfig setMinIdle

Introduction

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

Prototype

public void setMinIdle(int minIdle) 

Source Link

Document

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

Usage

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

@Override
public void init() throws ConnectionFailedException {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    if (maxTotal != null)
        config.setMaxTotal(maxTotal);//from   w w w .jav a  2  s .  co  m
    if (maxIdle != null)
        config.setMaxIdle(maxIdle);
    if (minIdle != null)
        config.setMinIdle(minIdle);
    config.setTestOnBorrow(true);
    config.setTestWhileIdle(true);
    config.setBlockWhenExhausted(true);
    pool = new GenericObjectPool<PooledPushSocket>(new PooledPushSocketFactory(socketFactory, gateway, port),
            config);
    try {
        pool.preparePool();
    } catch (Exception e) {
        throw (new ConnectionFailedException(e));
    }
    logger.debug("Started new push socket pool with {} sockets", pool.getNumIdle());
}

From source file:com.streamsets.pipeline.stage.origin.udptokafka.KafkaUDPConsumer.java

public void init() {
    executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(udpConfigBean.concurrency);
    int max = udpConfigBean.concurrency;
    int minIdle = Math.max(1, udpConfigBean.concurrency / 4);
    int maxIdle = udpConfigBean.concurrency / 2;
    GenericObjectPoolConfig kakfaPoolConfig = new GenericObjectPoolConfig();
    kakfaPoolConfig.setMaxTotal(udpConfigBean.concurrency);
    kakfaPoolConfig.setMinIdle(minIdle);
    kakfaPoolConfig.setMaxIdle(maxIdle);
    LOG.debug("Creating Kafka producer pool with max '{}' minIdle '{}' maxIdle '{}'", max, minIdle, maxIdle);
    kafkaProducerPool = new GenericObjectPool<>(
            new SdcKafkaProducerPooledObjectFactory(kafkaTargetConfig, DataFormat.BINARY), kakfaPoolConfig);
    GenericObjectPoolConfig serializerPoolConfig = new GenericObjectPoolConfig();
    serializerPoolConfig.setMaxTotal(udpConfigBean.concurrency);
    serializerPoolConfig.setMinIdle(udpConfigBean.concurrency);
    serializerPoolConfig.setMaxIdle(udpConfigBean.concurrency);
    udpSerializerPool = new GenericObjectPool<>(new UDPMessageSerializerPooledObjectFactory(),
            serializerPoolConfig);// ww  w  .j a v a  2  s.c  o m
    udpType = UDP_DATA_FORMAT_MAP.get(udpConfigBean.dataFormat);
    LOG.debug("Started, concurrency '{}'", udpConfigBean.concurrency);
}

From source file:com.heliosapm.streams.collector.ds.AbstractDataSourceFactory.java

/**
 * Reads the generic object pool configuration from the passed JSONObject
 * @param jsonConfig The json configuration for the current data source
 * @return the pool config/*from www  .  j a  v a  2 s  .  c o m*/
 */
protected GenericObjectPoolConfig getPoolConfig(final JsonNode jsonConfig) {
    if (jsonConfig == null)
        throw new IllegalArgumentException("The passed JSONObject was null");
    final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    if (jsonConfig.has("maxPoolSize"))
        config.setMaxTotal(jsonConfig.get("maxPoolSize").intValue());
    if (jsonConfig.has("maxPoolIdle"))
        config.setMaxIdle(jsonConfig.get("maxPoolIdle").intValue());
    if (jsonConfig.has("minPoolIdle"))
        config.setMinIdle(jsonConfig.get("minPoolIdle").intValue());
    if (jsonConfig.has("registerMbeans"))
        config.setJmxEnabled(jsonConfig.get("registerMbeans").booleanValue());
    if (jsonConfig.has("fair"))
        config.setFairness(jsonConfig.get("fair").booleanValue());
    if (jsonConfig.has("lifo"))
        config.setLifo(jsonConfig.get("lifo").booleanValue());
    if (jsonConfig.has("maxWait"))
        config.setMaxWaitMillis(jsonConfig.get("maxWait").longValue());

    final boolean testWhileIdle;
    if (jsonConfig.has("testWhileIdle")) {
        testWhileIdle = jsonConfig.get("testWhileIdle").booleanValue();
    } else {
        testWhileIdle = false;
    }
    if (testWhileIdle) {
        config.setTestWhileIdle(true);
        long testPeriod = 15000;
        if (jsonConfig.has("testPeriod")) {
            testPeriod = jsonConfig.get("testPeriod").longValue();
        }
        config.setTimeBetweenEvictionRunsMillis(testPeriod);
    } else {
        config.setTestWhileIdle(false);
    }
    // ALWAYS test on borrow
    config.setTestOnBorrow(true);
    config.setTestOnCreate(true);
    config.setTestOnReturn(false);
    return config;
}

From source file:de.qucosa.servlet.MetsDisseminatorServlet.java

@Override
public void init() throws ServletException {
    super.init();

    startupProperties = new PropertyCollector().source(getServletContext()).source(System.getProperties())
            .collect();/*from w  w  w. j  a  v a2s  .co m*/

    final FedoraClientFactory fedoraClientFactory = attemptToCreateFedoraClientFactoryFrom(startupProperties);

    if (fedoraClientFactory == null) {
        // we need a client factory for startup connections
        log.warn("Fedora connection credentials not configured. No connection pooling possible.");
    } else {
        final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(
                Integer.parseInt(startupProperties.getProperty(PROP_FEDORA_CONNECTIONPOOL_MAXSIZE, "20")));
        poolConfig.setMinIdle(5);
        poolConfig.setMinEvictableIdleTimeMillis(TimeUnit.MINUTES.toMillis(1));

        fedoraClientPool = new GenericObjectPool<>(fedoraClientFactory, poolConfig);

        log.info("Initialized Fedora connection pool.");
    }

    cacheManager = CacheManager.newInstance();
    cache = cacheManager.getCache("dscache");
}

From source file:herddb.jdbc.BasicHerdDBDataSource.java

protected synchronized void ensureClient() throws SQLException {
    if (client == null) {
        ClientConfiguration clientConfiguration = new ClientConfiguration(properties);
        Properties propsNoPassword = new Properties(properties);
        if (propsNoPassword.contains("password")) {
            propsNoPassword.setProperty("password", "-------");
        }//w w w  . ja  v a  2  s  . c  o  m
        LOGGER.log(Level.INFO, "Booting HerdDB Client, url:" + url + ", properties:" + propsNoPassword
                + " clientConfig " + clientConfiguration);
        clientConfiguration.readJdbcUrl(url);
        if (properties.containsKey("discoverTableSpaceFromQuery")) {
            this.discoverTableSpaceFromQuery = clientConfiguration.getBoolean("discoverTableSpaceFromQuery",
                    true);
        }
        client = new HDBClient(clientConfiguration);
    }
    if (pool == null) {
        if (properties.containsKey("maxActive")) {
            this.maxActive = Integer.parseInt(properties.get("maxActive").toString());
        }
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setBlockWhenExhausted(true);
        config.setMaxTotal(maxActive);
        config.setMaxIdle(maxActive);
        config.setMinIdle(maxActive / 2);
        config.setJmxNamePrefix("HerdDBClient");
        pool = new GenericObjectPool<>(new ConnectionsFactory(), config);
    }
}

From source file:edu.harvard.hul.ois.fits.service.servlets.FitsServlet.java

public void init() throws ServletException {

    // "fits.home" property set differently in Tomcat 7 and JBoss 7.
    // Tomcat: set in catalina.properties
    // JBoss: set as a command line value "-Dfits.home=<path/to/fits/home>
    fitsHome = System.getProperty(FITS_HOME_SYSTEM_PROP_NAME);
    logger.info(FITS_HOME_SYSTEM_PROP_NAME + ": " + fitsHome);

    if (StringUtils.isEmpty(fitsHome)) {
        logger.fatal(FITS_HOME_SYSTEM_PROP_NAME
                + " system property HAS NOT BEEN SET!!! This web application will not properly run.");
        throw new ServletException(FITS_HOME_SYSTEM_PROP_NAME
                + " system property HAS NOT BEEN SET!!! This web application will not properly run.");
    }//from w  ww.  ja  va  2s.  co  m

    // Set the projects properties.
    // First look for a system property pointing to a project properties file. (e.g. - file:/path/to/file)
    // If this value either does not exist or is not valid, the default
    // file that comes with this application will be used for initialization.
    String environmentProjectPropsFile = System.getProperty(ENV_PROJECT_PROPS);
    logger.info(
            "Value of environment property: [ + ENV_PROJECT_PROPS + ] for finding external properties file in location: ["
                    + environmentProjectPropsFile + "]");
    if (environmentProjectPropsFile != null) {
        logger.info("Will look for properties file from environment in location: ["
                + environmentProjectPropsFile + "]");
        try {
            File projectProperties = new File(environmentProjectPropsFile);
            if (projectProperties.exists() && projectProperties.isFile() && projectProperties.canRead()) {
                InputStream is = new FileInputStream(projectProperties);
                applicationProps = new Properties();
                applicationProps.load(is);
            }
        } catch (IOException e) {
            // fall back to default file
            logger.error("Unable to load properties file: [" + environmentProjectPropsFile + "] -- reason: "
                    + e.getMessage(), e);
            logger.error("Falling back to default project.properties file: [" + PROPERTIES_FILE_NAME + "]");
            applicationProps = null;
        }
    }

    if (applicationProps == null) { // did not load from environment variable location
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream resourceStream = classLoader.getResourceAsStream(PROPERTIES_FILE_NAME);
            if (resourceStream != null) {
                applicationProps = new Properties();
                applicationProps.load(resourceStream);
                logger.info("loaded default applicationProps");
            } else {
                logger.warn("project.properties not found!!!");
            }
        } catch (IOException e) {
            logger.error("Could not load properties file: [" + PROPERTIES_FILE_NAME + "]", e);
            // couldn't load default properties so bail...
            throw new ServletException("Couldn't load an applications properties file.", e);
        }
    }
    int maxPoolSize = Integer
            .valueOf(applicationProps.getProperty("max.objects.in.pool", DEFAULT_MAX_OBJECTS_IN_POOL));
    maxFileUploadSizeMb = Long
            .valueOf(applicationProps.getProperty("max.upload.file.size.MB", DEFAULT_MAX_UPLOAD_SIZE));
    maxRequestSizeMb = Long
            .valueOf(applicationProps.getProperty("max.request.size.MB", DEFAULT_MAX_REQUEST_SIZE));
    maxInMemoryFileSizeMb = Integer
            .valueOf(applicationProps.getProperty("max.in.memory.file.size.MB", DEFAULT_IN_MEMORY_FILE_SIZE));
    logger.info("Max objects in object pool: " + maxPoolSize + " -- Max file upload size: "
            + maxFileUploadSizeMb + "MB -- Max request object size: " + maxRequestSizeMb
            + "MB -- Max in-memory file size: " + maxInMemoryFileSizeMb + "MB");

    logger.debug("Initializing FITS pool");
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMinIdle(MIN_IDLE_OBJECTS_IN_POOL);
    poolConfig.setMaxTotal(maxPoolSize);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setBlockWhenExhausted(true);
    fitsWrapperPool = new FitsWrapperPool(new FitsWrapperFactory(), poolConfig);
    logger.debug("FITS pool finished Initializing");

    String uploadBaseDirName = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
    uploadBaseDir = new File(uploadBaseDirName);
    if (!uploadBaseDir.exists()) {
        uploadBaseDir.mkdir();
        logger.info("Created upload base directory: " + uploadBaseDir.getAbsolutePath());
    }
}

From source file:edu.harvard.hul.ois.drs.pdfaconvert.service.servlets.PdfaConverterServlet.java

@Override
public void init() throws ServletException {

    // Set the projects properties.
    // First look for a system property pointing to a project properties file.
    // This value can be either a file path, file protocol (e.g. - file:/path/to/file),
    // or a URL (http://some/server/file).
    // If this value either does not exist or is not valid, the default
    // file that comes with this application will be used for initialization.
    String environmentProjectPropsFile = System.getProperty(ENV_PROJECT_PROPS);
    logger.info("Value of environment property: [{}] for finding external properties file in location: {}",
            ENV_PROJECT_PROPS, environmentProjectPropsFile);
    if (environmentProjectPropsFile != null) {
        logger.info("Will look for properties file from environment in location: {}",
                environmentProjectPropsFile);
        try {/*from   ww  w. ja v  a  2s.  c o  m*/
            File projectProperties = new File(environmentProjectPropsFile);
            if (projectProperties.exists() && projectProperties.isFile() && projectProperties.canRead()) {
                InputStream is = new FileInputStream(projectProperties);
                applicationProps = new Properties();
                applicationProps.load(is);
            }
        } catch (IOException e) {
            // fall back to default file
            logger.error("Unable to load properties file: {} -- reason: {}", environmentProjectPropsFile,
                    e.getMessage());
            logger.error("Falling back to default project.properties file: {}", PROPERTIES_FILE_NAME);
            applicationProps = null;
        }
    }

    if (applicationProps == null) { // did not load from environment variable location
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream resourceStream = classLoader.getResourceAsStream(PROPERTIES_FILE_NAME);
            if (resourceStream != null) {
                applicationProps = new Properties();
                applicationProps.load(resourceStream);
                logger.info("loaded default applicationProps");
            } else {
                logger.warn("project.properties not found!!!");
            }
        } catch (IOException e) {
            logger.error("Could not load properties file: {}", PROPERTIES_FILE_NAME, e);
            // couldn't load default properties so bail...
            throw new ServletException("Couldn't load an applications properties file.", e);
        }
    }
    int maxPoolSize = Integer
            .valueOf(applicationProps.getProperty("max.objects.in.pool", DEFAULT_MAX_OBJECTS_IN_POOL));
    long maxFileUploadSizeMb = Long
            .valueOf(applicationProps.getProperty("max.upload.file.size.MB", DEFAULT_MAX_UPLOAD_SIZE));
    long maxRequestSizeMb = Long
            .valueOf(applicationProps.getProperty("max.request.size.MB", DEFAULT_MAX_REQUEST_SIZE));
    maxInMemoryFileSizeMb = Integer
            .valueOf(applicationProps.getProperty("max.in.memory.file.size.MB", DEFAULT_IN_MEMORY_FILE_SIZE));
    logger.info(
            "Max objects in object pool: {} -- Max file upload size: {}MB -- Max request object size: {}MB -- Max in-memory file size: {}MB",
            maxPoolSize, maxFileUploadSizeMb, maxRequestSizeMb, maxInMemoryFileSizeMb);

    logger.debug("Initializing PdfaConverter pool");
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMinIdle(MIN_IDLE_OBJECTS_IN_POOL);
    poolConfig.setMaxTotal(maxPoolSize);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setBlockWhenExhausted(true);
    pdfaConverterWrapperPool = new PdfaConverterWrapperPool(new PdfaConverterWrapperFactory(), poolConfig);

    // configures upload settings
    factory = new DiskFileItemFactory();
    factory.setSizeThreshold((maxInMemoryFileSizeMb * (int) MB_MULTIPLIER));
    File tempUploadDir = new File(System.getProperty(UPLOAD_DIRECTORY));
    if (!tempUploadDir.exists()) {
        tempUploadDir.mkdir();
    }
    factory.setRepository(tempUploadDir);

    upload = new ServletFileUpload(factory);
    upload.setFileSizeMax(maxFileUploadSizeMb * MB_MULTIPLIER); // convert from MB to bytes
    upload.setSizeMax(maxRequestSizeMb * MB_MULTIPLIER); // convert from MB to bytes

    logger.debug("PdfaConverter pool finished Initializing");
}

From source file:ch.cyberduck.core.worker.ConcurrentTransferWorker.java

public ConcurrentTransferWorker(final ConnectionService connect, final Transfer transfer,
        final TransferOptions options, final TransferSpeedometer meter, final TransferPrompt prompt,
        final TransferErrorCallback error, final TransferItemCallback transferItemCallback,
        final ConnectionCallback connectionCallback, final ProgressListener progressListener,
        final StreamListener streamListener, final X509TrustManager trust, final X509KeyManager key,
        final PathCache cache, final Integer connections) {
    super(transfer, options, prompt, meter, error, transferItemCallback, progressListener, streamListener,
            connectionCallback);//from  ww w  .  j av a  2s .c om
    final GenericObjectPoolConfig configuration = new GenericObjectPoolConfig() {
        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("GenericObjectPoolConfig{");
            sb.append("connections=").append(connections);
            sb.append('}');
            return sb.toString();
        }
    };
    configuration.setJmxEnabled(false);
    configuration.setMinIdle(0);
    configuration.setMaxTotal(connections);
    configuration.setMaxIdle(connections);
    configuration.setBlockWhenExhausted(true);
    configuration.setMaxWaitMillis(BORROW_MAX_WAIT_INTERVAL);
    progress = progressListener;
    retry = Integer.max(PreferencesFactory.get().getInteger("connection.retry"), connections);
    pool = new GenericObjectPool<Session>(new SessionPool(connect, trust, key, cache, transfer.getHost()),
            configuration) {
        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("GenericObjectPool{");
            sb.append("configuration=").append(configuration);
            sb.append('}');
            return sb.toString();
        }
    };
    completion = new DefaultThreadPool<TransferStatus>(connections, "transfer");
}

From source file:com.threecrickets.prudence.cache.SqlCache.java

/**
 * Constructor./* w  ww  . jav  a2  s  . co m*/
 * 
 * @param dataSource
 *        The data source
 * @param maxSize
 *        The max entry count
 * @param poolSize
 *        The number of connections in the pool
 * @param lockSource
 *        The lock source
 */
public SqlCache(DataSource dataSource, int maxSize, int poolSize, LockSource lockSource) {
    this.maxSize = maxSize;
    this.lockSource = lockSource;

    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(poolSize);
    config.setMaxIdle(poolSize);
    config.setMinIdle(poolSize);
    DataSourceConnectionFactory connectionFactory = new DataSourceConnectionFactory(dataSource);
    PoolableConnectionFactory pooledObjectFactory = new PoolableConnectionFactory(connectionFactory, null);
    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pooledObjectFactory,
            config);
    pooledObjectFactory.setPool(pool);
    this.dataSource = new PoolingDataSource<PoolableConnection>(pool);
}

From source file:com.escframework.support.dubbo.RedisRegistry.java

public RedisRegistry(URL url) {
    super(url);/*from   ww w .  j  a va2s .c  om*/
    if (url.isAnyHost()) {
        throw new IllegalStateException("registry address == null");
    }
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setTestOnBorrow(url.getParameter("test.on.borrow", true));
    config.setTestOnReturn(url.getParameter("test.on.return", false));
    config.setTestWhileIdle(url.getParameter("test.while.idle", false));

    if (url.getParameter("max.idle", 0) > 0)

        config.setMaxIdle(url.getParameter("max.idle", 0));
    if (url.getParameter("min.idle", 0) > 0)
        config.setMinIdle(url.getParameter("min.idle", 0));
    if (url.getParameter("max.active", 0) > 0)
        config.setMaxTotal(url.getParameter("max.total", 0));
    if (url.getParameter("max.wait", url.getParameter("timeout", 0)) > 0)
        config.setMaxWaitMillis(url.getParameter("max.wait", url.getParameter("timeout", 0)));
    if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
        config.setNumTestsPerEvictionRun(url.getParameter("num.tests.per.eviction.run", 0));
    if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
        config.setTimeBetweenEvictionRunsMillis(url.getParameter("time.between.eviction.runs.millis", 0));
    if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
        config.setMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0));

    String cluster = url.getParameter("cluster", "failover");
    if (!"failover".equals(cluster) && !"replicate".equals(cluster)) {
        throw new IllegalArgumentException("Unsupported redis cluster: " + cluster
                + ". The redis cluster only supported failover or replicate.");
    }
    replicate = "replicate".equals(cluster);

    List<String> addresses = new ArrayList<String>();
    addresses.add(url.getAddress());
    String[] backups = url.getParameter(Constants.BACKUP_KEY, new String[0]);
    if (backups != null && backups.length > 0) {
        addresses.addAll(Arrays.asList(backups));
    }
    for (String address : addresses) {
        int i = address.indexOf(':');
        String host;
        int port;
        if (i > 0) {
            host = address.substring(0, i);
            port = Integer.parseInt(address.substring(i + 1));
        } else {
            host = address;
            port = DEFAULT_REDIS_PORT;
        }
        this.jedisPools.put(address, new JedisPool(config, host, port,
                url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)));
    }

    this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY,
            Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (!group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    if (!group.endsWith(Constants.PATH_SEPARATOR)) {
        group = group + Constants.PATH_SEPARATOR;
    }
    this.root = group;

    this.expirePeriod = url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT);
    this.expireFuture = expireExecutor.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            try {
                deferExpired(); // 
            } catch (Throwable t) { // 
                logger.error("Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t);
            }
        }
    }, expirePeriod / 2, expirePeriod / 2, TimeUnit.MILLISECONDS);
}