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

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

Introduction

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

Prototype

public void preparePool() throws Exception 

Source Link

Document

Tries to ensure that #getMinIdle() idle instances are available in the pool.

Usage

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

/**
 * @param args//w w  w  .j  av  a2  s .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.heliosapm.streams.collector.ds.pool.PoolConfig.java

/**
 * Creates and deploys an ObjectPool based on the passed config props
 * @param p The configuration properties
 * @return the created GenericObjectPool
 *//*from   ww  w.j a v  a2  s .  c om*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static GenericObjectPool<?> deployPool(final Properties p) {
    final String factoryName = (String) p.remove(POOLED_OBJECT_FACTORY_KEY);
    final String poolName = p.getProperty(NAME.name().toLowerCase());
    if (poolName == null || poolName.trim().isEmpty())
        throw new RuntimeException("Pool was not assigned a name");
    if (factoryName == null || factoryName.trim().isEmpty())
        throw new RuntimeException("No pooled object factory defined");
    final GenericObjectPoolConfig cfg = DEFAULT_CONFIG.clone();
    try {
        final Class<PooledObjectFactoryBuilder<?>> clazz = loadFactoryClass(factoryName);
        final Constructor<PooledObjectFactoryBuilder<?>> ctor = clazz.getDeclaredConstructor(Properties.class);
        final PooledObjectFactoryBuilder<?> factory = ctor.newInstance(p);
        for (final String key : p.stringPropertyNames()) {
            if (isPoolConfig(key)) {
                final PoolConfig pc = decode(key);
                pc.apply(cfg, p.get(key));
            }
        }
        final PooledObjectFactory<?> pooledObjectFactory = factory.factory();
        GenericObjectPool<?> pool = new GenericObjectPool(pooledObjectFactory, cfg);
        pool.setSwallowedExceptionListener(EX_LISTENER);
        if (factory instanceof PoolAwareFactory) {
            ((PoolAwareFactory) factory).setPool(pool);
        }
        GlobalCacheService.getInstance().put("pool/" + poolName, pool);
        pool.setAbandonedConfig(Abandoned.create(p));
        pool.preparePool();
        return pool;
    } catch (Exception ex) {
        throw new RuntimeException("Failed to create GenericObjectPool from properties [" + p + "]", ex);
    }
}