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 -
- The third-party API will callback doPOST
- doPOST should be using Callable Task that executor service uses
- Then I will have Future returned from executor service submit call as shown above
- The doGET API will do a blocking
.get()
operation on this future - 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?