RESOLVED - issue BATCH-973: Switch RetryPolicy back to Throwable instead of Exception as in 1.x

http://jira.springframework.org/browse/BATCH-973
This commit is contained in:
dsyer
2009-09-24 08:24:47 +00:00
parent ab5ece862a
commit 6ef8865ce2
18 changed files with 64 additions and 43 deletions

View File

@@ -64,6 +64,6 @@ public interface RetryContext extends AttributeAccessor {
* be null if this is the first attempt, but also if the enclosing policy
* decides not to provide it (e.g. because of concerns about memory usage).
*/
Exception getLastThrowable();
Throwable getLastThrowable();
}

View File

@@ -58,6 +58,6 @@ public interface RetryPolicy {
* @param context the current status object.
*
*/
void registerThrowable(RetryContext context, Exception throwable);
void registerThrowable(RetryContext context, Throwable throwable);
}

View File

@@ -55,6 +55,6 @@ public interface RetryState {
* @param exception the exception that caused a retry attempt to fail
* @return true if this exception should cause a rollback
*/
boolean rollbackFor(Exception exception);
boolean rollbackFor(Throwable exception);
}

View File

@@ -26,7 +26,7 @@ public class RetryContextSupport extends AttributeAccessorSupport implements Ret
private int count;
private Exception lastException;
private Throwable lastException;
private RetryContext parent;
@@ -51,7 +51,7 @@ public class RetryContextSupport extends AttributeAccessorSupport implements Ret
return count;
}
public Exception getLastThrowable() {
public Throwable getLastThrowable() {
return lastException;
}
@@ -69,7 +69,7 @@ public class RetryContextSupport extends AttributeAccessorSupport implements Ret
* @param throwable the exception that caused the current retry attempt to
* fail.
*/
public void registerThrowable(Exception throwable) {
public void registerThrowable(Throwable throwable) {
this.lastException = throwable;
if (throwable != null)
count++;

View File

@@ -107,7 +107,7 @@ public class CompositeRetryPolicy implements RetryPolicy {
*
* @see org.springframework.batch.retry.RetryPolicy#close(org.springframework.batch.retry.RetryContext)
*/
public void registerThrowable(RetryContext context, Exception throwable) {
public void registerThrowable(RetryContext context, Throwable throwable) {
RetryContext[] contexts = ((CompositeRetryContext) context).contexts;
RetryPolicy[] policies = ((CompositeRetryContext) context).policies;
for (int i = 0; i < contexts.length; i++) {

View File

@@ -99,9 +99,9 @@ public class ExceptionClassifierRetryPolicy implements RetryPolicy {
* Delegate to the policy currently activated in the context.
*
* @see org.springframework.batch.retry.RetryPolicy#registerThrowable(org.springframework.batch.retry.RetryContext,
* Exception)
* Throwable)
*/
public void registerThrowable(RetryContext context, Exception throwable) {
public void registerThrowable(RetryContext context, Throwable throwable) {
RetryPolicy policy = (RetryPolicy) context;
policy.registerThrowable(context, throwable);
((RetryContextSupport) context).registerThrowable(throwable);
@@ -144,7 +144,7 @@ public class ExceptionClassifierRetryPolicy implements RetryPolicy {
return this;
}
public void registerThrowable(RetryContext context, Exception throwable) {
public void registerThrowable(RetryContext context, Throwable throwable) {
policy = exceptionClassifier.classify(throwable);
Assert.notNull(policy, "Could not locate policy for exception=[" + throwable + "].");
this.context = getContext(policy, context.getParent());

View File

@@ -60,11 +60,11 @@ public class NeverRetryPolicy implements RetryPolicy {
}
/**
* Do nothing.
* Make the throwable available for downstream use through the context.
* @see org.springframework.batch.retry.RetryPolicy#registerThrowable(org.springframework.batch.retry.RetryContext,
* Exception)
* Throwable)
*/
public void registerThrowable(RetryContext context, Exception throwable) {
public void registerThrowable(RetryContext context, Throwable throwable) {
((NeverRetryContext) context).setFinished();
((RetryContextSupport) context).registerThrowable(throwable);
}

View File

@@ -57,7 +57,8 @@ public class SimpleRetryPolicy implements RetryPolicy {
* attempts.
*/
public SimpleRetryPolicy() {
this(DEFAULT_MAX_ATTEMPTS, Collections.<Class<? extends Throwable>, Boolean>singletonMap(Exception.class, true));
this(DEFAULT_MAX_ATTEMPTS, Collections
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true));
}
/**
@@ -105,10 +106,9 @@ public class SimpleRetryPolicy implements RetryPolicy {
/**
* Update the status with another attempted retry and the latest exception.
*
* @see org.springframework.batch.retry.RetryPolicy#registerThrowable(org.springframework.batch.retry.RetryContext,
* Exception)
* @see RetryPolicy#registerThrowable(RetryContext, Throwable)
*/
public void registerThrowable(RetryContext context, Exception throwable) {
public void registerThrowable(RetryContext context, Throwable throwable) {
SimpleRetryContext simpleContext = ((SimpleRetryContext) context);
simpleContext.registerThrowable(throwable);
}

View File

@@ -61,7 +61,7 @@ public class TimeoutRetryPolicy implements RetryPolicy {
return new TimeoutRetryContext(parent, timeout);
}
public void registerThrowable(RetryContext context, Exception throwable) {
public void registerThrowable(RetryContext context, Throwable throwable) {
((RetryContextSupport) context).registerThrowable(throwable);
// otherwise no-op - we only time out, otherwise retry everything...
}

View File

@@ -103,10 +103,10 @@ public class DefaultRetryState implements RetryState {
* (non-Javadoc)
*
* @see
* org.springframework.batch.retry.IRetryState#rollbackFor(java.lang.Exception
* org.springframework.batch.retry.RetryState#rollbackFor(java.lang.Throwable
* )
*/
public boolean rollbackFor(Exception exception) {
public boolean rollbackFor(Throwable exception) {
if (rollbackClassifier == null) {
return true;
}

View File

@@ -23,6 +23,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.repeat.RepeatException;
import org.springframework.batch.retry.ExhaustedRetryException;
import org.springframework.batch.retry.RecoveryCallback;
import org.springframework.batch.retry.RetryCallback;
@@ -236,7 +237,7 @@ public class RetryTemplate implements RetryOperations {
lastException = null;
return retryCallback.doWithRetry(context);
}
catch (Exception e) {
catch (Throwable e) {
lastException = e;
@@ -258,7 +259,7 @@ public class RetryTemplate implements RetryOperations {
logger.debug("Checking for rethrow: count=" + context.getRetryCount());
if (shouldRethrow(retryPolicy, context, state)) {
logger.debug("Rethrow in retry for policy: count=" + context.getRetryCount());
throw e;
throw wrapIfNecessary(e);
}
}
@@ -327,7 +328,7 @@ public class RetryTemplate implements RetryOperations {
* @param context
* @param e
*/
protected void registerThrowable(RetryPolicy retryPolicy, RetryState state, RetryContext context, Exception e) {
protected void registerThrowable(RetryPolicy retryPolicy, RetryState state, RetryContext context, Throwable e) {
if (state != null) {
Object key = state.getKey();
if (context.getRetryCount() > 0 && !retryContextCache.containsKey(key)) {
@@ -415,7 +416,7 @@ public class RetryTemplate implements RetryOperations {
throw new ExhaustedRetryException("Retry exhausted after last attempt with no recovery path", context
.getLastThrowable());
}
throw context.getLastThrowable();
throw wrapIfNecessary(context.getLastThrowable());
}
/**
@@ -462,4 +463,20 @@ public class RetryTemplate implements RetryOperations {
}
}
/**
* Re-throws the original throwable if it is unchecked, wraps checked
* exceptions into {@link RepeatException}.
*/
private static Exception wrapIfNecessary(Throwable throwable) {
if (throwable instanceof Error) {
throw (Error) throwable;
}
else if (throwable instanceof Exception) {
return (Exception) throwable;
}
else {
return new RetryException("Exception in batch process", throwable);
}
}
}