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 org.springframework.batch.repeat.RepeatContext; |
19 | import org.springframework.batch.repeat.exception.ExceptionHandler; |
20 | import org.springframework.batch.repeat.support.RepeatSynchronizationManager; |
21 | import org.springframework.batch.retry.RetryCallback; |
22 | import org.springframework.batch.retry.RetryContext; |
23 | import org.springframework.batch.retry.RetryPolicy; |
24 | import org.springframework.batch.retry.listener.RetryListenerSupport; |
25 | import org.springframework.batch.support.BinaryExceptionClassifier; |
26 | |
27 | /** |
28 | * @author Dave Syer |
29 | * |
30 | */ |
31 | public class SimpleRetryExceptionHandler extends RetryListenerSupport implements ExceptionHandler { |
32 | |
33 | /** |
34 | * Attribute key, whose existence signals an exhausted retry. |
35 | */ |
36 | private static final String EXHAUSTED = SimpleRetryExceptionHandler.class.getName() + ".RETRY_EXHAUSTED"; |
37 | |
38 | final private RetryPolicy retryPolicy; |
39 | |
40 | final private ExceptionHandler exceptionHandler; |
41 | |
42 | final private BinaryExceptionClassifier fatalExceptionClassifier; |
43 | |
44 | /** |
45 | * @param retryPolicy |
46 | * @param exceptionHandler |
47 | * @param classes |
48 | */ |
49 | public SimpleRetryExceptionHandler(RetryPolicy retryPolicy, ExceptionHandler exceptionHandler, Class[] classes) { |
50 | this.retryPolicy = retryPolicy; |
51 | this.exceptionHandler = exceptionHandler; |
52 | this.fatalExceptionClassifier = new BinaryExceptionClassifier(); |
53 | fatalExceptionClassifier.setExceptionClasses(classes); |
54 | } |
55 | |
56 | /* |
57 | * (non-Javadoc) |
58 | * @see org.springframework.batch.repeat.exception.handler.ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, |
59 | * java.lang.Throwable) |
60 | */ |
61 | public void handleException(RepeatContext context, Throwable throwable) throws Throwable { |
62 | // Only bother to check the delegate exception handler if we know that |
63 | // retry is exhausted |
64 | if (!fatalExceptionClassifier.isDefault(throwable) || context.hasAttribute(EXHAUSTED)) { |
65 | exceptionHandler.handleException(context, throwable); |
66 | } |
67 | } |
68 | |
69 | /* |
70 | * (non-Javadoc) |
71 | * @see org.springframework.batch.retry.RetryListener#close(org.springframework.batch.retry.RetryContext, |
72 | * org.springframework.batch.retry.RetryCallback, java.lang.Throwable) |
73 | */ |
74 | public void close(RetryContext context, RetryCallback callback, Throwable throwable) { |
75 | if (!retryPolicy.canRetry(context)) { |
76 | getRepeatContext().setAttribute(EXHAUSTED, "true"); |
77 | } |
78 | } |
79 | |
80 | /** |
81 | * Get the parent context (the retry is in an inner "chunk" loop and we want |
82 | * the exception to be handled at the outer "step" level). |
83 | * @return the {@link RepeatContext} that should hold the exhausted flag. |
84 | */ |
85 | private RepeatContext getRepeatContext() { |
86 | RepeatContext context = RepeatSynchronizationManager.getContext(); |
87 | if (context.getParent() != null) { |
88 | return context.getParent(); |
89 | } |
90 | return context; |
91 | } |
92 | |
93 | } |