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);
}
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");
}
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);
}
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;
}
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());
}
Aggregations