Search in sources :

Example 6 with PooledObjectFactory

use of org.apache.commons.pool2.PooledObjectFactory in project tomcat by apache.

the class PoolImplUtils method getFactoryType.

/**
 * Identifies the concrete type of object that an object factory creates.
 *
 * @param factoryClass The factory to examine
 *
 * @return the type of object the factory creates
 */
@SuppressWarnings("rawtypes")
static Class<?> getFactoryType(final Class<? extends PooledObjectFactory> factoryClass) {
    final Class<PooledObjectFactory> type = PooledObjectFactory.class;
    final Object genericType = getGenericType(type, factoryClass);
    if (genericType instanceof Integer) {
        // POOL-324 org.apache.commons.pool2.impl.GenericObjectPool.getFactoryType() throws
        // java.lang.ClassCastException
        // 
        // A bit hackish, but we must handle cases when getGenericType() does not return a concrete types.
        final ParameterizedType pi = getParameterizedType(type, factoryClass);
        if (pi != null) {
            final Type[] bounds = ((TypeVariable) pi.getActualTypeArguments()[((Integer) genericType).intValue()]).getBounds();
            if (bounds != null && bounds.length > 0) {
                final Type bound0 = bounds[0];
                if (bound0 instanceof Class) {
                    return (Class<?>) bound0;
                }
            }
        }
        // last resort: Always return a Class
        return Object.class;
    }
    return (Class<?>) genericType;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) PooledObjectFactory(org.apache.tomcat.dbcp.pool2.PooledObjectFactory) TypeVariable(java.lang.reflect.TypeVariable)

Example 7 with PooledObjectFactory

use of org.apache.commons.pool2.PooledObjectFactory in project cachecloud by sohutv.

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)

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