Search in sources :

Example 6 with PooledObject

use of org.apache.commons.pool2.PooledObject 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 7 with PooledObject

use of org.apache.commons.pool2.PooledObject in project cuba by cuba-platform.

the class AbstractScripting method getPool.

private synchronized GenericKeyedObjectPool<String, Script> getPool() {
    if (pool == null) {
        GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
        poolConfig.setMaxTotalPerKey(-1);
        poolConfig.setMaxIdlePerKey(globalConfig.getGroovyEvaluationPoolMaxIdle());
        pool = new GenericKeyedObjectPool<>(new BaseKeyedPooledObjectFactory<String, Script>() {

            @Override
            public Script create(String key) throws Exception {
                return createScript(key);
            }

            @Override
            public PooledObject<Script> wrap(Script value) {
                return new DefaultPooledObject<>(value);
            }
        }, poolConfig);
    }
    return pool;
}
Also used : GenericKeyedObjectPoolConfig(org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig) Script(groovy.lang.Script) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) BaseKeyedPooledObjectFactory(org.apache.commons.pool2.BaseKeyedPooledObjectFactory)

Example 8 with PooledObject

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

the class NettyClientFactory method makeObject.

@Override
public PooledObject<NettyClient> makeObject() throws Exception {
    ChannelFuture f = b.connect(address);
    f.get(connectTimeoutMilli, TimeUnit.MILLISECONDS);
    Channel channel = f.channel();
    logger.info("[makeObject]{}", channel);
    NettyClient nettyClient = new DefaultNettyClient(channel);
    channel.attr(NettyClientHandler.KEY_CLIENT).set(nettyClient);
    return new DefaultPooledObject<NettyClient>(nettyClient);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel)

Example 9 with PooledObject

use of org.apache.commons.pool2.PooledObject 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)

Example 10 with PooledObject

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

the class GenericKeyedObjectPool method evict.

/**
 * {@inheritDoc}
 * <p>
 * Successive activations of this method examine objects in keyed sub-pools
 * in sequence, cycling through the keys and examining objects in
 * oldest-to-youngest order within the keyed sub-pools.
 * </p>
 */
@Override
public void evict() throws Exception {
    assertOpen();
    if (getNumIdle() > 0) {
        PooledObject<T> underTest = null;
        final EvictionPolicy<T> evictionPolicy = getEvictionPolicy();
        synchronized (evictionLock) {
            final EvictionConfig evictionConfig = new EvictionConfig(getMinEvictableIdleDuration(), getSoftMinEvictableIdleDuration(), getMinIdlePerKey());
            final boolean testWhileIdle = getTestWhileIdle();
            for (int i = 0, m = getNumTests(); i < m; i++) {
                if (evictionIterator == null || !evictionIterator.hasNext()) {
                    if (evictionKeyIterator == null || !evictionKeyIterator.hasNext()) {
                        final List<K> keyCopy = new ArrayList<>();
                        final Lock readLock = keyLock.readLock();
                        readLock.lock();
                        try {
                            keyCopy.addAll(poolKeyList);
                        } finally {
                            readLock.unlock();
                        }
                        evictionKeyIterator = keyCopy.iterator();
                    }
                    while (evictionKeyIterator.hasNext()) {
                        evictionKey = evictionKeyIterator.next();
                        final ObjectDeque<T> objectDeque = poolMap.get(evictionKey);
                        if (objectDeque == null) {
                            continue;
                        }
                        final Deque<PooledObject<T>> idleObjects = objectDeque.getIdleObjects();
                        evictionIterator = new EvictionIterator(idleObjects);
                        if (evictionIterator.hasNext()) {
                            break;
                        }
                        evictionIterator = null;
                    }
                }
                if (evictionIterator == null) {
                    // Pools exhausted
                    return;
                }
                final Deque<PooledObject<T>> idleObjects;
                try {
                    underTest = evictionIterator.next();
                    idleObjects = evictionIterator.getIdleObjects();
                } catch (final NoSuchElementException nsee) {
                    // Object was borrowed in another thread
                    // Don't count this as an eviction test so reduce i;
                    i--;
                    evictionIterator = null;
                    continue;
                }
                if (!underTest.startEvictionTest()) {
                    // Object was borrowed in another thread
                    // Don't count this as an eviction test so reduce i;
                    i--;
                    continue;
                }
                // User provided eviction policy could throw all sorts of
                // crazy exceptions. Protect against such an exception
                // killing the eviction thread.
                boolean evict;
                try {
                    evict = evictionPolicy.evict(evictionConfig, underTest, poolMap.get(evictionKey).getIdleObjects().size());
                } catch (final Throwable t) {
                    // Slightly convoluted as SwallowedExceptionListener
                    // uses Exception rather than Throwable
                    PoolUtils.checkRethrow(t);
                    swallowException(new Exception(t));
                    // Don't evict on error conditions
                    evict = false;
                }
                if (evict) {
                    destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                    destroyedByEvictorCount.incrementAndGet();
                } else {
                    if (testWhileIdle) {
                        boolean active = false;
                        try {
                            factory.activateObject(evictionKey, underTest);
                            active = true;
                        } catch (final Exception e) {
                            destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                            destroyedByEvictorCount.incrementAndGet();
                        }
                        if (active) {
                            boolean validate = false;
                            Throwable validationThrowable = null;
                            try {
                                validate = factory.validateObject(evictionKey, underTest);
                            } catch (final Throwable t) {
                                PoolUtils.checkRethrow(t);
                                validationThrowable = t;
                            }
                            if (!validate) {
                                destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                                destroyedByEvictorCount.incrementAndGet();
                                if (validationThrowable != null) {
                                    if (validationThrowable instanceof RuntimeException) {
                                        throw (RuntimeException) validationThrowable;
                                    }
                                    throw (Error) validationThrowable;
                                }
                            } else {
                                try {
                                    factory.passivateObject(evictionKey, underTest);
                                } catch (final Exception e) {
                                    destroy(evictionKey, underTest, true, DestroyMode.NORMAL);
                                    destroyedByEvictorCount.incrementAndGet();
                                }
                            }
                        }
                    }
                    if (!underTest.endEvictionTest(idleObjects)) {
                    // TODO - May need to add code here once additional
                    // states are used
                    }
                }
            }
        }
    }
    final AbandonedConfig ac = this.abandonedConfig;
    if (ac != null && ac.getRemoveAbandonedOnMaintenance()) {
        removeAbandoned(ac);
    }
}
Also used : ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) PooledObject(org.apache.tomcat.dbcp.pool2.PooledObject) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)8 PooledObject (org.apache.commons.pool2.PooledObject)5 URISyntaxException (java.net.URISyntaxException)4 NoSuchElementException (java.util.NoSuchElementException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 PooledObjectFactory (org.apache.commons.pool2.PooledObjectFactory)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 ArrayList (java.util.ArrayList)3 PooledObject (org.apache.tomcat.dbcp.pool2.PooledObject)3 Jedis (redis.clients.jedis.Jedis)3 JedisPool (redis.clients.jedis.JedisPool)3 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 Lock (java.util.concurrent.locks.Lock)2