How to make request header available in rxJava threads? Injecting HttpServletRequest in a new thread does not work

438 Views Asked by At

I am working on a Spring boot application and need to read request header in a RxJava thread, but injecting HttpServletRequest doesn't work because of a new thread. Passing header from current thread to new thread is unwanted.

code flow: Controller -> Service -> for each order -> (this code spawns new threads) Observable.just(invokeOrderService(orderNumber)) -> fetch request header something like httpRequest.getHeader("isApp")

I am completely clueless about approach. All I need is header info to be available in the newly created threads.

1

There are 1 best solutions below

0
zlaval On

First of all, I suggest to use spring reactive web, your life will be much more easier. But if you must use plain spring web with rxjava, you must know that requests are held in threadlocals, so you can only reach it in the same thread which handle the request. But if you want, you are able to pass properties to rxjava in the form of context propagation. You can find more information here: https://github.com/reactiverse/reactive-contexts