001/*
002 * Copyright 2006-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.step.item;
017
018import org.apache.commons.logging.Log;
019import org.apache.commons.logging.LogFactory;
020import org.springframework.batch.repeat.RepeatContext;
021import org.springframework.batch.repeat.exception.ExceptionHandler;
022import org.springframework.batch.repeat.support.RepeatSynchronizationManager;
023import org.springframework.classify.BinaryExceptionClassifier;
024import org.springframework.retry.RetryCallback;
025import org.springframework.retry.RetryContext;
026import org.springframework.retry.RetryPolicy;
027import org.springframework.retry.listener.RetryListenerSupport;
028
029import java.util.Collection;
030
031/**
032 * An {@link ExceptionHandler} that is aware of the retry context so that it can
033 * distinguish between a fatal exception and one that can be retried. Delegates
034 * the actual exception handling to another {@link ExceptionHandler}.
035 *
036 * @author Dave Syer
037 *
038 */
039public class SimpleRetryExceptionHandler extends RetryListenerSupport implements ExceptionHandler {
040
041        /**
042         * Attribute key, whose existence signals an exhausted retry.
043         */
044        private static final String EXHAUSTED = SimpleRetryExceptionHandler.class.getName() + ".RETRY_EXHAUSTED";
045
046        private static final Log logger = LogFactory.getLog(SimpleRetryExceptionHandler.class);
047
048        final private RetryPolicy retryPolicy;
049
050        final private ExceptionHandler exceptionHandler;
051
052        final private BinaryExceptionClassifier fatalExceptionClassifier;
053
054        /**
055         * Create an exception handler from its mandatory properties.
056         *
057         * @param retryPolicy the retry policy that will be under effect when an
058         * exception is encountered
059         * @param exceptionHandler the delegate to use if an exception actually
060         * needs to be handled
061         * @param fatalExceptionClasses exceptions
062         */
063        public SimpleRetryExceptionHandler(RetryPolicy retryPolicy, ExceptionHandler exceptionHandler, Collection<Class<? extends Throwable>> fatalExceptionClasses) {
064                this.retryPolicy = retryPolicy;
065                this.exceptionHandler = exceptionHandler;
066                this.fatalExceptionClassifier = new BinaryExceptionClassifier(fatalExceptionClasses);
067        }
068
069        /**
070         * Check if the exception is going to be retried, and veto the handling if
071         * it is. If retry is exhausted or the exception is on the fatal list, then
072         * handle using the delegate.
073         *
074         * @see ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext,
075         * java.lang.Throwable)
076         */
077        @Override
078        public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
079                // Only bother to check the delegate exception handler if we know that
080                // retry is exhausted
081                if (fatalExceptionClassifier.classify(throwable) || context.hasAttribute(EXHAUSTED)) {
082                        logger.debug("Handled fatal exception");
083                        exceptionHandler.handleException(context, throwable);
084                }
085                else {
086                        logger.debug("Handled non-fatal exception", throwable);
087                }
088        }
089
090        /**
091         * If retry is exhausted set up some state in the context that can be used
092         * to signal that the exception should be handled.
093         *
094         * @see org.springframework.retry.RetryListener#close(org.springframework.retry.RetryContext,
095         * org.springframework.retry.RetryCallback, java.lang.Throwable)
096         */
097        @Override
098        public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
099                if (!retryPolicy.canRetry(context)) {
100                        if (logger.isDebugEnabled()) {
101                                logger.debug("Marking retry as exhausted: "+context);
102                        }
103                        getRepeatContext().setAttribute(EXHAUSTED, "true");
104                }
105        }
106
107        /**
108         * Get the parent context (the retry is in an inner "chunk" loop and we want
109         * the exception to be handled at the outer "step" level).
110         * @return the {@link RepeatContext} that should hold the exhausted flag.
111         */
112        private RepeatContext getRepeatContext() {
113                RepeatContext context = RepeatSynchronizationManager.getContext();
114                if (context.getParent() != null) {
115                        return context.getParent();
116                }
117                return context;
118        }
119
120}