Difference between HystrixCommand and HystrixObservableCommand

一曲冷凌霜 提交于 2019-12-23 08:47:16

问题


I m trying to understand the difference between HystrixCommand and HystrixObservableCommand. The reason i am confused is the HysterixCommand also has a observe() or toObservable() method which emit hot and cold observable respectively. So what was the need to create HystrixObservableCommand. If i will be working completely on non blocking calls which one should i use? why?


回答1:


From the Javadocs:

HystrixCommand

This command is essentially a blocking command but provides an Observable facade if used with observe()

HystrixObservableCommand

This command should be used for a purely non-blocking call pattern. The caller of this command will be subscribed to the Observable returned by the run() method.

The difference is that HystrixCommand by default supports a blocking paradigm, but also provides non-blocking behavior by way of Observables via a facade, whereas HystrixObservableCommand was implemented specifically for a non-blocking setup. I'm not entirely sure why it's split into two implementations, but I would guess that the reason is because originally HystrixCommand did not support non-blocking. It was added about a year or so after the original implementation. Could have just been cleaner to write a purely non-blocking hystrix class.

If you are working with only non-blocking calls, you should likely be using HystrixObservableCommand. Ben Christensen, one of the Hystrix devs, sums it up nicely in this post:

However, if you are wrapping blocking calls, you should just stick with using HystrixCommand as that’s what it’s built for and it defaults to running everything in a separate thread. Using HystrixCommand.observe() will give you the concurrent, async composition you’re looking for.

HystrixObservableCommand is intended for wrapping around async, non-blocking Observables that don’t need extra threads.




回答2:


Additionally to the answer of Nick Defazio, an implementation of HystrixObservableCommand wrap Observables that can emit multiple items, whereas HystrixCommand, will never emit more than one item, even when invoking observe() or .toObservable() which are only wrapping the single value retuned by the run() method.



来源:https://stackoverflow.com/questions/35569560/difference-between-hystrixcommand-and-hystrixobservablecommand

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!