First of all, I am sorry what I am asking is so vague and not provided enough context to make you understand.
Here it is my endpoint in Controller.java
@GetMapping("/endpoint")
public ResponseEntity<?> addDocument(@RequestParam String param1, @RequestParam String param2) {
AddDocumentRequest request = new AddDocumentRequest(param1, param2);
callRequestHandler(request);
log.info("Job Done");
return ResponseEntity.ok(true);
}
And it is the callRequestHandler in Controller.java. It just calls handler and handle the exceptions.
private void callRequestHandler(AddDocumentRequest request) {
try {
requestHandler.requestHandler(request);
} catch (Exception e) {
log.error("error");
}
}
It is the method of RequestHandler.java and it acts as @Service. It calls the Async method which send requests to other APIs.
public void requestHandler(AddDocumentRequest request) throws Exception {
//calls @Async method.
CompletableFuture<String> registry = service.addDocuments(request);
//blocks the thread and wait for the desire state.
blockAndWaitResponses(request);
}
And here it is the infinite loop
waiting to meet the desire state, if it meets the state it inserts the data and break the infinite loop, else update state and sleep for 2mins.
This infinite loop
takes a time up to several hours and in this specific situation, it takes 15 mins.
I am aware of the busy waiting is bad design, but there is no better solution for my business situation.
private void blockAndWaitResponses(AddDocumentRequest request) throws Exception {
while (true) {
//busy wait
Thread.sleep(2mins);
if (desiredStateOccured()) {
insertDataToDB();
break;
}
else {
updateState();
}
}
}
And after all of that, back to controller, log.info("Job Done");
is called but return ResponseEntity.ok(true);
is not called.
All I can say is just it is not called. In my Postman, Sending request...
is not gone.
I have debugged it, nothing is occured. It just looks so normal.
And some requests which goes exact same flow work fine (meaning it returns ResponseEntity.ok(true)
without any problem.) but some does not.
I have got no idea how to deal with it. Any help would be thankful.