0

A client request is handled by doGET which calls a third-party API. This third-party API then does not send a response back to doGET but calls my other endpoint to be handled by doPOST. I now need to write a response from doPOST into doGET.

Required workflow is as follows -

  1. The third-party API will callback doPOST
  2. doPOST should be using Callable Task that executor service uses
  3. Then I will have Future returned from executor service submit call as shown above
  4. The doGET API will do a blocking .get() operation on this future
  5. Once the reply from doPOST is received, doGET will return the same response back to the client

To make this happen, I am trying to setup executor and future mechanism as follows -

public HttpServletResponse doGET(Request jettyReq, HttpServletRequest request, HttpServletResponse response) throws ExecutionException {
        ExecutorService executor = Executors.newFixedThreadPool(10);

        // Placeholder for the value which will arrive in the future
        Future<WriteDoGetResponse> future = executor.submit(new WriteDoGetResponse());

        // perform some unrelated actions here

        // check future status that gives me an actual response object
        HttpServletResponse modifiedResponse = future.get();

        // if hashmap has future for request-id
        executor.shutdown();
        return modifiedResponse;
    }

public WriteDoGetResponse doPOST(Request jettyReq, HttpServletRequest request, HttpServletResponse response) {

}

I have the callable task in the other class for now but this is not something I am looking to solve.

static class WritedoGetResponse implements Callable<HttpServletResponse> {
    @Override
    public HttpServletResponse call() throws Exception {
        return null;
    }

But I need help with how do I make my doPOST api callable? I need a mechanism to solve this. Because executor.submit() takes an instance and I can not make doPOST implement callable. Any idea?

a13e
  • 838
  • 2
  • 11
  • 27
  • Seems like you have XY problem. If you need some asynchronous operation why do you use thread synchronization, and in fact why do you creating new additional 10 native thread (their creation is actually really CPU and memory expensive) each time you need only one synchronous operation? What you trying to achieve, do you really understand your task ? P.S. In any case - it is considered that crating new threads in a web application is a bed practice. – Victor Gubin Jul 08 '21 at 18:54
  • @VictorGubin I am still figuring out how an executor can be used for this purpose. what I am trying to achieve is the clientRequest will receive the response written by doPOST. Using callable, my clientRequest will wait till it receives the response from doPOST. – a13e Jul 08 '21 at 19:13
  • I am trying to follow a solution over here - https://stackoverflow.com/a/65966370/6843149 This question seems to have a similar requirement as mine. – a13e Jul 08 '21 at 19:14
  • Ok, I suppose that doPOST it is a method provided by some another another server i.e. you are doing some integration between two different services (orchestration). In this case you'd better use a special library for this propose. For example [netflix feign](https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html). 2. I also can assume you are looking for asynchronous IO (i.e. have such requirements ) https://stackoverflow.com/questions/39802643/java-async-in-servlet-3-0-vs-nio-in-servlet-3-1 – Victor Gubin Jul 08 '21 at 19:24
  • No. doPOST is my API that is called by 3rd party API. This 3rd party API was called by my doGET. Now I need to write response into doGET from doPOST. Yes it is an asynchronous IO. Since I use Jetty 11 server, I am not sure if I can use Netflix fein library. – a13e Jul 08 '21 at 19:31
  • If you can not use faign, try [RestEasy](https://www.baeldung.com/resteasy-client-tutorial) In any case calling you'r own function with HTTP web service over the 3d party, are you sure it is right thing to do ? – Victor Gubin Jul 08 '21 at 19:37

0 Answers0