| 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.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.springframework.batch.classify.Classifier; |
| 23 | import org.springframework.batch.classify.ClassifierSupport; |
| 24 | import org.springframework.batch.classify.SubclassClassifier; |
| 25 | import org.springframework.batch.retry.RetryContext; |
| 26 | import org.springframework.batch.retry.RetryPolicy; |
| 27 | import org.springframework.batch.retry.context.RetryContextSupport; |
| 28 | import org.springframework.util.Assert; |
| 29 | |
| 30 | /** |
| 31 | * A {@link RetryPolicy} that dynamically adapts to one of a set of injected |
| 32 | * policies according to the value of the latest exception. |
| 33 | * |
| 34 | * @author Dave Syer |
| 35 | * |
| 36 | */ |
| 37 | public class ExceptionClassifierRetryPolicy implements RetryPolicy { |
| 38 | |
| 39 | private Classifier<Throwable, RetryPolicy> exceptionClassifier = new ClassifierSupport<Throwable, RetryPolicy>( |
| 40 | new NeverRetryPolicy()); |
| 41 | |
| 42 | /** |
| 43 | * Setter for policy map used to create a classifier. Either this property |
| 44 | * or the exception classifier directly should be set, but not both. |
| 45 | * |
| 46 | * @param policyMap a map of Throwable class to {@link RetryPolicy} that |
| 47 | * will be used to create a {@link Classifier} to locate a policy. |
| 48 | */ |
| 49 | public void setPolicyMap(Map<Class<? extends Throwable>, RetryPolicy> policyMap) { |
| 50 | SubclassClassifier<Throwable, RetryPolicy> subclassClassifier = new SubclassClassifier<Throwable, RetryPolicy>( |
| 51 | policyMap, (RetryPolicy) new NeverRetryPolicy()); |
| 52 | this.exceptionClassifier = subclassClassifier; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Setter for an exception classifier. The classifier is responsible for |
| 57 | * translating exceptions to concrete retry policies. Either this property |
| 58 | * or the policy map should be used, but not both. |
| 59 | * |
| 60 | * @param exceptionClassifier ExceptionClassifier to use |
| 61 | */ |
| 62 | public void setExceptionClassifier(Classifier<Throwable, RetryPolicy> exceptionClassifier) { |
| 63 | this.exceptionClassifier = exceptionClassifier; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Delegate to the policy currently activated in the context. |
| 68 | * |
| 69 | * @see org.springframework.batch.retry.RetryPolicy#canRetry(org.springframework.batch.retry.RetryContext) |
| 70 | */ |
| 71 | public boolean canRetry(RetryContext context) { |
| 72 | RetryPolicy policy = (RetryPolicy) context; |
| 73 | return policy.canRetry(context); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Delegate to the policy currently activated in the context. |
| 78 | * |
| 79 | * @see org.springframework.batch.retry.RetryPolicy#close(org.springframework.batch.retry.RetryContext) |
| 80 | */ |
| 81 | public void close(RetryContext context) { |
| 82 | RetryPolicy policy = (RetryPolicy) context; |
| 83 | policy.close(context); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create an active context that proxies a retry policy by chosing a target |
| 88 | * from the policy map. |
| 89 | * |
| 90 | * @see org.springframework.batch.retry.RetryPolicy#open(RetryContext) |
| 91 | */ |
| 92 | public RetryContext open(RetryContext parent) { |
| 93 | return new ExceptionClassifierRetryContext(parent, exceptionClassifier).open(parent); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Delegate to the policy currently activated in the context. |
| 98 | * |
| 99 | * @see org.springframework.batch.retry.RetryPolicy#registerThrowable(org.springframework.batch.retry.RetryContext, |
| 100 | * Throwable) |
| 101 | */ |
| 102 | public void registerThrowable(RetryContext context, Throwable throwable) { |
| 103 | RetryPolicy policy = (RetryPolicy) context; |
| 104 | policy.registerThrowable(context, throwable); |
| 105 | ((RetryContextSupport) context).registerThrowable(throwable); |
| 106 | } |
| 107 | |
| 108 | private static class ExceptionClassifierRetryContext extends RetryContextSupport implements RetryPolicy { |
| 109 | |
| 110 | final private Classifier<Throwable, RetryPolicy> exceptionClassifier; |
| 111 | |
| 112 | // Dynamic: depends on the latest exception: |
| 113 | private RetryPolicy policy; |
| 114 | |
| 115 | // Dynamic: depends on the policy: |
| 116 | private RetryContext context; |
| 117 | |
| 118 | final private Map<RetryPolicy, RetryContext> contexts = new HashMap<RetryPolicy, RetryContext>(); |
| 119 | |
| 120 | public ExceptionClassifierRetryContext(RetryContext parent, |
| 121 | Classifier<Throwable, RetryPolicy> exceptionClassifier) { |
| 122 | super(parent); |
| 123 | this.exceptionClassifier = exceptionClassifier; |
| 124 | } |
| 125 | |
| 126 | public boolean canRetry(RetryContext context) { |
| 127 | if (this.context == null) { |
| 128 | // there was no error yet |
| 129 | return true; |
| 130 | } |
| 131 | return policy.canRetry(this.context); |
| 132 | } |
| 133 | |
| 134 | public void close(RetryContext context) { |
| 135 | // Only close those policies that have been used (opened): |
| 136 | for (RetryPolicy policy : contexts.keySet()) { |
| 137 | policy.close(getContext(policy, context.getParent())); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public RetryContext open(RetryContext parent) { |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | public void registerThrowable(RetryContext context, Throwable throwable) { |
| 146 | policy = exceptionClassifier.classify(throwable); |
| 147 | Assert.notNull(policy, "Could not locate policy for exception=[" + throwable + "]."); |
| 148 | this.context = getContext(policy, context.getParent()); |
| 149 | policy.registerThrowable(this.context, throwable); |
| 150 | } |
| 151 | |
| 152 | private RetryContext getContext(RetryPolicy policy, RetryContext parent) { |
| 153 | RetryContext context = contexts.get(policy); |
| 154 | if (context == null) { |
| 155 | context = policy.open(parent); |
| 156 | contexts.put(policy, context); |
| 157 | } |
| 158 | return context; |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | } |