org.vibur.objectpool.ConcurrentPool Java Examples

The following examples show how to use org.vibur.objectpool.ConcurrentPool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViburDBCPDataSource.java    From vibur-dbcp with Apache License 2.0 5 votes vote down vote up
private void doStart() throws ViburDBCPException {
    if (!state.compareAndSet(NEW, WORKING)) {
        throw new IllegalStateException();
    }

    validateConfig();

    if (getExternalDataSource() == null) {
        initJdbcDriver();
    }
    if (getConnector() == null) {
        setConnector(buildConnector(this, getUsername(), getPassword()));
    }

    initDefaultHooks();

    ViburObjectFactory connectionFactory = getConnectionFactory();
    if (connectionFactory == null) {
        setConnectionFactory(connectionFactory = new ConnectionFactory(this));
    }
    PoolService<ConnHolder> pool = getPool();
    if (pool == null) {
        if (isPoolEnableConnectionTracking() && getTakenConnectionsFormatter() == null) {
            setTakenConnectionsFormatter(new TakenConnectionsFormatter.Default(this));
        }

        pool = new ConcurrentPool<>(getConcurrentCollection(), connectionFactory,
                getPoolInitialSize(), getPoolMaxSize(), isPoolFair(),
                isPoolEnableConnectionTracking() ? new ViburListener(this) : null);
        setPool(pool);
    }
    poolOperations = new PoolOperations(this, connectionFactory, pool);

    initPoolReducer();
    initStatementCache();

    if (isEnableJMX()) {
        registerMBean(this);
    }
}
 
Example #2
Source File: SamplingPoolReducerTest.java    From vibur-object-pool with Apache License 2.0 4 votes vote down vote up
@Test
public void testPoolShrinking() throws InterruptedException {
    pool = new ConcurrentPool<>(new ConcurrentLinkedDequeCollection<>(), new SimpleObjectFactory(), 10, 100, false);

    // tests the initial pool state
    assertEquals(10, pool.initialSize());
    assertEquals(100, pool.maxSize());

    assertEquals(10, pool.createdTotal());
    assertEquals(10, pool.remainingCreated());
    assertEquals(100, pool.remainingCapacity());
    assertEquals(0, pool.taken());

    // takes 90 objects and test
    Object[] objs = new Object[90];
    for (int i = 0; i < 90; i++) {
        objs[i] = pool.take();
        assertNotNull(objs[i]);
    }
    assertEquals(90, pool.createdTotal());
    assertEquals(0, pool.remainingCreated());
    assertEquals(10, pool.remainingCapacity());
    assertEquals(90, pool.taken());

    // restores 30 objects and test
    for (int i = 0; i < 30; i++) {
        pool.restore(objs[i]);
    }
    assertEquals(90, pool.createdTotal());
    assertEquals(30, pool.remainingCreated());
    assertEquals(40, pool.remainingCapacity());
    assertEquals(60, pool.taken());

    // creates, starts and then terminates the pool reducer
    final CountDownLatch finishLatch = new CountDownLatch(2);
    ThreadedPoolReducer poolReducer = new SamplingPoolReducer(pool, 400, TimeUnit.MILLISECONDS, 500) {
        @Override
        protected void afterReduce(int reduction, int reduced, Throwable thrown) {
            super.afterReduce(reduction, reduced, thrown);
            finishLatch.countDown();
        }
    };
    poolReducer.start();
    finishLatch.await();
    poolReducer.terminate();

    // Tests the pool metrics after the reducer was called 2 times.
    // Maximum allowed reduction of 20% of 90 will apply on the first call,
    // and on the second call the reduction will be 12, as this is the number
    // of remaining created elements in the pool (smaller than 20% of 72 which is 14.4),
    // i.e. 90 - 18 - 12 = 60 elements created total.
    assertEquals(60, pool.createdTotal());
    assertEquals(0, pool.remainingCreated());
    assertEquals(40, pool.remainingCapacity());
    assertEquals(60, pool.taken());
}