This commit is contained in:
dsyer
2007-09-28 09:04:27 +00:00
parent 20a2fb518a
commit c2ea350185
2 changed files with 39 additions and 20 deletions

View File

@@ -28,21 +28,32 @@ import org.springframework.batch.repeat.support.RepeatTemplate;
import org.springframework.util.Assert;
/**
* A {@link MethodInterceptor} that can be used to automatically repeat calls to
* a method on a service. The injected {@link RepeatOperations} is used to
* control the completion of the loop. By default it will repeat until the
* target method returns null. Be careful when injecting a bespoke
* {@link RepeatOperations} that the loop will actually terminate, because the
* default policy for a vanilla {@link RepeatTemplate} will never complete if
* the return type of the target method is void (the value returned is always
* not-null, representing the {@link Void#TYPE}).
*
* @author Dave Syer
* @since 2.1
*/
public class RepeatOperationsInterceptor implements MethodInterceptor {
private RepeatOperations batchTempate = new RepeatTemplate();
private RepeatOperations repeatOperations = new RepeatTemplate();
/**
* Setter for the {@link RepeatOperations}.
*
* @param batchTempate
* @throws IllegalArgumentException if the argument is null.
* @throws IllegalArgumentException
* if the argument is null.
*/
public void setRepeatOperations(RepeatOperations batchTempate) {
Assert.notNull(batchTempate, "'batchTemplate' cannot be null.");
this.batchTempate = batchTempate;
Assert.notNull(batchTempate, "'repeatOperations' cannot be null.");
this.repeatOperations = batchTempate;
}
/**
@@ -53,9 +64,10 @@ public class RepeatOperationsInterceptor implements MethodInterceptor {
*/
public Object invoke(final MethodInvocation invocation) throws Throwable {
batchTempate.iterate(new RepeatCallback() {
repeatOperations.iterate(new RepeatCallback() {
public ExitStatus doInIteration(RepeatContext context) throws Exception {
public ExitStatus doInIteration(RepeatContext context)
throws Exception {
try {
MethodInvocation clone = invocation;
@@ -63,28 +75,29 @@ public class RepeatOperationsInterceptor implements MethodInterceptor {
clone = ((ReflectiveMethodInvocation) invocation)
.invocableClone();
} else {
throw new IllegalStateException("MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
throw new IllegalStateException(
"MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
}
// N.B. discards return value if there is one
if (clone.getMethod().getGenericReturnType().equals(Void.TYPE)) {
if (clone.getMethod().getGenericReturnType().equals(
Void.TYPE)) {
clone.proceed();
return ExitStatus.CONTINUABLE;
}
return new ExitStatus(clone.proceed() != null);
}
catch (Throwable e) {
} catch (Throwable e) {
if (e instanceof Exception) {
throw (Exception) e;
}
else {
throw new RepeatException("Unexpected error in batch interceptor", e);
} else {
throw new RepeatException(
"Unexpected error in batch interceptor", e);
}
}
}
});
return null;
}

View File

@@ -26,22 +26,27 @@ import org.springframework.batch.retry.support.RetryTemplate;
import org.springframework.util.Assert;
/**
* A {@link MethodInterceptor} that can be used to automatically retry calls to
* a method on a service if it fails. The injected {@link RetryOperations} is
* used to control the number of retries. By default it will retry a fixed
* number of times, according to the defaults in {@link RetryTemplate}.
*
* @author Rob Harrop
* @author Dave Syer
* @since 2.1
*/
public class RetryOperationsInterceptor implements MethodInterceptor {
private RetryOperations retryTemplate = new RetryTemplate();
private RetryOperations retryOperations = new RetryTemplate();
public void setRetryTemplate(RetryOperations retryTemplate) {
Assert.notNull(retryTemplate, "'retryTemplate' cannot be null.");
this.retryTemplate = retryTemplate;
Assert.notNull(retryTemplate, "'retryOperations' cannot be null.");
this.retryOperations = retryTemplate;
}
public Object invoke(final MethodInvocation invocation) throws Throwable {
// TODO: use the method name to initialise a statistics context
return this.retryTemplate.execute(new RetryCallback() {
return this.retryOperations.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
@@ -57,7 +62,8 @@ public class RetryOperationsInterceptor implements MethodInterceptor {
clone = ((ReflectiveMethodInvocation) invocation)
.invocableClone();
} else {
throw new IllegalStateException("MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
throw new IllegalStateException(
"MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
}
return clone.proceed();