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.retry; 017 018import org.apache.commons.logging.LogFactory; 019 020public class Retry { 021 022 public interface Block<T> { 023 T retry() throws FailException, ContinueException; 024 } 025 026 public interface Policy { 027 boolean allow(); 028 029 void pause(); 030 } 031 032 public static class ContinueException extends Exception { 033 034 private static final long serialVersionUID = 1L; 035 036 public ContinueException(Throwable cause) { 037 super(cause); 038 } 039 040 } 041 042 public static class FailException extends Exception { 043 044 public FailException(String message) { 045 super(message); 046 } 047 048 public FailException(Throwable cause) { 049 super(cause); 050 } 051 052 private static final long serialVersionUID = 1L; 053 054 } 055 056 public <T> T retry(Block<T> block, Policy policy) throws FailException { 057 FailException causes = new FailException( 058 "Cannot execute block, retry policy failed, check supressed exception for more infos"); 059 while (policy.allow()) { 060 try { 061 return block.retry(); 062 } catch (ContinueException error) { 063 causes.addSuppressed(error.getCause()); 064 } catch (FailException error) { 065 causes.addSuppressed(error.getCause()); 066 throw causes; 067 } 068 policy.pause(); 069 } 070 throw causes; 071 } 072 073}