@HystrixCommand(fallbackMethod = "hiError") public String hiService(String name) { return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class); }
@RequestMapping("/hi") @HystrixCommand(fallbackMethod = "hiError") public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) { return "hi " + name + " ,i am from port:" + port; }
/*********************************************************************************/ @HystrixCommand(fallbackMethod = "userFailureWithFallback_f_0") String userFailureWithFallback() { throw new UserException(); }
@CacheResult(cacheKeyMethod = "getUserByEmailCacheKey") @HystrixCommand public User getUserByEmail(final String email) { return Iterables.tryFind(storage.values(), new Predicate<User>() { @Override public boolean apply(User input) { return input.getProfile().getEmail().equalsIgnoreCase(email); } }).orNull(); }
@HystrixCommand(fallbackMethod = "fallbackInheritsDefaultIgnoreExceptions") public Object commandWithFallbackInheritsDefaultIgnoreExceptions() throws SpecificException { // isn't ignored, need to trigger fallback throw new SpecificException("from 'commandWithFallbackInheritsDefaultIgnoreExceptions'"); }
@HystrixCommand(fallbackMethod = "fallback") @Override public User getByKey(String sKey, Long lKey) { return new User(sKey, "name: " + lKey); // it should be network call }
@HystrixCommand(observableExecutionMode = ObservableExecutionMode.EAGER) private Observable<User> rxCommandFallback(String id, String name, Throwable throwable) { if (throwable instanceof GetUserException && "getUserRxCommandFallback has failed".equals(throwable.getMessage())) { return Observable.just(new User("def", "def")); } else { throw new IllegalStateException(); } }
@HystrixCommand(threadPoolProperties = { @HystrixProperty(name = "maxQueueSize", value = "321") }) public Object commandOverridesDefaultThreadPoolProperties() { return null; } }
@HystrixCommand public Object commandInheritsDefaultIgnoreExceptions() throws BadRequestException { // this exception will be ignored (wrapped in HystrixBadRequestException) because specified in default ignore exceptions throw new BadRequestException("from 'commandInheritsIgnoreExceptionsFromDefault'"); }
@HystrixCommand(fallbackMethod = "getUserByIdsFallbackWithThrowableParam2") private List<User> getUserByIdsFallbackWithThrowableParam1(List<String> ids, Throwable e) { if (e.getMessage().equals("getUserByIdsFails failed")) { throw new RuntimeException("getUserByIdsFallbackWithThrowableParam1 failed"); } List<User> users = new ArrayList<User>(); for (String id : ids) { users.add(new User(id, "name: " + id)); } return users; }
@HystrixCommand private List<User> getUserByIdsFallbackWithThrowableParam3(List<String> ids, Throwable e) { if (!e.getMessage().equals("getUserByIdsFallbackWithThrowableParam2 failed")) { throw new RuntimeException("getUserByIdsFallbackWithThrowableParam3 failed"); } List<User> users = new ArrayList<User>(); for (String id : ids) { users.add(new User(id, "name: " + id)); } return users; }
/*********************************************************************************/ @HystrixCommand(fallbackMethod = "commandWithFallbackThatFailsByTimeOut_f_0") String commandWithFallbackThatFailsByTimeOut() { throw new UserException(0); }
@HystrixCommand(fallbackMethod = "fallbackOverridesDefaultIgnoreExceptions") public Object commandWithFallbackOverridesDefaultIgnoreExceptions(Class<? extends Throwable> errorType) { // isn't ignored, need to trigger fallback throw new SpecificException(); }
@CacheResult @HystrixCommand public User getUserById(@CacheKey String id) { return storage.get(id); }
@HystrixCommand(fallbackMethod = "firstFallbackCommand") public User getUserSyncFallbackCommand(String id, String name) { validate(id, name); return new User(id, name + id); // it should be network call }
@HystrixCommand(fallbackMethod = "secondFallbackCommand") private User firstFallbackCommand(String id, String name) { validate(id, name); return new User(id, name + id); // it should be network call }
@HystrixCommand(fallbackMethod = "fallbackWithStringReturnType") public User commandWithWrongFallbackReturnType(final String id, final String name) { validate(id, name); return new User(id, name); }
@HystrixCommand(groupKey = "FallbackGroupKey", threadPoolKey = "FallbackThreadPoolKey", commandProperties = { @HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "654") }, threadPoolProperties = { @HystrixProperty(name = "maxQueueSize", value = "321") }) private Object fallbackOverridesDefaultProperties() { return null; } }
@HystrixCommand( ignoreExceptions = {BadRequestException.class, NotFoundException.class}, fallbackMethod = "activateFallback") public void activateUser(String id) throws NotFoundException, ActivationException { validate(id); if (!USERS.containsKey(id)) { throw new NotFoundException("user with id: " + id + " not found"); } // always throw this exception throw new ActivationException("user cannot be activate"); }
@CacheRemove(commandKey = "getUserByEmail") @HystrixCommand public void updateProfile(@CacheKey("profile.email") User user) { storage.get(user.getId()).setProfile(user.getProfile()); }