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

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

Introduction

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

Prototype

@Override
    public int getNumActive() 

Source Link

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 w  w.  j  a  va  2 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  w w. ja v a  2  s  . 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.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);//from w w  w .ja 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);//  ww w .  ja  va 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());

    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.netflix.spinnaker.orca.config.RedisConfiguration.java

@Deprecated // rz - Kept for backwards compat with old connection configs
@Bean//w w  w.  j  a v a2s  .  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);
    }
}

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);//  www  . j  av a  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 2s  . 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();/*from   w  ww.ja v a 2s  .c om*/
    parser2.close();
    parser3.close();

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

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

public void warmup(final GenericObjectPool<Worker> objectPool) throws CoreException {
    ExecutorService populator = Executors
            .newCachedThreadPool(new ManagedThreadFactory(this.getClass().getSimpleName()));
    try {//from   w  w w  .ja  v a 2 s .  co  m
        log.trace("Warming up {} service-workers", maxThreads);
        final List<Future<Worker>> futures = new ArrayList<>(maxThreads);

        for (int i = 0; i < maxThreads; i++) {
            futures.add(populator.submit(new Callable<Worker>() {

                @Override
                public Worker call() throws Exception {
                    return objectPool.borrowObject();
                }

            }));
        }
        for (Worker w : waitFor(futures)) {
            objectPool.returnObject(w);
        }
        log.trace("ObjectPool contains {} (active) of {} objects", objectPool.getNumActive(),
                objectPool.getNumIdle());
    } catch (Exception e) {
        throw ExceptionHelper.wrapCoreException(e);
    } finally {
        populator.shutdownNow();
    }
}

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

/**
 * //ww w  .ja v  a  2 s. c  o m
 * @deprecated since 3.8.3 switch to commons-pool2 instead.
 */
@Deprecated
@Removal(version = "3.9.0", message = "use commons-pool2 variant instead")
public void warmup(final org.apache.commons.pool.impl.GenericObjectPool<Worker> objectPool)
        throws CoreException {
    logDeprecationWarning();
    ExecutorService populator = Executors
            .newCachedThreadPool(new ManagedThreadFactory(this.getClass().getSimpleName()));
    try {
        log.trace("Warming up {} service-workers", maxThreads);
        final List<Future<Worker>> futures = new ArrayList<>(maxThreads);

        for (int i = 0; i < maxThreads; i++) {
            futures.add(populator.submit(new Callable<Worker>() {

                @Override
                public Worker call() throws Exception {
                    return objectPool.borrowObject();
                }

            }));
        }
        for (Worker w : waitFor(futures)) {
            objectPool.returnObject(w);
        }
        log.trace("ObjectPool contains {} (active) of {} objects", objectPool.getNumActive(),
                objectPool.getNumIdle());

    } catch (Exception e) {
        throw ExceptionHelper.wrapCoreException(e);
    } finally {
        populator.shutdownNow();
    }
}