001/*******************************************************************************
002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 ******************************************************************************/
009package org.nuxeo.ecm.core.redis;
010
011import java.io.IOException;
012
013import redis.clients.jedis.Jedis;
014import redis.clients.jedis.exceptions.JedisConnectionException;
015import redis.clients.jedis.exceptions.JedisException;
016import redis.clients.util.Pool;
017
018public class RedisPoolExecutor implements RedisExecutor {
019
020    protected Pool<Jedis> pool;
021
022    public RedisPoolExecutor(Pool<Jedis> pool) {
023        this.pool = pool;
024    }
025
026    @Override
027    public <T> T execute(RedisCallable<T> callable) throws JedisException {
028        Jedis jedis = pool.getResource();
029        boolean brokenResource = false;
030        try {
031            return callable.call(jedis);
032        } catch (JedisConnectionException cause) {
033            brokenResource = true;
034            throw cause;
035        } finally {
036            if (brokenResource) {
037                pool.returnBrokenResource(jedis);
038            } else {
039                pool.returnResource(jedis);
040            }
041        }
042
043    }
044
045    @Override
046    public Pool<Jedis> getPool() {
047        return pool;
048    }
049
050    @Override
051    public boolean supportPipelined() {
052        return true;
053    }
054
055}