简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM