javaspringspring-bootspring-retryrecover

How to specify which @Recover method to use for which @Retryable method?


So, I've a spring boot app, where in my service class, there are multiple methods that I'm retrying using @Retryable on them. I've also created their respective recovery methods using @Recover annotation for them.

Now, a lot of these service methods have the same attributes in method signature; due to which their recover methods end up having the same signature as well.

The problem this is leading to is that the recovery method selection on retry exhaustion is NOT as expected. I'm seeing that the recover method declared under method1 is getting called when method3 fails after all retries.

Is there a way to control the recovery method selection? I've seen the use of RetryTemplate where the RetryCallback and RecoveryCallback are explicitly provided, is that the only way?

Thanks!

Here's what the code looks like:

@Retryable
public boolean method1(String arg1) {
// do something
}

@Recover
public boolean method1Recovery(Exception e, String arg1) {
//do something
}

.
.
.

@Retryable
public boolean method3(String arg1) {
// do something
}

@Recover
public boolean method3Recovery(Exception e, String arg1) {
//do something
}

Solution

  • Another way that would probably work is to define and use different exception types, e.g. Method1ExceptionMethod3Exception.