001/* 002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.nuxeo.ecm.core.redis; 017 018import org.nuxeo.ecm.core.redis.retry.ExponentialBackofDelay; 019import org.nuxeo.ecm.core.redis.retry.Retry; 020import org.nuxeo.ecm.core.redis.retry.Retry.ContinueException; 021import org.nuxeo.ecm.core.redis.retry.Retry.FailException; 022 023import redis.clients.jedis.Jedis; 024import redis.clients.jedis.exceptions.JedisConnectionException; 025import redis.clients.util.Pool; 026 027public class RedisFailoverExecutor implements RedisExecutor { 028 029 protected final int timeout; 030 031 protected final RedisExecutor executor; 032 033 public RedisFailoverExecutor(int timeout, RedisExecutor base) { 034 this.timeout = timeout; 035 executor = base; 036 } 037 038 @Override 039 public <T> T execute(final RedisCallable<T> callable) throws JedisConnectionException { 040 try { 041 return new Retry().retry(new Retry.Block<T>() { 042 043 @Override 044 public T retry() throws ContinueException, FailException { 045 try { 046 return executor.execute(callable); 047 } catch (JedisConnectionException cause) { 048 throw new Retry.ContinueException(cause); 049 } 050 } 051 052 }, new ExponentialBackofDelay(1, timeout)); 053 } catch (FailException cause) { 054 throw new JedisConnectionException("Cannot reconnect to jedis ..", cause); 055 } 056 } 057 058 @Override 059 public Pool<Jedis> getPool() { 060 return executor.getPool(); 061 } 062 063 @Override 064 public boolean supportPipelined() { 065 return executor.supportPipelined(); 066 } 067 068}