| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.springframework.batch.retry.policy; |
| 18 | |
| 19 | import java.util.Collections; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.springframework.batch.classify.BinaryExceptionClassifier; |
| 23 | import org.springframework.batch.retry.RetryContext; |
| 24 | import org.springframework.batch.retry.RetryPolicy; |
| 25 | import org.springframework.batch.retry.context.RetryContextSupport; |
| 26 | |
| 27 | /** |
| 28 | * |
| 29 | * Simple retry policy that retries a fixed number of times for a set of named |
| 30 | * exceptions (and subclasses). The number of attempts includes the initial try, |
| 31 | * so e.g. |
| 32 | * |
| 33 | * <pre> |
| 34 | * retryTemplate = new RetryTemplate(new SimpleRetryPolicy(3)); |
| 35 | * retryTemplate.execute(callback); |
| 36 | * </pre> |
| 37 | * |
| 38 | * will execute the callback at least once, and as many as 3 times. |
| 39 | * |
| 40 | * @author Dave Syer |
| 41 | * @author Rob Harrop |
| 42 | * |
| 43 | */ |
| 44 | public class SimpleRetryPolicy implements RetryPolicy { |
| 45 | |
| 46 | /** |
| 47 | * The default limit to the number of attempts for a new policy. |
| 48 | */ |
| 49 | public final static int DEFAULT_MAX_ATTEMPTS = 3; |
| 50 | |
| 51 | private volatile int maxAttempts; |
| 52 | |
| 53 | private volatile BinaryExceptionClassifier retryableClassifier = new BinaryExceptionClassifier(false); |
| 54 | |
| 55 | /** |
| 56 | * Create a {@link SimpleRetryPolicy} with the default number of retry |
| 57 | * attempts. |
| 58 | */ |
| 59 | public SimpleRetryPolicy() { |
| 60 | this(DEFAULT_MAX_ATTEMPTS, Collections |
| 61 | .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Create a {@link SimpleRetryPolicy} with the specified number of retry |
| 66 | * attempts. |
| 67 | * |
| 68 | * @param maxAttempts |
| 69 | * @param retryableExceptions |
| 70 | */ |
| 71 | public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions) { |
| 72 | super(); |
| 73 | this.maxAttempts = maxAttempts; |
| 74 | this.retryableClassifier = new BinaryExceptionClassifier(retryableExceptions); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param retryableExceptions |
| 79 | */ |
| 80 | public void setRetryableExceptions(Map<Class<? extends Throwable>, Boolean> retryableExceptions) { |
| 81 | this.retryableClassifier = new BinaryExceptionClassifier(retryableExceptions); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Setter for retry attempts. |
| 86 | * |
| 87 | * @param retryAttempts the number of attempts before a retry becomes |
| 88 | * impossible. |
| 89 | */ |
| 90 | public void setMaxAttempts(int retryAttempts) { |
| 91 | this.maxAttempts = retryAttempts; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * The maximum number of retry attempts before failure. |
| 96 | * |
| 97 | * @return the maximum number of attempts |
| 98 | */ |
| 99 | public int getMaxAttempts() { |
| 100 | return maxAttempts; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Test for retryable operation based on the status. |
| 105 | * |
| 106 | * @see org.springframework.batch.retry.RetryPolicy#canRetry(org.springframework.batch.retry.RetryContext) |
| 107 | * |
| 108 | * @return true if the last exception was retryable and the number of |
| 109 | * attempts so far is less than the limit. |
| 110 | */ |
| 111 | public boolean canRetry(RetryContext context) { |
| 112 | Throwable t = context.getLastThrowable(); |
| 113 | return (t == null || retryForException(t)) && context.getRetryCount() < maxAttempts; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @see org.springframework.batch.retry.RetryPolicy#close(RetryContext) |
| 118 | */ |
| 119 | public void close(RetryContext status) { |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Update the status with another attempted retry and the latest exception. |
| 124 | * |
| 125 | * @see RetryPolicy#registerThrowable(RetryContext, Throwable) |
| 126 | */ |
| 127 | public void registerThrowable(RetryContext context, Throwable throwable) { |
| 128 | SimpleRetryContext simpleContext = ((SimpleRetryContext) context); |
| 129 | simpleContext.registerThrowable(throwable); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get a status object that can be used to track the current operation |
| 134 | * according to this policy. Has to be aware of the latest exception and the |
| 135 | * number of attempts. |
| 136 | * |
| 137 | * @see org.springframework.batch.retry.RetryPolicy#open(RetryContext) |
| 138 | */ |
| 139 | public RetryContext open(RetryContext parent) { |
| 140 | return new SimpleRetryContext(parent); |
| 141 | } |
| 142 | |
| 143 | private static class SimpleRetryContext extends RetryContextSupport { |
| 144 | public SimpleRetryContext(RetryContext parent) { |
| 145 | super(parent); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Delegates to an exception classifier. |
| 151 | * |
| 152 | * @param ex |
| 153 | * @return true if this exception or its ancestors have been registered as |
| 154 | * retryable. |
| 155 | */ |
| 156 | private boolean retryForException(Throwable ex) { |
| 157 | return retryableClassifier.classify(ex); |
| 158 | } |
| 159 | } |