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

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

Introduction

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

Prototype

@Override
    public int getNumIdle() 

Source Link

Usage

From source file:com.heliosapm.streams.collector.ds.pool.TestMQPool.java

/**
 * @param args/*from   w  ww.  jav a 2s . c  o  m*/
 */
public static void main(String[] args) {
    try {
        log("Pool Test");
        log(TEST_PROPS);
        JMXHelper.fireUpJMXMPServer(1077);
        final Properties p = Props.strToProps(TEST_PROPS);
        log("Props:" + p);
        final GenericObjectPool<Object> pool = null;//(GenericObjectPool<Object>)PoolConfig.deployPool(p);
        pool.preparePool();
        log("Pool Deployed:" + pool.getNumIdle());
        final List<Object> objects = new ArrayList<Object>();
        for (int i = 0; i < 4; i++) {
            try {
                final Object o = pool.borrowObject();
                log("Borrowed:" + o);
                objects.add(o);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log("Objects:" + objects.size());
        StdInCommandHandler.getInstance().registerCommand("close", new Runnable() {
            public void run() {
                for (Object o : objects) {
                    pool.returnObject(o);
                }
                objects.clear();
                try {
                    pool.close();
                } catch (Exception ex) {
                    ex.printStackTrace(System.err);
                }
            }
        }).run();
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }

}

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

public static HealthIndicator build(Pool<Jedis> jedisPool) {
    try {/*  ww  w.  j a va 2 s .co  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.adaptris.core.services.splitter.ServiceWorkerPoolTest.java

@Test
public void testWarmup() throws Exception {
    GenericObjectPool<ServiceWorkerPool.Worker> objPool = createCommonsObjectPool();
    warmup(objPool);//from ww w . java2 s  . c o  m
    assertEquals(10, objPool.getNumIdle());
    closeQuietly(objPool);
}

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  .  com
    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.java  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.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);//w  w  w . j  av a  2  s . com
    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();/*w w w  .ja  va 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();/*from  ww  w  .  ja  va2s.c  om*/
    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 ava  2 s  .  c  o 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.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 {/*w ww.  ja v  a2 s. c  o  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();
    }
}