-
Notifications
You must be signed in to change notification settings - Fork 536
Description
Hello Spring Retry team,
This is my first post in this repo.
If not anything else, just wanted to say thanks for this amazing project.
I fully understand this project is at an end, but we do use this project heavily (and we are quite happy with this) on a Spring Boot 3.2 project.
Hence, I hope you can consider this.
Our springboot project interacts with an external third-party REST endpoint, which we have no control over.
The endpoint is fairly stable, meaning quite available, quite resilient.
The endpoint will answer only two things, and only those two.
It answers the string, just the string "WAIT" or just the string "GO", based on its own business logic.
To oversimplify this third-party service, here is the code snippet:
@SpringBootApplication
public class StateApplication {
public static void main(String[] args) {
SpringApplication.run(StateApplication.class, args);
}
}
@RestController
class StateController {
@GetMapping("/getState")
public String getState() throws InterruptedException {
Thread.sleep(300);
if (Math.random() < 0.8) {
return "WAIT";
} else {
return "GO";
}
}
}
Our service needs to call this service, and yes, RETRY until we get a go, to do the next steps.
Here is the code of our service:
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
@Service
public class MyDemoService {
private final RestClient restClient;
public MyDemoService(RestClient.Builder restClientBuilder) {
this.restClient = restClientBuilder
.baseUrl("http://localhost:8070")
.build();
}
@Retryable(retryFor = "the response is WAIT", maxAttempts = "Until the response is GO")
public String getThirdPartyState() {
String retryString = restClient.get()
.uri("/getState")
.retrieve()
.body(String.class);
System.out.println("the state is: " + retryString);
return retryString;
}
}
@RestController
public class MyDemoController {
private final MyDemoService myDemoService;
@Autowired
public MyDemoController(MyDemoService myDemoService) {
this.myDemoService = myDemoService;
}
@GetMapping("/hello")
public String hello() {
String doThisUntilReturnGO = myDemoService.getThirdPartyState();
return getSomethingImportantButItWillWorkOnlyIfGO();
}
}
I would like to reach out, asking if there is a pattern to retry for the response of the third-party API.
We use spring retry in other places of our project, for calling other services that are flaky, where we can retry on a Java exception.
However, for this case, the answer will be a correct HTTP 200, without a Java exception, but we would like to retry on "WAIT" until it is a "GO.
Is there a way to do this with @Retry annotation?
I am hoping for something like this: @Retryable(retryFor = "the response is WAIT", maxAttempts = "Until the response is GO")
Thank you for your help.