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