Improve parameterisation of exception handler

This commit is contained in:
dsyer
2008-08-23 13:22:35 +00:00
parent c3ed34b3a8
commit 8e498f0d52
21 changed files with 731 additions and 655 deletions

View File

@@ -56,7 +56,7 @@ public class LogOrRethrowExceptionHandler implements ExceptionHandler {
protected final Log logger = LogFactory.getLog(LogOrRethrowExceptionHandler.class);
private ExceptionClassifier<String> exceptionClassifier = new ExceptionClassifierSupport() {
private ExceptionClassifier<String,Throwable> exceptionClassifier = new ExceptionClassifierSupport() {
public String classify(Throwable throwable) {
return RETHROW;
}
@@ -68,7 +68,7 @@ public class LogOrRethrowExceptionHandler implements ExceptionHandler {
*
* @param exceptionClassifier the ExceptionClassifier to use
*/
public void setExceptionClassifier(ExceptionClassifier<String> exceptionClassifier) {
public void setExceptionClassifier(ExceptionClassifier<String,Throwable> exceptionClassifier) {
this.exceptionClassifier = exceptionClassifier;
}
@@ -81,7 +81,7 @@ public class LogOrRethrowExceptionHandler implements ExceptionHandler {
*/
public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
Object key = exceptionClassifier.classify(throwable);
String key = exceptionClassifier.classify(throwable);
if (ERROR.equals(key)) {
logger.error("Exception encountered in batch repeat.", throwable);
} else if (WARN.equals(key)) {

View File

@@ -18,7 +18,6 @@ package org.springframework.batch.repeat.exception;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -26,7 +25,6 @@ import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.context.RepeatContextCounter;
import org.springframework.batch.support.ExceptionClassifier;
import org.springframework.batch.support.ExceptionClassifierSupport;
import org.springframework.util.Assert;
/**
* Implementation of {@link ExceptionHandler} that rethrows when exceptions of a
@@ -41,9 +39,9 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler {
protected final Log logger = LogFactory.getLog(RethrowOnThresholdExceptionHandler.class);
private ExceptionClassifier<String> exceptionClassifier = new ExceptionClassifierSupport();
private ExceptionClassifier<String,Throwable> exceptionClassifier = new ExceptionClassifierSupport();
private Map<Object, Integer> thresholds = new HashMap<Object, Integer>();
private Map<String, Integer> thresholds = new HashMap<String, Integer>();
private boolean useParent = false;
@@ -75,15 +73,7 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler {
*
* @param thresholds the threshold value map.
*/
public void setThresholds(Map<Object, Integer> thresholds) {
for (Entry<Object, Integer> entry : thresholds.entrySet()) {
if (!(entry.getKey() instanceof String)) {
logger.warn("Key in thresholds map is not of type String: " + entry.getKey());
}
Assert.state(entry.getValue() != null, "Threshold value must be of type Integer. "
+ "Try using the value-type attribute if you care configuring this map via xml.");
}
public void setThresholds(Map<String, Integer> thresholds) {
this.thresholds = thresholds;
}
@@ -95,7 +85,7 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler {
*
* @param exceptionClassifier ExceptionClassifier to use
*/
public void setExceptionClassifier(ExceptionClassifier<String> exceptionClassifier) {
public void setExceptionClassifier(ExceptionClassifier<String,Throwable> exceptionClassifier) {
this.exceptionClassifier = exceptionClassifier;
}

View File

@@ -118,7 +118,7 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler {
* @param limit the limit
*/
public void setLimit(final int limit) {
delegate.setThresholds(new HashMap<Object, Integer>() {
delegate.setThresholds(new HashMap<String, Integer>() {
{
put(ExceptionClassifierSupport.DEFAULT, 0);
put(TX_INVALID, limit);

View File

@@ -37,7 +37,7 @@ import org.springframework.util.Assert;
*/
public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy {
private ExceptionClassifier<String> exceptionClassifier = new ExceptionClassifierSupport();
private ExceptionClassifier<String,Throwable> exceptionClassifier = new ExceptionClassifierSupport();
private Map<String, RetryPolicy> policyMap = new HashMap<String, RetryPolicy>();
@@ -64,7 +64,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy
*
* @param exceptionClassifier ExceptionClassifier to use
*/
public void setExceptionClassifier(ExceptionClassifier<String> exceptionClassifier) {
public void setExceptionClassifier(ExceptionClassifier<String,Throwable> exceptionClassifier) {
this.exceptionClassifier = exceptionClassifier;
}
@@ -113,7 +113,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy
private class ExceptionClassifierRetryContext extends RetryContextSupport implements RetryPolicy {
private ExceptionClassifier<String> exceptionClassifier;
private ExceptionClassifier<String,Throwable> exceptionClassifier;
// Dynamic: depends on the latest exception:
RetryPolicy policy;
@@ -126,7 +126,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy
Map<RetryPolicy, RetryContext> contexts = new HashMap<RetryPolicy, RetryContext>();
public ExceptionClassifierRetryContext(RetryContext parent, ExceptionClassifier<String> exceptionClassifier) {
public ExceptionClassifierRetryContext(RetryContext parent, ExceptionClassifier<String,Throwable> exceptionClassifier) {
super(parent);
this.exceptionClassifier = exceptionClassifier;
Object key = exceptionClassifier.getDefault();

View File

@@ -16,6 +16,9 @@
package org.springframework.batch.retry.policy;
import java.util.Collection;
import java.util.HashSet;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.context.RetryContextSupport;
@@ -67,8 +70,13 @@ public class SimpleRetryPolicy extends AbstractStatelessRetryPolicy {
*/
public SimpleRetryPolicy(int maxAttempts) {
super();
setRetryableExceptionClasses(new Class[] { Exception.class });
setFatalExceptionClasses(new Class[] { Error.class });
Collection<Class<? extends Throwable>> classes;
classes = new HashSet<Class<? extends Throwable>>();
classes.add(Exception.class);
setRetryableExceptionClasses(classes);
classes = new HashSet<Class<? extends Throwable>>();
classes.add(Error.class);
setFatalExceptionClasses(classes);
this.maxAttempts = maxAttempts;
}
@@ -103,7 +111,7 @@ public class SimpleRetryPolicy extends AbstractStatelessRetryPolicy {
*
* @param retryableExceptionClasses defaults to {@link Exception}.
*/
public final void setRetryableExceptionClasses(Class<?>[] retryableExceptionClasses) {
public final void setRetryableExceptionClasses(Collection<Class<? extends Throwable>> retryableExceptionClasses) {
retryableClassifier.setExceptionClasses(retryableExceptionClasses);
}
@@ -114,7 +122,7 @@ public class SimpleRetryPolicy extends AbstractStatelessRetryPolicy {
*
* @param fatalExceptionClasses defaults to {@link Exception}.
*/
public final void setFatalExceptionClasses(Class<?>[] fatalExceptionClasses) {
public final void setFatalExceptionClasses(Collection<Class<? extends Throwable>> fatalExceptionClasses) {
fatalClassifier.setExceptionClasses(fatalExceptionClasses);
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.batch.support;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -41,9 +42,9 @@ public class BinaryExceptionClassifier extends ExceptionClassifierSupport {
*
* @param exceptionClasses defaults to {@link Exception}.
*/
public final void setExceptionClasses(Class<?>[] exceptionClasses) {
Map<Class<?>, String> temp = new HashMap<Class<?>, String>();
for (Class<?> exceptionClass : exceptionClasses) {
public final void setExceptionClasses(Collection<Class<? extends Throwable>> exceptionClasses) {
Map<Class<? extends Throwable>, String> temp = new HashMap<Class<? extends Throwable>, String>();
for (Class<? extends Throwable> exceptionClass : exceptionClasses) {
temp.put(exceptionClass, NON_DEFAULT);
}
this.delegate.setTypeMap(temp);
@@ -55,7 +56,7 @@ public class BinaryExceptionClassifier extends ExceptionClassifierSupport {
*
* @param throwable the Throwable to classify
* @return true if it is default classified (i.e. not on the list provided
* in {@link #setExceptionClasses(Class[])}.
* in {@link #setExceptionClasses(Collection)}.
*/
public boolean isDefault(Throwable throwable) {
return classify(throwable).equals(DEFAULT);
@@ -67,7 +68,7 @@ public class BinaryExceptionClassifier extends ExceptionClassifierSupport {
* of the throwable or one of its ancestors is on the exception class list
* the classification is as {@link #NON_DEFAULT}.
*
* @see #setExceptionClasses(Class[])
* @see #setExceptionClasses(Collection)
* @see ExceptionClassifierSupport#classify(Throwable)
*/
public String classify(Throwable throwable) {

View File

@@ -22,24 +22,24 @@ package org.springframework.batch.support;
* @author Dave Syer
*
*/
public interface ExceptionClassifier<T> {
public interface ExceptionClassifier<T,C> {
/**
* Get a default value, normally the same as would be returned by
* {@link #classify(Throwable)} with null argument.
* {@link #classify(Object)} with null argument.
*
* @return the default value.
*/
T getDefault();
/**
* Classify the given exception and return a non-null object. The return
* Classify the given object and return a non-null object. The return
* type depends on the implementation but typically would be a key in a map
* which the client maintains.
*
* @param throwable the input exception. Can be null.
* @param classifiable the input object. Can be null.
* @return an object.
*/
T classify(Throwable throwable);
T classify(C classifiable);
}

View File

@@ -23,7 +23,7 @@ package org.springframework.batch.support;
* @author Dave Syer
*
*/
public class ExceptionClassifierSupport implements ExceptionClassifier<String> {
public class ExceptionClassifierSupport implements ExceptionClassifier<String,Throwable> {
/**
* Default classification key.
@@ -33,7 +33,7 @@ public class ExceptionClassifierSupport implements ExceptionClassifier<String> {
/**
* Always returns the value of {@link #DEFAULT}.
*
* @see org.springframework.batch.support.ExceptionClassifier#classify(java.lang.Throwable)
* @see org.springframework.batch.support.ExceptionClassifier#classify(Object)
*/
public String classify(Throwable throwable) {
return DEFAULT;

View File

@@ -30,7 +30,7 @@ import org.springframework.util.Assert;
*/
public class SubclassExceptionClassifier extends ExceptionClassifierSupport {
private Map<Class<?>, String> classified = new HashMap<Class<?>, String>();
private Map<Class<? extends Throwable>, String> classified = new HashMap<Class<? extends Throwable>, String>();
/**
* Map of Throwable class types to keys for the classifier. Any subclass of
@@ -39,9 +39,9 @@ public class SubclassExceptionClassifier extends ExceptionClassifierSupport {
*
* @param typeMap the typeMap to set
*/
public final void setTypeMap(Map<Class<?>, String> typeMap) {
Map<Class<?>, String> map = new HashMap<Class<?>, String>();
for (Map.Entry<Class<?>, String> entry : typeMap.entrySet()) {
public final void setTypeMap(Map<Class<? extends Throwable>, String> typeMap) {
Map<Class<? extends Throwable>, String> map = new HashMap<Class<? extends Throwable>, String>();
for (Map.Entry<Class<? extends Throwable>, String> entry : typeMap.entrySet()) {
addRetryableExceptionClass(entry.getKey(), entry.getValue(), map);
}
this.classified = map;
@@ -59,15 +59,15 @@ public class SubclassExceptionClassifier extends ExceptionClassifierSupport {
return super.classify(throwable);
}
Class<?> exceptionClass = throwable.getClass();
Class<? extends Throwable> exceptionClass = throwable.getClass();
if (classified.containsKey(exceptionClass)) {
return classified.get(exceptionClass);
}
// check for subclasses
Set<Class<?>> classes = new TreeSet<Class<?>>(new ClassComparator());
Set<Class<? extends Throwable>> classes = new TreeSet<Class<? extends Throwable>>(new ClassComparator());
classes.addAll(classified.keySet());
for (Class<?> cls : classes) {
for (Class<? extends Throwable> cls : classes) {
if (cls.isAssignableFrom(exceptionClass)) {
String value = classified.get(cls);
addRetryableExceptionClass(exceptionClass, value, this.classified);
@@ -78,7 +78,7 @@ public class SubclassExceptionClassifier extends ExceptionClassifierSupport {
return super.classify(throwable);
}
private void addRetryableExceptionClass(Class<?> exceptionClass, String classifiedAs, Map<Class<?>, String> map) {
private void addRetryableExceptionClass(Class<? extends Throwable> exceptionClass, String classifiedAs, Map<Class<? extends Throwable>, String> map) {
Assert.isAssignable(Throwable.class, exceptionClass);
map.put(exceptionClass, classifiedAs);
}