HTTP context not available for Async call

29 views
Skip to first unread message

Sivakumar Raja

unread,
Aug 9, 2017, 3:50:41 AM8/9/17
to Play Framework
Hi everyone,
    I'm making a web service call. In the case of 200 OK, I need to fetch some additional data based on the response got from the first WS call and return back the final result as JSON, during this process I am making one more web service call which run on the context of currentHttp and it's getting failed. Please suggest me a way to resolve this issue.

   
   
public CompletionStage<Result> handleRequest() {
       
ObjectNode body = Json.newObject();
        body
.put("key", value);

       
return wsClient.url(serviceUri)
               
.setContentType("application/json")
               
.post(body)
               
.thenApply(this::handleWsResponse)
               
.exceptionally(e -> {
                   
Logger.error("Exception", e);
                   
return internalServerError();
               
});
   
}

   
private Result handleWsResponse(WSResponse wsResponse) {
       
if(wsResponse.getStatus() == Http.Status.OK) {
           
try {
               
return processData(wsResponse);
           
} catch (InterruptedException | TimeoutException | ExecutionException e) {
               
Logger.error("Exception e", e);
               
return internalServerError();
           
}
       
} else {
           
return new Result(wsResponse.getStatus());
       
}
   
}

   
private Result processData(WSResponse wsResponse) throws InterruptedException, ExecutionException, TimeoutException {
       
return CompletableFuture.supplyAsync(() -> wsResponse)
               
.thenApply(WSResponse::asJson)
               
.thenComposeAsync(this::fetchResponse, httpExecutionContext.current())
               
.thenApply(Results::ok)
               
.get(2, TimeUnit.SECONDS);   // Error: java.lang.RuntimeException: There is no HTTP Context available from here.
   
}

Thanks,
Sivakumar

Igmar Palsenberg

unread,
Aug 9, 2017, 3:52:41 AM8/9/17
to Play Framework
You need to supply the proper execution context in the async block.



Igmar 

Sivakumar Raja

unread,
Aug 10, 2017, 2:25:33 PM8/10/17
to Play Framework
Igmar, thanks for the suggestion. It works.

Sivakumar
Reply all
Reply to author
Forward
0 new messages