Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright 2006-2007 the original author or authors.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.springframework.batch.retry.interceptor;
A org.aopalliance.intercept.MethodInterceptor that can be used to automatically retry calls to a method on a service if it fails. The injected org.springframework.batch.retry.RetryOperations is used to control the number of retries. By default it will retry a fixed number of times, according to the defaults in
Hint about transaction boundaries. If you want to retry a failed transaction you need to make sure that the transaction boundary is inside the retry, otherwise the successful attempt will roll back with the whole transaction. If the method being intercepted is also transactional, then use the ordering hints in the advice declarations to ensure that this one is before the transaction interceptor in the advice chain.

Rob Harrop
Dave Syer
public class RetryOperationsInterceptor implements MethodInterceptor {
	public void setRetryOperations(RetryOperations retryTemplate) {
		Assert.notNull(retryTemplate"'retryOperations' cannot be null.");
		this. = retryTemplate;
	public Object invoke(final MethodInvocation invocationthrows Throwable {
			public Object doWithRetry(RetryContext contextthrows Exception {
				 * If we don't copy the invocation carefully it won't keep a
				 * reference to the other interceptors in the chain. We don't
				 * have a choice here but to specialise to
				 * ReflectiveMethodInvocation (but how often would another
				 * implementation come along?).
				if (invocation instanceof ProxyMethodInvocation) {
					try {
						return ((ProxyMethodInvocationinvocation)
					catch (Exception e) {
						throw e;
catch (Error e) {
						throw e;
catch (Throwable e) {
						throw new IllegalStateException(e);
else {
							"MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
New to GrepCode? Check out our FAQ X