Java Magazine, Sept/Oct 2018
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 45 the leading edge the result from the underlying subsystem or API Here the code is very simple I delegate implementation to the existing API that I wrote earlier to get the ticker price see Listing 2 I dont need to worry about setting a time out here because it along with other configuration parameters is set in the configuration object that I use during the initialization of the command The getFallback method is overwritten because I use an alternative result when the run method fails due to time out or due to the underlying service throwing an error In my case the fallback is to use the price from cache If I didnt overwrite the getFallback method then a time out or an error received by the price delegate would be propagated to the calling code The command needs to be instantiated for every request because it is stateful and cannot be reused I instantiate it with a specific configuration which I describe shortly The code in Listing 5 instantiates the Hystrix command Listing 5 Instantiating the Hystrix command @ Service public class HystrixDelegate implements PriceDelegate HystrixCommand Setter config public HystrixDelegate TickerPriceRetrieverService priceDelegateImpl this priceDelegateImpl priceDelegateImpl restTemplate new RestTemplate config HystrixCommand Setter withGroupKey HystrixCommandGroupKey Factory asKey PriceCommand HystrixCommandProperties Setter commandProperties HystrixCommandProperties Setter
You must have JavaScript enabled to view digital editions.