Example usage for org.apache.commons.pool.impl GenericKeyedObjectPool setMaxWait

List of usage examples for org.apache.commons.pool.impl GenericKeyedObjectPool setMaxWait

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericKeyedObjectPool setMaxWait.

Prototype

public synchronized void setMaxWait(long maxWait) 

Source Link

Document

Sets the maximum amount of time (in milliseconds) the #borrowObject method should block before throwing an exception when the pool is exhausted and the #setWhenExhaustedAction "when exhausted" action is #WHEN_EXHAUSTED_BLOCK .

Usage

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericKeyedObjectPool.java

public void testBlockedKeyDoesNotBlockPool() throws Exception {
    SimpleFactory factory = new SimpleFactory();
    GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory);
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    pool.setMaxWait(5000);
    pool.setMaxActive(1);/* w  w w .  j  av  a 2s .com*/
    pool.setMaxTotal(-1);
    pool.borrowObject("one");
    long start = System.currentTimeMillis();
    // Needs to be in a separate thread as this will block
    Runnable simple = new SimpleTestThread(pool, "one");
    Thread borrowThread = new Thread(simple, "borrowThread");
    borrowThread.start();
    // This should be almost instant. If it isn't it means this thread got
    // stuck behind the thread created above which is bad.
    // Give other thread a chance to start        
    //Thread.sleep(1000);    
    pool.borrowObject("two");
    long end = System.currentTimeMillis();
    // If it fails it will be more than 4000ms (5000 less the 1000 sleep)
    // If it passes it should be almost instant
    // Use 3000ms as the threshold - should avoid timing issues on most
    // (all? platforms)
    //assertTrue ((end-start) < 4000);

}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericKeyedObjectPool.java

public void testSettersAndGetters() throws Exception {
    GenericKeyedObjectPool pool = new GenericKeyedObjectPool();
    {//ww  w  .  ja  v  a2s .c om
        pool.setFactory(new SimpleFactory());
    }
    {
        pool.setMaxActive(123);
        assertEquals(123, pool.getMaxActive());
    }
    {
        pool.setMaxIdle(12);
        assertEquals(12, pool.getMaxIdle());
    }
    {
        pool.setMaxWait(1234L);
        assertEquals(1234L, pool.getMaxWait());
    }
    {
        pool.setMinEvictableIdleTimeMillis(12345L);
        assertEquals(12345L, pool.getMinEvictableIdleTimeMillis());
    }
    {
        pool.setNumTestsPerEvictionRun(11);
        assertEquals(11, pool.getNumTestsPerEvictionRun());
    }
    {
        pool.setTestOnBorrow(true);
        assertTrue(pool.getTestOnBorrow());
        pool.setTestOnBorrow(false);
        assertTrue(!pool.getTestOnBorrow());
    }
    {
        pool.setTestOnReturn(true);
        assertTrue(pool.getTestOnReturn());
        pool.setTestOnReturn(false);
        assertTrue(!pool.getTestOnReturn());
    }
    {
        pool.setTestWhileIdle(true);
        assertTrue(pool.getTestWhileIdle());
        pool.setTestWhileIdle(false);
        assertTrue(!pool.getTestWhileIdle());
    }
    {
        pool.setTimeBetweenEvictionRunsMillis(11235L);
        assertEquals(11235L, pool.getTimeBetweenEvictionRunsMillis());
    }
    {
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
    }
}