Example usage for org.apache.commons.pool2.impl GenericObjectPool getMinIdle

List of usage examples for org.apache.commons.pool2.impl GenericObjectPool getMinIdle

Introduction

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

Prototype

@Override
public int getMinIdle() 

Source Link

Document

Returns the target for the minimum number of idle objects to maintain in the pool.

Usage

From source file:com.netflix.spinnaker.orca.config.RedisConfiguration.java

@Deprecated // rz - Kept for backwards compat with old connection configs
public static JedisPool createPool(GenericObjectPoolConfig redisPoolConfig, String connection, int timeout,
        Registry registry, String poolName) {
    URI redisConnection = URI.create(connection);

    String host = redisConnection.getHost();
    int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();

    String redisConnectionPath = isNotEmpty(redisConnection.getPath()) ? redisConnection.getPath()
            : "/" + DEFAULT_DATABASE;
    int database = Integer.parseInt(redisConnectionPath.split("/", 2)[1]);

    String password = redisConnection.getUserInfo() != null ? redisConnection.getUserInfo().split(":", 2)[1]
            : null;//from w  ww . j av  a2 s.  c o m

    JedisPool jedisPool = new JedisPool(
            redisPoolConfig != null ? redisPoolConfig : new GenericObjectPoolConfig(), host, port, timeout,
            password, database, null);
    final Field poolAccess;
    try {
        poolAccess = Pool.class.getDeclaredField("internalPool");
        poolAccess.setAccessible(true);
        GenericObjectPool<Jedis> pool = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
        registry.gauge(registry.createId("redis.connectionPool.maxIdle", "poolName", poolName), pool,
                (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMaxIdle()).doubleValue());
        registry.gauge(registry.createId("redis.connectionPool.minIdle", "poolName", poolName), pool,
                (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMinIdle()).doubleValue());
        registry.gauge(registry.createId("redis.connectionPool.numActive", "poolName", poolName), pool,
                (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getNumActive()).doubleValue());
        registry.gauge(registry.createId("redis.connectionPool.numIdle", "poolName", poolName), pool,
                (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMaxIdle()).doubleValue());
        registry.gauge(registry.createId("redis.connectionPool.numWaiters", "poolName", poolName), pool,
                (GenericObjectPool<Jedis> p) -> Integer.valueOf(p.getMaxIdle()).doubleValue());
        return jedisPool;
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new BeanCreationException("Error creating Redis pool", e);
    }
}

From source file:com.netflix.spinnaker.kork.jedis.JedisHealthIndicatorFactory.java

public static HealthIndicator build(Pool<Jedis> jedisPool) {
    try {/*  w ww .  j  ava  2s .c o  m*/
        final Pool<Jedis> src = jedisPool;
        final Field poolAccess = Pool.class.getDeclaredField("internalPool");
        poolAccess.setAccessible(true);
        GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
        return () -> {
            Jedis jedis = null;
            Health.Builder health;
            try {
                jedis = src.getResource();
                if ("PONG".equals(jedis.ping())) {
                    health = Health.up();
                } else {
                    health = Health.down();
                }
            } catch (Exception ex) {
                health = Health.down(ex);
            } finally {
                if (jedis != null)
                    jedis.close();
            }
            health.withDetail("maxIdle", internal.getMaxIdle());
            health.withDetail("minIdle", internal.getMinIdle());
            health.withDetail("numActive", internal.getNumActive());
            health.withDetail("numIdle", internal.getNumIdle());
            health.withDetail("numWaiters", internal.getNumWaiters());

            return health.build();
        };
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}

From source file:com.streamsets.pipeline.stage.origin.tokafka.TestKafkaFragmentWriter.java

@Test
public void testLifecycle() throws Exception {
    KafkaTargetConfig configs = new KafkaTargetConfig();
    KafkaFragmentWriter writer = new KafkaFragmentWriter(configs, 2, 10);
    writer = Mockito.spy(writer);//from w  w w .  jav  a2s. c o  m
    writer.init(
            ContextInfoCreator.createSourceContext("n", false, OnRecordError.TO_ERROR, ImmutableList.of("l")));

    Assert.assertEquals(2, writer.getMaxFragmentSizeKB());

    GenericObjectPool pool = writer.getKafkaProducerPool();
    Assert.assertEquals(2, pool.getMinIdle());
    Assert.assertEquals(5, pool.getMaxIdle());
    Assert.assertEquals(10, pool.getMaxTotal());

    GenericObjectPool mockPool = Mockito.mock(GenericObjectPool.class);
    Mockito.doReturn(mockPool).when(writer).getKafkaProducerPool();

    SdcKafkaProducer producer = writer.getKafkaProducer();
    Mockito.verify(mockPool, Mockito.times(1)).borrowObject();

    writer.releaseKafkaProducer(producer);
    Mockito.verify(mockPool, Mockito.times(1)).returnObject(Mockito.eq(producer));

    writer.destroy();
    Mockito.verify(mockPool, Mockito.times(1)).close();
}

From source file:com.adaptris.core.services.splitter.ServiceWorkerPoolTest.java

@Test
public void testCreateObjectPool() throws Exception {
    GenericObjectPool<ServiceWorkerPool.Worker> pool = createCommonsObjectPool();
    assertNotNull(pool);/*from  w w  w.  ja v  a 2  s.c  om*/
    assertEquals(10, pool.getMaxTotal());
    assertEquals(10, pool.getMinIdle());
    assertEquals(10, pool.getMaxIdle());
    assertEquals(-1, pool.getMaxWaitMillis());
    assertTrue(pool.getBlockWhenExhausted());
}

From source file:com.streamsets.pipeline.lib.parser.text.TestTextDataParserFactory.java

@Test
public void testStringBuilderPoolException() throws Exception {

    // Parser with default string builder pool config
    DataParserFactoryBuilder dataParserFactoryBuilder = new DataParserFactoryBuilder(getContext(),
            DataParserFormat.TEXT);/* ww w. j  a  v a  2 s. c o m*/
    WrapperDataParserFactory factory = (WrapperDataParserFactory) dataParserFactoryBuilder.setMaxDataLen(1000)
            .setConfig(TextDataParserFactory.USE_CUSTOM_DELIMITER_KEY, true)
            .setConfig(TextDataParserFactory.CUSTOM_DELIMITER_KEY, "\\\\r\\\\n").build();

    TextDataParserFactory textDataParserFactory = (TextDataParserFactory) factory.getFactory();
    GenericObjectPool<StringBuilder> stringBuilderPool = textDataParserFactory.getStringBuilderPool();
    Assert.assertNotNull(stringBuilderPool);
    Assert.assertEquals(1, stringBuilderPool.getMaxIdle());
    Assert.assertEquals(1, stringBuilderPool.getMinIdle());
    Assert.assertEquals(1, stringBuilderPool.getMaxTotal());
    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());

    factory.getParser("id", "Hello\\r\\nBye");

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(1, stringBuilderPool.getNumActive());

    try {
        factory.getParser("id", "Hello\\r\\nBye");
        Assert.fail("Expected IOException which wraps NoSuchElementException since pool is empty");
    } catch (DataParserException e) {
        Assert.assertTrue(e.getCause() instanceof IOException);
        Assert.assertTrue(e.getCause().getCause() instanceof NoSuchElementException);
    }

}

From source file:com.streamsets.pipeline.lib.parser.text.TestTextDataParserFactory.java

@Test
public void testParserFactoryStringBuilderPool() throws Exception {

    // Parser with default string builder pool config
    DataParserFactoryBuilder dataParserFactoryBuilder = new DataParserFactoryBuilder(getContext(),
            DataParserFormat.TEXT);/*from  w w w . j ava2 s. c  o  m*/
    WrapperDataParserFactory factory = (WrapperDataParserFactory) dataParserFactoryBuilder.setMaxDataLen(1000)
            .setConfig(TextDataParserFactory.USE_CUSTOM_DELIMITER_KEY, true)
            .setConfig(TextDataParserFactory.CUSTOM_DELIMITER_KEY, "\\\\r\\\\n").build();

    TextDataParserFactory textDataParserFactory = (TextDataParserFactory) factory.getFactory();
    GenericObjectPool<StringBuilder> stringBuilderPool = textDataParserFactory.getStringBuilderPool();
    Assert.assertNotNull(stringBuilderPool);
    Assert.assertEquals(1, stringBuilderPool.getMaxIdle());
    Assert.assertEquals(1, stringBuilderPool.getMinIdle());
    Assert.assertEquals(1, stringBuilderPool.getMaxTotal());
    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());

    DataParser parser = factory.getParser("id", "Hello\\r\\nBye");

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(1, stringBuilderPool.getNumActive());

    parser.close();

    Assert.assertEquals(1, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());

    // Parser with non default string builder pool config

    dataParserFactoryBuilder = new DataParserFactoryBuilder(getContext(), DataParserFormat.TEXT);
    factory = (WrapperDataParserFactory) dataParserFactoryBuilder.setMaxDataLen(1000)
            .setStringBuilderPoolSize(5).setConfig(TextDataParserFactory.USE_CUSTOM_DELIMITER_KEY, true)
            .setConfig(TextDataParserFactory.CUSTOM_DELIMITER_KEY, "\\\\r\\\\n").build();

    textDataParserFactory = (TextDataParserFactory) factory.getFactory();
    stringBuilderPool = textDataParserFactory.getStringBuilderPool();
    Assert.assertNotNull(stringBuilderPool);
    Assert.assertEquals(5, stringBuilderPool.getMaxIdle());
    Assert.assertEquals(5, stringBuilderPool.getMinIdle());
    Assert.assertEquals(5, stringBuilderPool.getMaxTotal());

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());

    parser = factory.getParser("id", "Hello\\r\\nBye");
    DataParser parser2 = factory.getParser("id", "Hello\\r\\nBye");
    DataParser parser3 = factory.getParser("id", "Hello\\r\\nBye");

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(3, stringBuilderPool.getNumActive());

    parser.close();
    parser2.close();
    parser3.close();

    Assert.assertEquals(3, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());
}

From source file:com.streamsets.pipeline.lib.parser.log.TestLogDataParserFactory.java

@Test
public void testStringBuilderPoolException() throws Exception {

    // Parser with default string builder pool config
    DataParserFactoryBuilder dataParserFactoryBuilder = new DataParserFactoryBuilder(getContext(),
            DataParserFormat.LOG);//from  w  w  w.  j a  va  2 s.co m
    WrapperDataParserFactory factory = (WrapperDataParserFactory) dataParserFactoryBuilder.setMaxDataLen(1000)
            .setMaxDataLen(100).setMode(LogMode.COMMON_LOG_FORMAT).build();

    LogDataParserFactory logDataParserFactory = (LogDataParserFactory) factory.getFactory();
    GenericObjectPool<StringBuilder> stringBuilderPool = logDataParserFactory.getPreviousLineBuilderPool();
    Assert.assertNotNull(stringBuilderPool);
    Assert.assertEquals(1, stringBuilderPool.getMaxIdle());
    Assert.assertEquals(1, stringBuilderPool.getMinIdle());
    Assert.assertEquals(1, stringBuilderPool.getMaxTotal());
    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());

    factory.getParser("id", "Hello\\r\\nBye");

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(1, stringBuilderPool.getNumActive());

    try {
        factory.getParser("id", "Hello\\r\\nBye");
        Assert.fail("Expected IOException which wraps NoSuchElementException since pool is empty");
    } catch (DataParserException e) {
        Assert.assertTrue(e.getCause() instanceof IOException);
        Assert.assertTrue(e.getCause().getCause() instanceof NoSuchElementException);
    }

}

From source file:com.streamsets.pipeline.lib.parser.log.TestLogDataParserFactory.java

private void testDefaultStringBuilderPool(GenericObjectPool<StringBuilder> stringBuilderPool,
        LogDataParserFactory factory, int poolSize) throws Exception {
    Assert.assertNotNull(stringBuilderPool);
    Assert.assertEquals(poolSize, stringBuilderPool.getMaxIdle());
    Assert.assertEquals(poolSize, stringBuilderPool.getMinIdle());
    Assert.assertEquals(poolSize, stringBuilderPool.getMaxTotal());
    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());

    DataParser parser = factory.getParser("id", "Hello\\r\\nBye");

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(1, stringBuilderPool.getNumActive());

    parser.close();//from w  w w. j a  v a  2  s.  c  o  m

    Assert.assertEquals(1, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());
}

From source file:com.streamsets.pipeline.lib.parser.log.TestLogDataParserFactory.java

private void testNonDefaultStringBuilderPool(GenericObjectPool<StringBuilder> stringBuilderPool,
        LogDataParserFactory factory, int poolSize) throws Exception {
    Assert.assertNotNull(stringBuilderPool);
    Assert.assertEquals(poolSize, stringBuilderPool.getMaxIdle());
    Assert.assertEquals(poolSize, stringBuilderPool.getMinIdle());
    Assert.assertEquals(poolSize, stringBuilderPool.getMaxTotal());

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());

    DataParser parser1 = factory.getParser("id", "Hello\\r\\nBye");
    DataParser parser2 = factory.getParser("id", "Hello\\r\\nBye");
    DataParser parser3 = factory.getParser("id", "Hello\\r\\nBye");

    Assert.assertEquals(0, stringBuilderPool.getNumIdle());
    Assert.assertEquals(3, stringBuilderPool.getNumActive());

    parser1.close();/*ww w .  jav a2s  .c  o m*/
    parser2.close();
    parser3.close();

    Assert.assertEquals(3, stringBuilderPool.getNumIdle());
    Assert.assertEquals(0, stringBuilderPool.getNumActive());
}

From source file:com.netflix.spinnaker.orca.config.RedisConfiguration.java

@Deprecated // rz - Kept for backwards compat with old connection configs
@Bean/*  w  w w  .ja  v  a2 s .co  m*/
HealthIndicator redisHealth(@Qualifier("jedisPool") Pool<Jedis> jedisPool) {
    try {
        final Pool<Jedis> src = jedisPool;
        final Field poolAccess = Pool.class.getDeclaredField("internalPool");
        poolAccess.setAccessible(true);
        GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
        return () -> {
            Jedis jedis = null;
            Health.Builder health;
            try {
                jedis = src.getResource();
                if ("PONG".equals(jedis.ping())) {
                    health = Health.up();
                } else {
                    health = Health.down();
                }
            } catch (Exception ex) {
                health = Health.down(ex);
            } finally {
                if (jedis != null)
                    jedis.close();
            }
            health.withDetail("maxIdle", internal.getMaxIdle());
            health.withDetail("minIdle", internal.getMinIdle());
            health.withDetail("numActive", internal.getNumActive());
            health.withDetail("numIdle", internal.getNumIdle());
            health.withDetail("numWaiters", internal.getNumWaiters());

            return health.build();
        };
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}