0

In th springboot project, I have to build a link asynchronously. But inside CompletableFuture method, HttpServletRequest's getScheme() & getServerName() methods are returning null.

And few days back it was working fine but since some time it is returning null.

public void printUrl(HttpServletRequest request) {
    //Working Fine here
    System.out.println("URL is - "+request.getScheme()+"//:"+request.getServerName()+"/mylink");
    CompletableFuture<Integer> asyncSendReadyEmail = CompletableFuture.supplyAsync(() -> {
        //Not Working here
        System.out.println("URL is - "+request.getScheme()+"//:"+request.getServerName()+"/mylink");
        return 1;
    });
    System.out.println("Return from this method");
}

Can anyone help me out, what is the reason behind it.

  • 2
    The request isn't available in the new thread (the request is thread bound). Retrieve it outside the `CompletableFuture` then access it from inside. You might need to make the variables `final` to allow access. – M. Deinum Feb 10 '21 at 13:36
  • Thanks for helping out. How do you know the request is thread bound? I want to understand this end to end. Where can I read that? – Priyanka Chaudhary Feb 10 '21 at 15:17
  • 1
    Because that is how a servlet container works. When you handover processing to another thread, the request processing continues and the request (might) have finished when the other thread reaches the point where it needs to access it. – M. Deinum Feb 10 '21 at 15:23

0 Answers0