Retry Operation at specific Exception using Spring Retry

Spring-Retry allows for the re-execution of operations when an Exception is thrown. How do we limit the retries to specific exceptions only? For instance, we wouldn’t want to retry a SOAP Web Service operations (@WebMethod) that threw exceptions due to invalid values or internal errors on the remote services. However, we would want to retry those failed operations caused by connectivity or unavailability issues, e.g., no Internet connection, and “server is busy”.

For Starters

Please read the following posts for some basic usage of Spring Retry API.

Using Spring Retry API

More Elegant way to Retry Operations in Java using Spring

Retry Only for Specific Exception

Our RetryTemplate within a Java Configuration.

Our retry policy:

With these, the call to our remote services look something like:

public method

This method is the one being invoked by other codes directly. This is where we implement our “retry” codes.

private method – internal

Tested on

  • Spring Framework 3.0.7.RELEASE
  • Spring Retry 1.0.3.RELEASE
  • Java 8