1
1
/*
2
- * Copyright 2021 the original author or authors.
2
+ * Copyright 2021-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -55,6 +55,16 @@ private static ExtendedBinaryExceptionClassifier configureDefaultClassifier() {
55
55
return new ExtendedBinaryExceptionClassifier (classified , true );
56
56
}
57
57
58
+ /**
59
+ * By default, unmatched types classify as true. Call this method to make the default
60
+ * false, and remove types explicitly classified as false. This should be called before
61
+ * calling any of the classification modification methods.
62
+ * @since 2.8.4
63
+ */
64
+ public void defaultFalse () {
65
+ this .classifier = new ExtendedBinaryExceptionClassifier (new HashMap <>(), false );
66
+ }
67
+
58
68
/**
59
69
* Return the exception classifier.
60
70
* @return the classifier.
@@ -97,20 +107,39 @@ public void setClassifications(Map<Class<? extends Throwable>, Boolean> classifi
97
107
* <li>{@link NoSuchMethodException}</li>
98
108
* <li>{@link ClassCastException}</li>
99
109
* </ul>
100
- * All others will be retried.
110
+ * All others will be retried, unless {@link #defaultFalse()} has been called .
101
111
* @param exceptionTypes the exception types.
102
- * @see #removeNotRetryableException (Class)
112
+ * @see #removeClassification (Class)
103
113
* @see #setClassifications(Map, boolean)
104
114
*/
105
115
@ SafeVarargs
106
116
@ SuppressWarnings ("varargs" )
107
117
public final void addNotRetryableExceptions (Class <? extends Exception >... exceptionTypes ) {
118
+ add (false , exceptionTypes );
119
+ }
120
+
121
+ /**
122
+ * Add exception types that can be retried. Call this after {@link #defaultFalse()} to
123
+ * specify those exception types that should be classified as true.
124
+ * All others will be retried, unless {@link #defaultFalse()} has been called.
125
+ * @param exceptionTypes the exception types.
126
+ * @since 2.8.4
127
+ * @see #removeClassification(Class)
128
+ * @see #setClassifications(Map, boolean)
129
+ */
130
+ @ SafeVarargs
131
+ @ SuppressWarnings ("varargs" )
132
+ public final void addRetryableExceptions (Class <? extends Exception >... exceptionTypes ) {
133
+ add (true , exceptionTypes );
134
+ }
135
+
136
+ private void add (boolean classified , Class <? extends Exception >... exceptionTypes ) {
108
137
Assert .notNull (exceptionTypes , "'exceptionTypes' cannot be null" );
109
138
Assert .noNullElements (exceptionTypes , "'exceptionTypes' cannot contain nulls" );
110
139
for (Class <? extends Exception > exceptionType : exceptionTypes ) {
111
140
Assert .isTrue (Exception .class .isAssignableFrom (exceptionType ),
112
141
() -> "exceptionType " + exceptionType + " must be an Exception" );
113
- this .classifier .getClassified ().put (exceptionType , false );
142
+ this .classifier .getClassified ().put (exceptionType , classified );
114
143
}
115
144
}
116
145
@@ -125,13 +154,38 @@ public final void addNotRetryableExceptions(Class<? extends Exception>... except
125
154
* <li>{@link NoSuchMethodException}</li>
126
155
* <li>{@link ClassCastException}</li>
127
156
* </ul>
128
- * All others will be retried.
157
+ * All others will be retried, unless {@link #defaultFalse()} has been called .
129
158
* @param exceptionType the exception type.
130
159
* @return true if the removal was successful.
160
+ * @deprecated in favor of {@link #removeClassification(Class)}
131
161
* @see #addNotRetryableExceptions(Class...)
132
162
* @see #setClassifications(Map, boolean)
163
+ * @see #defaultFalse()
133
164
*/
165
+ @ Deprecated
134
166
public boolean removeNotRetryableException (Class <? extends Exception > exceptionType ) {
167
+ return this .removeClassification (exceptionType );
168
+ }
169
+
170
+ /**
171
+ * Remove an exception type from the configured list. By default, the following
172
+ * exceptions will not be retried:
173
+ * <ul>
174
+ * <li>{@link DeserializationException}</li>
175
+ * <li>{@link MessageConversionException}</li>
176
+ * <li>{@link ConversionException}</li>
177
+ * <li>{@link MethodArgumentResolutionException}</li>
178
+ * <li>{@link NoSuchMethodException}</li>
179
+ * <li>{@link ClassCastException}</li>
180
+ * </ul>
181
+ * All others will be retried, unless {@link #defaultFalse()} has been called.
182
+ * @param exceptionType the exception type.
183
+ * @return true if the removal was successful.
184
+ * @since 2.8.4
185
+ * @see #addNotRetryableExceptions(Class...)
186
+ * @see #setClassifications(Map, boolean)
187
+ */
188
+ public boolean removeClassification (Class <? extends Exception > exceptionType ) {
135
189
return this .classifier .getClassified ().remove (exceptionType );
136
190
}
137
191
0 commit comments