Search in sources :

Example 1 with PooledObjectFactory

use of org.apache.commons.pool2.PooledObjectFactory in project jedis by xetorthio.

the class JedisPoolTest method returnResourceDestroysResourceOnException.

@Test
public void returnResourceDestroysResourceOnException() {
    class CrashingJedis extends Jedis {

        @Override
        public void resetState() {
            throw new RuntimeException();
        }
    }
    final AtomicInteger destroyed = new AtomicInteger(0);
    class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {

        @Override
        public PooledObject<Jedis> makeObject() throws Exception {
            return new DefaultPooledObject<Jedis>(new CrashingJedis());
        }

        @Override
        public void destroyObject(PooledObject<Jedis> p) throws Exception {
            destroyed.incrementAndGet();
        }

        @Override
        public boolean validateObject(PooledObject<Jedis> p) {
            return true;
        }

        @Override
        public void activateObject(PooledObject<Jedis> p) throws Exception {
        }

        @Override
        public void passivateObject(PooledObject<Jedis> p) throws Exception {
        }
    }
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    pool.initPool(config, new CrashingJedisPooledObjectFactory());
    Jedis crashingJedis = pool.getResource();
    try {
        crashingJedis.close();
    } catch (Exception ignored) {
    }
    assertEquals(destroyed.get(), 1);
}
Also used : Jedis(redis.clients.jedis.Jedis) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObjectFactory(org.apache.commons.pool2.PooledObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URISyntaxException(java.net.URISyntaxException) JedisException(redis.clients.jedis.exceptions.JedisException) InvalidURIException(redis.clients.jedis.exceptions.InvalidURIException) JedisExhaustedPoolException(redis.clients.jedis.exceptions.JedisExhaustedPoolException) Test(org.junit.Test)

Example 2 with PooledObjectFactory

use of org.apache.commons.pool2.PooledObjectFactory in project fess-crawler by codelibs.

the class WebDriverClientTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    CrawlerPooledObjectFactory<CrawlerWebDriver> pooledObjectFactory = new CrawlerPooledObjectFactory<>();
    pooledObjectFactory.setComponentName("webDriver");
    pooledObjectFactory.setOnDestroyListener(p -> {
        final CrawlerWebDriver driver = p.getObject();
        driver.quit();
    });
    final StandardCrawlerContainer container = new StandardCrawlerContainer();
    container.prototype("webDriver", CrawlerWebDriver.class).singleton("mimeTypeHelper", MimeTypeHelperImpl.class).singleton("pooledObjectFactory", pooledObjectFactory).singleton("webDriverPool", new GenericObjectPool<>(pooledObjectFactory), null, pool -> {
        pool.close();
    }).<AOnClickAction>singleton("aOnClickAction", AOnClickAction.class).<FormAction>singleton("formAction", FormAction.class).<WebDriverClient>singleton("webDriverClient", WebDriverClient.class, client -> {
        AOnClickAction aOnClick = container.getComponent("aOnClickAction");
        aOnClick.setName("aOnClick");
        aOnClick.setCssQuery("a");
        client.addUrlAction(aOnClick);
        FormAction formAction = container.getComponent("formAction");
        formAction.setName("form");
        formAction.setCssQuery("form");
        client.addUrlAction(formAction);
    });
    webDriverClient = container.getComponent("webDriverClient");
}
Also used : StandardCrawlerContainer(org.codelibs.fess.crawler.container.StandardCrawlerContainer) MimeTypeHelperImpl(org.codelibs.fess.crawler.helper.impl.MimeTypeHelperImpl) Iterator(java.util.Iterator) CrawlerPooledObjectFactory(org.codelibs.fess.crawler.pool.CrawlerPooledObjectFactory) ResourceUtil(org.codelibs.core.io.ResourceUtil) PlainTestCase(org.dbflute.utflute.core.PlainTestCase) CrawlerWebDriver(org.codelibs.fess.crawler.client.http.webdriver.CrawlerWebDriver) Resource(javax.annotation.Resource) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Set(java.util.Set) File(java.io.File) AOnClickAction(org.codelibs.fess.crawler.client.http.action.AOnClickAction) SystemUtil(org.codelibs.core.lang.SystemUtil) Constants(org.codelibs.fess.crawler.Constants) RequestData(org.codelibs.fess.crawler.entity.RequestData) FormAction(org.codelibs.fess.crawler.client.http.action.FormAction) WebDriverClient(org.codelibs.fess.crawler.client.http.WebDriverClient) CrawlerWebServer(org.codelibs.fess.crawler.util.CrawlerWebServer) RequestDataBuilder(org.codelibs.fess.crawler.builder.RequestDataBuilder) InputStreamUtil(org.codelibs.core.io.InputStreamUtil) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) AOnClickAction(org.codelibs.fess.crawler.client.http.action.AOnClickAction) WebDriverClient(org.codelibs.fess.crawler.client.http.WebDriverClient) FormAction(org.codelibs.fess.crawler.client.http.action.FormAction) CrawlerWebDriver(org.codelibs.fess.crawler.client.http.webdriver.CrawlerWebDriver) StandardCrawlerContainer(org.codelibs.fess.crawler.container.StandardCrawlerContainer) CrawlerPooledObjectFactory(org.codelibs.fess.crawler.pool.CrawlerPooledObjectFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) MimeTypeHelperImpl(org.codelibs.fess.crawler.helper.impl.MimeTypeHelperImpl)

Example 3 with PooledObjectFactory

use of org.apache.commons.pool2.PooledObjectFactory in project new-cloud by xie-summer.

the class JedisPoolTest method returnResourceDestroysResourceOnException.

@Test
public void returnResourceDestroysResourceOnException() {
    class CrashingJedis extends Jedis {

        @Override
        public void resetState() {
            throw new RuntimeException();
        }
    }
    final AtomicInteger destroyed = new AtomicInteger(0);
    class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {

        @Override
        public PooledObject<Jedis> makeObject() throws Exception {
            return new DefaultPooledObject<Jedis>(new CrashingJedis());
        }

        @Override
        public void destroyObject(PooledObject<Jedis> p) throws Exception {
            destroyed.incrementAndGet();
        }

        @Override
        public boolean validateObject(PooledObject<Jedis> p) {
            return true;
        }

        @Override
        public void activateObject(PooledObject<Jedis> p) throws Exception {
        }

        @Override
        public void passivateObject(PooledObject<Jedis> p) throws Exception {
        }
    }
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    pool.initPool(config, new CrashingJedisPooledObjectFactory());
    Jedis crashingJedis = pool.getResource();
    try {
        crashingJedis.close();
    } catch (Exception ignored) {
    }
    assertEquals(destroyed.get(), 1);
}
Also used : Jedis(redis.clients.jedis.Jedis) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObjectFactory(org.apache.commons.pool2.PooledObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URISyntaxException(java.net.URISyntaxException) JedisException(redis.clients.jedis.exceptions.JedisException) InvalidURIException(redis.clients.jedis.exceptions.InvalidURIException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Example 4 with PooledObjectFactory

use of org.apache.commons.pool2.PooledObjectFactory in project x-pipe by ctripcorp.

the class XpipeNettyClientKeyedObjectPool method doInitialize.

@Override
protected void doInitialize() throws Exception {
    this.pooledObjectFactory = new NettyKeyedPoolClientFactory();
    pooledObjectFactory.start();
    GenericKeyedObjectPool<InetSocketAddress, NettyClient> genericKeyedObjectPool = new GenericKeyedObjectPool<>(pooledObjectFactory, config);
    genericKeyedObjectPool.setTestOnBorrow(true);
    genericKeyedObjectPool.setTestOnCreate(true);
    genericKeyedObjectPool.setSoftMinEvictableIdleTimeMillis(DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    genericKeyedObjectPool.setMinEvictableIdleTimeMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    genericKeyedObjectPool.setTimeBetweenEvictionRunsMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
    this.objectPool = genericKeyedObjectPool;
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) NettyKeyedPoolClientFactory(com.ctrip.xpipe.netty.commands.NettyKeyedPoolClientFactory) GenericKeyedObjectPool(org.apache.commons.pool2.impl.GenericKeyedObjectPool) InetSocketAddress(java.net.InetSocketAddress)

Example 5 with PooledObjectFactory

use of org.apache.commons.pool2.PooledObjectFactory in project jedis by xetorthio.

the class JedisPoolTest method returnResourceDestroysResourceOnException.

@Test
public void returnResourceDestroysResourceOnException() {
    class CrashingJedis extends Jedis {

        @Override
        public void resetState() {
            throw new RuntimeException();
        }
    }
    final AtomicInteger destroyed = new AtomicInteger(0);
    class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {

        @Override
        public PooledObject<Jedis> makeObject() throws Exception {
            return new DefaultPooledObject<Jedis>(new CrashingJedis());
        }

        @Override
        public void destroyObject(PooledObject<Jedis> p) throws Exception {
            destroyed.incrementAndGet();
        }

        @Override
        public boolean validateObject(PooledObject<Jedis> p) {
            return true;
        }

        @Override
        public void activateObject(PooledObject<Jedis> p) throws Exception {
        }

        @Override
        public void passivateObject(PooledObject<Jedis> p) throws Exception {
        }
    }
    GenericObjectPoolConfig<Jedis> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(1);
    JedisPool pool = new JedisPool(config, new CrashingJedisPooledObjectFactory());
    Jedis crashingJedis = pool.getResource();
    try {
        crashingJedis.close();
    } catch (Exception ignored) {
    }
    assertEquals(1, destroyed.get());
}
Also used : DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObjectFactory(org.apache.commons.pool2.PooledObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) URISyntaxException(java.net.URISyntaxException) JedisException(redis.clients.jedis.exceptions.JedisException) InvalidURIException(redis.clients.jedis.exceptions.InvalidURIException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Aggregations

URISyntaxException (java.net.URISyntaxException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 PooledObject (org.apache.commons.pool2.PooledObject)4 PooledObjectFactory (org.apache.commons.pool2.PooledObjectFactory)4 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)4 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 Test (org.junit.Test)4 InvalidURIException (redis.clients.jedis.exceptions.InvalidURIException)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 Jedis (redis.clients.jedis.Jedis)3 JedisPool (redis.clients.jedis.JedisPool)3 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)1 NettyKeyedPoolClientFactory (com.ctrip.xpipe.netty.commands.NettyKeyedPoolClientFactory)1 File (java.io.File)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 InetSocketAddress (java.net.InetSocketAddress)1 Iterator (java.util.Iterator)1