rx java - Setting request context for Hystrix collapsing with RxJava running in Vert.X


Keywords:rx  java 


Question: 

I want to use Hystrix (1.5.12) collapsing with RxJava (1.3.3) running in Vert.X (3.3.3). The problem I am running into is that the Hystrix request context is not set for the thread on which the cache key is requested.

I create a Hystrix request context when a request comes in, and pass this context as a parameter to all classes that create observables.

The class that creates the HystrixObservableCollapser implementation and invokes toObservable() on the created collapser, gets invoked on some Vert.X event loop thread.

The subscription on the observable returned above, may or may not be on the collapser-creating thread. This subscription thread is different from the thread on which the request came in and that created the Hystrix request context. So the Hystrix request context is not set for the subscription thread.

I have read HystrixRequestContext in case of async requests and other issues, and I have already tried a number of RxJava and Hystrix hooks, but none of them seem to work:

  • RxJavaHooks#setOnScheduleAction
  • HystrixConcurrencyStrategy#wrapCallable
  • Wrapping the RxJava scheduler in RxJavaHooks#setOnComputationScheduler (and the other 2 scheduler hooks) with a HystrixContextScheduler.

Things do start to work for the following hooks, when I set the Hystrix request context in the lambda passed to doOnSubscribe(...) of the observable returned by the collapser:

  • RxJavaHooks#setOnScheduleAction
  • Wrapping the RxJava scheduler in RxJavaHooks#setOnComputationScheduler (and the other 2 scheduler hooks).

Is setting the Hystrix request context for the current thread in doOnSubscribe(...) of the observable returned by the collapser, and using 1 of the 2 hooks above, the correct way of setting the Hystrix request context?

If so, which of the hooks is the preferred method?

If not, how to correctly ensure that the Hystrix request context is set for a thread?


1 Answer: 

I am not marking this as the answer, because I am not 100% sure this is the correct way. But this works for me.

I am using the RxJava scheduler hooks, in combination with the doOnSubscribe(...) on the observables returned by the collapsers.