In the spring-boot application, I am calling a rest endpoint which results in 200 success status code and the response object is something like this:
{
"id" : "some-uuid",
"status" : "success"
}
Possbile values for the status
object are creating, queued, running, failed, skipped, killed, success
I want to retry the rest-endpoint request when the status
object is either queued
or creating
or running
.
For all other e.g. success
, failed
, skipped
I want the retry to stop and get the result.
I am implementing it with spring-retry.
The api call:
private String makeApiCall(String jobId) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
List<MediaType> acceptType = new ArrayList<>();
acceptType.add(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(acceptType);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
ResponseEntity<String> response = restTemplate.exchange(apiEndpoint, HttpMethod.GET, requestEntity, String.class, params);
ObjectMapper objectMapper = new ObjectMapper();
JobStatus jobStatus = null;
try {
jobStatus = objectMapper.readValue(response.getBody(), JobStatus.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return jobStatus.getStatus();
}
The RetryTemp
public ResponseEntity<String> jobStatusCheck(String jobId) throws JsonProcessingException {
RetryTemplate retry = new RetryTemplate();
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(10000);
retry.setRetryPolicy(new JobStatusRetryPolicy());
retry.setBackOffPolicy(backOffPolicy);
try {
String result = (String) retry.execute(context -> {
// No Idea on how to handle such conditions.
String s = makeApiCall(jobId);
if (s.equalsIgnoreCase("success")) {
return s;
}
return null;
});
return ResponseEntity.ok().body(result);
} catch (Exception e) {
throw new RuntimeException("Error occurred during retry", e);
}
}
The retry policy :
public class JobStatusRetryPolicy extends ExceptionClassifierRetryPolicy {
public JobStatusRetryPolicy() {
final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(3);
this.setExceptionClassifier( new Classifier<Throwable, RetryPolicy>()
{
@Override
public RetryPolicy classify( Throwable classifiable )
{
// Retry only when FailureJobStatusException was thrown
if ( classifiable instanceof FailureJobStatusException)
{
return simpleRetryPolicy;
}
// Do not retry for other exceptions
return new NeverRetryPolicy();
}
} );
}
}
The exception:
public class FailureJobStatusException extends Throwable {
}