Spring boot requestcontextholder with completableFuture.supplyAsync

445 Views Asked by At

I am developing an Api using spring boot. I need to call a dependent service asynchronously. I have an interceptor which uses RequestContextHolder from spring to get the httpservletrequest. But as the calls to dependent service is happening ashnchronously, a new thread is created for executing the dependent call and requestContextHolder.getAttributes returns null for the requests started in new thread. Any suggestions to overcome this?

Code :

CompletableFuture.supplyAsyn(()-> dependentService.getSomething());

Interceptor:

HttpServletRequest request = (ServletRequest) (RequestContextHolder.getAttributes()).getRequest();

Here RequestContextHolder.getAttributes() returns null for the requests happening in new threads.Anyway to hold the requestcontext ?

0

There are 0 best solutions below