0

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.

user275616
  • 25
  • 5
  • 1
    IIRC, web clients will only wait so long before giving up waiting for an answer. Are the requests that you say work taking shorter times, and the ones that don't work taking longer (long enough that the client will have given up on the response)? – moilejter Jan 03 '23 at 02:00
  • @moilejter I haven't measured them specifically, but it seems so. It tends to be shorter when it works, and longer when it is not. If so, how can I set it to not giving up to response? – user275616 Jan 03 '23 at 02:10
  • It would be better to create a request in your DB and immediately return a request id to the client. Then execute that request asynchronously, and allow the client to poll for the status of the request. – tgdavies Jan 03 '23 at 02:17
  • If your business process need synchronous response why create async request to other api? – Aleson Jan 03 '23 at 04:48
  • You have specified the wait time 2 min for every iteration of checking the desired state but what can be threshold number where max it will wait. The reason I am asking this is http request will not wait for long time and it will go in timeout related issues. If the wait time really high then I would suggest to think of some other approach. – Kunal Varpe Jan 03 '23 at 06:58
  • You should be getting some notification from postman that your request timed out. If that is the case, you can change that timeout as described here: https://stackoverflow.com/questions/36355732/how-to-increase-postman-client-request-timeout – moilejter Jan 04 '23 at 04:03

0 Answers0