My customer uses a generated Swagger API Client to use a REST Server from a monolithic application. Unfortunatly the monolith is stuck on Java 1.7.
I did find the FailSafe Framework which would do exactly what we needed. With the exception being that every example code was using lambdas đ So I had to work my way back to use the Java 1.7 syntax.
Before we used FailSafe we had the following call to access the webservice:
final MyResponse response = new SwaggerClientApi().getEntries(owner, null, key, profile);
I start by adding the following dependencies in the pom.xml
:
<dependency>
<groupId>net.jodah</groupId>
<artifactId>failsafe</artifactId>
<version>1.0.5</version>
</dependency>
Next we need to define the Retry Policy to do up to 3 retries with a 500ms pause time when an Exception with the Message „Socket reset“ is thrown.
private static final RetryPolicy retryPolicy = new RetryPolicy()
.retryOn(new Predicate<Throwable>() {
@Override
public boolean test(Throwable throwable) {
if (throwable.getMessage().contains("Connection reset")) {
LOGGER.warn("There was a connection reset error while communication with the userprofile service. Starting a retry.");
return true;
}
return false;
}
})
.withMaxRetries(3)
.withDelay(500, TimeUnit.MILLISECONDS);
}
After that we wrap the Swagger Client API Call in a Callable<MyResponse>
and then start it with FailSafe.
final Callable<MyResponse> callable = new Callable<MyResponse>() {
public MyResponse call() throws Exception {
return new SwaggerClientApi().getEntries(owner, null, key, profile);
}
};
final MyResponse response = Failsafe.with(retryPolicy).get(callable);
And that`s it!