Not directly; you would have to construct a custom interceptor (RetryInterceptorBuilder
) bean and provide its bean name in @Retryable.interceptor
.
Use an ExceptionClassifierRetryPolicy
to use a different policy for each exception.
EDIT
Here's an example:
@SpringBootApplication
@EnableRetry
public class So64029544Application {
public static void main(String[] args) {
SpringApplication.run(So64029544Application.class, args);
}
@Bean
public ApplicationRunner runner(Retryer retryer) {
return args -> {
retryer.toRetry("state");
retryer.toRetry("arg");
};
}
@Bean
public Object retryInterceptor(Retryer retryer) throws Exception {
ExceptionClassifierRetryPolicy policy = new ExceptionClassifierRetryPolicy();
policy.setPolicyMap(Map.of(
IllegalStateException.class, new SimpleRetryPolicy(2),
IllegalArgumentException.class, new SimpleRetryPolicy(3)));
Method recover = retryer.getClass().getDeclaredMethod("recover", Exception.class);
return RetryInterceptorBuilder.stateless()
.retryPolicy(policy)
.backOffOptions(1_000, 1.5, 10_000)
.recoverer(new RecoverAnnotationRecoveryHandler<>(retryer, recover))
.build();
}
}
@Component
class Retryer {
@Retryable(interceptor = "retryInterceptor")
public void toRetry(String in) {
System.out.println(in);
if ("state".equals(in)) {
throw new IllegalStateException();
}
else {
throw new IllegalArgumentException();
}
}
@Recover
public void recover(Exception ex) {
System.out.println("Recovered from " + ex
+ ", retry count:" + RetrySynchronizationManager.getContext().getRetryCount());
}
}
state
state
Recovered from java.lang.IllegalStateException, retry count:2
arg
arg
arg
Recovered from java.lang.IllegalArgumentException, retry count:3