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 | package org.springframework.batch.core.step.item; |
17 | |
18 | import java.util.Collection; |
19 | |
20 | import org.apache.commons.logging.LogFactory; |
21 | import org.apache.commons.logging.Log; |
22 | import org.springframework.batch.classify.BinaryExceptionClassifier; |
23 | import org.springframework.batch.repeat.RepeatContext; |
24 | import org.springframework.batch.repeat.exception.ExceptionHandler; |
25 | import org.springframework.batch.repeat.support.RepeatSynchronizationManager; |
26 | import org.springframework.batch.retry.RetryCallback; |
27 | import org.springframework.batch.retry.RetryContext; |
28 | import org.springframework.batch.retry.RetryPolicy; |
29 | import org.springframework.batch.retry.listener.RetryListenerSupport; |
30 | |
31 | /** |
32 | * An {@link ExceptionHandler} that is aware of the retry context so that it can |
33 | * distinguish between a fatal exception and one that can be retried. Delegates |
34 | * the actual exception handling to another {@link ExceptionHandler}. |
35 | * |
36 | * @author Dave Syer |
37 | * |
38 | */ |
39 | public class SimpleRetryExceptionHandler extends RetryListenerSupport implements ExceptionHandler { |
40 | |
41 | /** |
42 | * Attribute key, whose existence signals an exhausted retry. |
43 | */ |
44 | private static final String EXHAUSTED = SimpleRetryExceptionHandler.class.getName() + ".RETRY_EXHAUSTED"; |
45 | |
46 | private static final Log logger = LogFactory.getLog(SimpleRetryExceptionHandler.class); |
47 | |
48 | final private RetryPolicy retryPolicy; |
49 | |
50 | final private ExceptionHandler exceptionHandler; |
51 | |
52 | final private BinaryExceptionClassifier fatalExceptionClassifier; |
53 | |
54 | /** |
55 | * Create an exception handler from its mandatory properties. |
56 | * |
57 | * @param retryPolicy the retry policy that will be under effect when an |
58 | * exception is encountered |
59 | * @param exceptionHandler the delegate to use if an exception actually |
60 | * needs to be handled |
61 | * @param fatalExceptionClasses |
62 | */ |
63 | public SimpleRetryExceptionHandler(RetryPolicy retryPolicy, ExceptionHandler exceptionHandler, Collection<Class<? extends Throwable>> fatalExceptionClasses) { |
64 | this.retryPolicy = retryPolicy; |
65 | this.exceptionHandler = exceptionHandler; |
66 | this.fatalExceptionClassifier = new BinaryExceptionClassifier(); |
67 | fatalExceptionClassifier.setTypes(fatalExceptionClasses); |
68 | } |
69 | |
70 | /** |
71 | * Check if the exception is going to be retried, and veto the handling if |
72 | * it is. If retry is exhausted or the exception is on the fatal list, then |
73 | * handle using the delegate. |
74 | * |
75 | * @see ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, |
76 | * java.lang.Throwable) |
77 | */ |
78 | public void handleException(RepeatContext context, Throwable throwable) throws Throwable { |
79 | // Only bother to check the delegate exception handler if we know that |
80 | // retry is exhausted |
81 | if (fatalExceptionClassifier.classify(throwable) || context.hasAttribute(EXHAUSTED)) { |
82 | logger.debug("Handled fatal exception"); |
83 | exceptionHandler.handleException(context, throwable); |
84 | } |
85 | else { |
86 | logger.debug("Handled non-fatal exception", throwable); |
87 | } |
88 | } |
89 | |
90 | /** |
91 | * If retry is exhausted set up some state in the context that can be used |
92 | * to signal that the exception should be handled. |
93 | * |
94 | * @see org.springframework.batch.retry.RetryListener#close(org.springframework.batch.retry.RetryContext, |
95 | * org.springframework.batch.retry.RetryCallback, java.lang.Throwable) |
96 | */ |
97 | public <T> void close(RetryContext context, RetryCallback<T> callback, Throwable throwable) { |
98 | if (!retryPolicy.canRetry(context)) { |
99 | logger.debug("Marking retry as exhausted: "+context); |
100 | getRepeatContext().setAttribute(EXHAUSTED, "true"); |
101 | } |
102 | } |
103 | |
104 | /** |
105 | * Get the parent context (the retry is in an inner "chunk" loop and we want |
106 | * the exception to be handled at the outer "step" level). |
107 | * @return the {@link RepeatContext} that should hold the exhausted flag. |
108 | */ |
109 | private RepeatContext getRepeatContext() { |
110 | RepeatContext context = RepeatSynchronizationManager.getContext(); |
111 | if (context.getParent() != null) { |
112 | return context.getParent(); |
113 | } |
114 | return context; |
115 | } |
116 | |
117 | } |