greekgeek
greekgeek

Reputation: 21

Spring Retry using .retryOn and .notRetryOn

I am trying to execute the spring retry functionality in a certain code base using the imperative style. I want to retry only on RuntimeException, but not on NullPointerException. NullPointerException is a subclass of RuntimeException, hence I will be forced to create a custom retry policy.

By default, we can only use either whitelist or blacklist policies but not both together in spring retry. (We can use either .retryOn or .notRetryOn, but not both together). How do I go about this problem.

My implementation so far has been this:

RetryTemplate template = RetryTemplate.builder()
                         .notRetryOn(IOException.class)
                         .notRetryOn(NullPointerException.class)
                         .traversingCauses()
                         .build();

I am trying to exclude as many other Exceptions like IOException and so on till I arrive on RuntimeException, after which I exclude NullPointerException as well. However, this does not seem full-proof since there might be other exhaustive exceptions as well. Is there a way to use both .retryOn and .notRetryOn together to arrive at specific exception list? Or maybe a way to build this custom retry feature?

TIA!

Upvotes: 2

Views: 1310

Answers (1)

Ashfaq
Ashfaq

Reputation: 29

Your right we can use include or exclude not both at once else we get this Caused by: java.lang.IllegalArgumentException: Please use only retryOn() or only notRetryOn()

Upvotes: 1

Related Questions