View Javadoc

1   /*
2    * Copyright 2006-2013 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.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.springframework.batch.repeat.RepeatContext;
23  import org.springframework.batch.repeat.exception.ExceptionHandler;
24  import org.springframework.batch.repeat.support.RepeatSynchronizationManager;
25  import org.springframework.classify.BinaryExceptionClassifier;
26  import org.springframework.retry.RetryCallback;
27  import org.springframework.retry.RetryContext;
28  import org.springframework.retry.RetryPolicy;
29  import org.springframework.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(fatalExceptionClasses);
67  	}
68  
69  	/**
70  	 * Check if the exception is going to be retried, and veto the handling if
71  	 * it is. If retry is exhausted or the exception is on the fatal list, then
72  	 * handle using the delegate.
73  	 *
74  	 * @see ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext,
75  	 * java.lang.Throwable)
76  	 */
77  	@Override
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.retry.RetryListener#close(org.springframework.retry.RetryContext,
95  	 * org.springframework.retry.RetryCallback, java.lang.Throwable)
96  	 */
97  	@Override
98  	public <T> void close(RetryContext context, RetryCallback<T> callback, Throwable throwable) {
99  		if (!retryPolicy.canRetry(context)) {
100 			logger.debug("Marking retry as exhausted: "+context);
101 			getRepeatContext().setAttribute(EXHAUSTED, "true");
102 		}
103 	}
104 
105 	/**
106 	 * Get the parent context (the retry is in an inner "chunk" loop and we want
107 	 * the exception to be handled at the outer "step" level).
108 	 * @return the {@link RepeatContext} that should hold the exhausted flag.
109 	 */
110 	private RepeatContext getRepeatContext() {
111 		RepeatContext context = RepeatSynchronizationManager.getContext();
112 		if (context.getParent() != null) {
113 			return context.getParent();
114 		}
115 		return context;
116 	}
117 
118 }