Hystrix: Latency and Fault Tolerance for Distributed Systems



com.netflix.hystrix
Interface HystrixExecutable<R>

Type Parameters:
R -
All Known Implementing Classes:
HystrixCollapser, HystrixCommand

public interface HystrixExecutable<R>

Common interface for executables (HystrixCommand and HystrixCollapser) so client code can treat them the same and combine in typed collections if desired.


Method Summary
 R execute()
          Used for synchronous execution of command.
 rx.Observable<R> observe()
          Used for asynchronous execution of command with a callback by subscribing to the Observable.
 java.util.concurrent.Future<R> queue()
          Used for asynchronous execution of command.
 

Method Detail

execute

R execute()
Used for synchronous execution of command.

Returns:
R Result of HystrixCommand execution
Throws:
HystrixRuntimeException - if an error occurs and a fallback cannot be retrieved
HystrixBadRequestException - if the HystrixCommand instance considers request arguments to be invalid and needs to throw an error that does not represent a system failure

queue

java.util.concurrent.Future<R> queue()
Used for asynchronous execution of command.

This will queue up the command on the thread pool and return an Future to get the result once it completes.

NOTE: If configured to not run in a separate thread, this will have the same effect as HystrixExecutable.execute() and will block.

We don't throw an exception in that case but just flip to synchronous execution so code doesn't need to change in order to switch a circuit from running a separate thread to the calling thread.

Returns:
Future<R> Result of HystrixCommand execution
Throws:
HystrixRuntimeException - if an error occurs and a fallback cannot be retrieved
HystrixBadRequestException - if the HystrixCommand instance considers request arguments to be invalid and needs to throw an error that does not represent a system failure

observe

rx.Observable<R> observe()
Used for asynchronous execution of command with a callback by subscribing to the Observable.

This eagerly starts execution of the command the same as HystrixExecutable.queue() and HystrixExecutable.execute(). A lazy Observable can be obtained from HystrixCommand.toObservable() or HystrixCollapser.toObservable().

Callback Scheduling

Use HystrixCommand.toObservable(rx.Scheduler) or HystrixCollapser.toObservable(rx.Scheduler) to schedule the callback differently.

See https://github.com/Netflix/RxJava/wiki for more information.

Returns:
Observable<R> that executes and calls back with the result of #run() execution or a fallback from #getFallback() if the command fails for any reason.
Throws:
HystrixRuntimeException - if a fallback does not exist

  • via Observer#onError if a failure occurs
  • or immediately if the command can not be queued (such as short-circuited, thread-pool/semaphore rejected)
HystrixBadRequestException - via Observer#onError if invalid arguments or state were used representing a user failure, not a system failure
java.lang.IllegalStateException - if invoked more than once