Skip to content

Commit

Permalink
ExceptionClassifier Reverse Option
Browse files Browse the repository at this point in the history
Option to not retry by default and retry specific.
  • Loading branch information
garyrussell committed Feb 22, 2022
1 parent b198c5b commit 63c5a2f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ private static ExtendedBinaryExceptionClassifier configureDefaultClassifier() {
return new ExtendedBinaryExceptionClassifier(classified, true);
}

/**
* By default, unmatched types classify as true. Call this method to make the default
* false, and remove types explicitly classified as false. This should be called before
* calling any of the classification modification methods.
* @since 2.8.4
*/
public void defaultFalse() {
this.classifier = new ExtendedBinaryExceptionClassifier(new HashMap<>(), false);
}

/**
* Return the exception classifier.
* @return the classifier.
Expand Down Expand Up @@ -99,18 +109,37 @@ public void setClassifications(Map<Class<? extends Throwable>, Boolean> classifi
* </ul>
* All others will be retried.
* @param exceptionTypes the exception types.
* @see #removeNotRetryableException(Class)
* @see #removeClassification(Class)
* @see #setClassifications(Map, boolean)
*/
@SafeVarargs
@SuppressWarnings("varargs")
public final void addNotRetryableExceptions(Class<? extends Exception>... exceptionTypes) {
add(false, exceptionTypes);
}

/**
* Add exception types that can be retried. Call this after {@link #defaultFalse()} to
* specify those exception types that should be classified as true.
* All others will not be retried.
* @param exceptionTypes the exception types.
* @see #removeClassification(Class)
* @see #setClassifications(Map, boolean)
* @since 2.8.4
*/
@SafeVarargs
@SuppressWarnings("varargs")
public final void addRetryableExceptions(Class<? extends Exception>... exceptionTypes) {
add(true, exceptionTypes);
}

private void add(boolean classified, Class<? extends Exception>... exceptionTypes) {
Assert.notNull(exceptionTypes, "'exceptionTypes' cannot be null");
Assert.noNullElements(exceptionTypes, "'exceptionTypes' cannot contain nulls");
for (Class<? extends Exception> exceptionType : exceptionTypes) {
Assert.isTrue(Exception.class.isAssignableFrom(exceptionType),
() -> "exceptionType " + exceptionType + " must be an Exception");
this.classifier.getClassified().put(exceptionType, false);
this.classifier.getClassified().put(exceptionType, classified);
}
}

Expand All @@ -125,13 +154,38 @@ public final void addNotRetryableExceptions(Class<? extends Exception>... except
* <li>{@link NoSuchMethodException}</li>
* <li>{@link ClassCastException}</li>
* </ul>
* All others will be retried.
* All others will be retried, unless {@link #defaultFalse()} has been called.
* @param exceptionType the exception type.
* @return true if the removal was successful.
* @see #addNotRetryableExceptions(Class...)
* @see #setClassifications(Map, boolean)
* @see #defaultFalse()
* @deprecated in favor of {@link #removeClassification(Class)}
*/
@Deprecated
public boolean removeNotRetryableException(Class<? extends Exception> exceptionType) {
return this.removeClassification(exceptionType);
}

/**
* Remove an exception type from the configured list. By default, the following
* exceptions will not be retried:
* <ul>
* <li>{@link DeserializationException}</li>
* <li>{@link MessageConversionException}</li>
* <li>{@link ConversionException}</li>
* <li>{@link MethodArgumentResolutionException}</li>
* <li>{@link NoSuchMethodException}</li>
* <li>{@link ClassCastException}</li>
* </ul>
* All others will be retried, unless {@link #defaultFalse()} has been called.
* @param exceptionType the exception type.
* @return true if the removal was successful.
* @see #addNotRetryableExceptions(Class...)
* @see #setClassifications(Map, boolean)
* @since 2.8.4
*/
public boolean removeClassification(Class<? extends Exception> exceptionType) {
return this.classifier.getClassified().remove(exceptionType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected DeadLetterPublishingRecoverer.HeaderNames getHeaderNames() {
recoverer.setSkipSameTopicFatalExceptions(false);
this.recovererCustomizer.accept(recoverer);
this.fatalExceptions.forEach(recoverer::addNotRetryableExceptions);
this.nonFatalExceptions.forEach(recoverer::removeNotRetryableException);
this.nonFatalExceptions.forEach(recoverer::removeClassification);
return recoverer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ void noCircularRoutingIfFatal() {
recoverer.addNotRetryableExceptions(IllegalStateException.class);
recoverer.accept(record, new IllegalStateException());
verify(template, never()).send(any(ProducerRecord.class));
recoverer.removeNotRetryableException(IllegalStateException.class);
recoverer.removeClassification(IllegalStateException.class);
recoverer.setFailIfSendResultIsError(false);
recoverer.accept(record, new IllegalStateException());
verify(template).send(any(ProducerRecord.class));
Expand All @@ -580,7 +580,7 @@ void doNotSkipCircularFatalIfSet() {
recoverer.addNotRetryableExceptions(IllegalStateException.class);
recoverer.accept(record, new IllegalStateException());
verify(template, times(2)).send(any(ProducerRecord.class));
recoverer.removeNotRetryableException(IllegalStateException.class);
recoverer.removeClassification(IllegalStateException.class);
recoverer.setFailIfSendResultIsError(false);
recoverer.accept(record, new IllegalStateException());
verify(template, times(3)).send(any(ProducerRecord.class));
Expand Down

3 comments on commit 63c5a2f

@artembilan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM then 😉

@tomazfernandes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too

@garyrussell
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a second commit and issued a PR: spring-projects#2127

Please sign in to comment.