|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.netflix.hystrix.HystrixCommand<R>
R
- the return type@ThreadSafe public abstract class HystrixCommand<R>
Used to wrap code that will execute potentially risky functionality (typically meaning a service call over the network) with fault and latency tolerance, statistics and performance metrics capture, circuit breaker and bulkhead functionality.
Nested Class Summary | |
---|---|
static class |
HystrixCommand.Setter
Fluent interface for arguments to the HystrixCommand constructor. |
Constructor Summary | |
---|---|
protected |
HystrixCommand(HystrixCommand.Setter setter)
Construct a HystrixCommand with defined HystrixCommand.Setter that allows injecting property and strategy overrides and other optional arguments. |
protected |
HystrixCommand(HystrixCommandGroupKey group)
Construct a HystrixCommand with defined HystrixCommandGroupKey . |
Method Summary | |
---|---|
protected java.lang.RuntimeException |
decomposeException(java.lang.Exception e)
Take an Exception and determine whether to throw it, its cause or a new HystrixRuntimeException. |
R |
execute()
Used for synchronous execution of command. |
protected java.lang.String |
getCacheKey()
Key to be used for request caching. |
HystrixCommandGroupKey |
getCommandGroup()
|
HystrixCommandKey |
getCommandKey()
|
java.util.List<HystrixEventType> |
getExecutionEvents()
List of HystrixCommandEventType enums representing events that occurred during execution. |
int |
getExecutionTimeInMilliseconds()
The execution time of this command instance in milliseconds, or -1 if not executed. |
java.lang.Throwable |
getFailedExecutionException()
Get the Throwable/Exception thrown that caused the failure. |
protected R |
getFallback()
If HystrixCommand.execute() or HystrixCommand.queue() fails in any way then this method will be invoked to provide an opportunity to return a fallback response. |
HystrixCommandMetrics |
getMetrics()
The HystrixCommandMetrics associated with this HystrixCommand instance. |
HystrixCommandProperties |
getProperties()
The HystrixCommandProperties associated with this HystrixCommand instance. |
HystrixThreadPoolKey |
getThreadPoolKey()
|
boolean |
isCircuitBreakerOpen()
Whether the 'circuit-breaker' is open meaning that execute() will immediately return
the getFallback() response and not attempt a HystrixCommand execution. |
boolean |
isExecutedInThread()
Whether the execution occurred in a separate thread. |
boolean |
isExecutionComplete()
If this command has completed execution either successfully, via fallback or failure. |
boolean |
isFailedExecution()
Whether the run() resulted in a failure (exception). |
boolean |
isResponseFromCache()
Whether the response is from cache and run() was not invoked. |
boolean |
isResponseFromFallback()
Whether the response received from was the result of some type of failure and getFallback() being called. |
boolean |
isResponseRejected()
Whether the response received was a fallback as result of being rejected (from thread-pool or semaphore) and getFallback() being called. |
boolean |
isResponseShortCircuited()
Whether the response received was a fallback as result of being short-circuited (meaning isCircuitBreakerOpen() == true ) and getFallback() being called. |
boolean |
isResponseTimedOut()
Whether the response received was the result of a timeout and getFallback() being called. |
boolean |
isSuccessfulExecution()
Whether the response was returned successfully either by executing run() or from cache. |
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. |
protected abstract R |
run()
Implement this method with code to be executed when HystrixCommand.execute() or HystrixCommand.queue() are invoked. |
rx.Observable<R> |
toObservable()
A lazy Observable that will execute the command when subscribed to. |
rx.Observable<R> |
toObservable(rx.Scheduler observeOn)
A lazy Observable that will execute the command when subscribed to. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected HystrixCommand(HystrixCommandGroupKey group)
HystrixCommand
with defined HystrixCommandGroupKey
.
The HystrixCommandKey
will be derived from the implementing class name.
group
- HystrixCommandGroupKey
used to group together multiple HystrixCommand
objects.
The HystrixCommandGroupKey
is used to represent a common relationship between commands. For example, a library or team name, the system all related commands interace with,
common business purpose etc.
protected HystrixCommand(HystrixCommand.Setter setter)
HystrixCommand
with defined HystrixCommand.Setter
that allows injecting property and strategy overrides and other optional arguments.
NOTE: The HystrixCommandKey
is used to associate a HystrixCommand
with HystrixCircuitBreaker
, HystrixCommandMetrics
and other objects.
Do not create multiple HystrixCommand
implementations with the same HystrixCommandKey
but different injected default properties as the first instantiated will win.
Properties passed in via HystrixCommand.Setter.andCommandPropertiesDefaults(com.netflix.hystrix.HystrixCommandProperties.Setter)
or HystrixCommand.Setter.andThreadPoolPropertiesDefaults(com.netflix.hystrix.HystrixThreadPoolProperties.Setter)
are cached for the given HystrixCommandKey
for the life of the JVM
or until Hystrix.reset()
is called. Dynamic properties allow runtime changes. Read more on the Hystrix Wiki.
setter
- Fluent interface for constructor argumentsMethod Detail |
---|
protected abstract R run() throws java.lang.Exception
HystrixCommand.execute()
or HystrixCommand.queue()
are invoked.
java.lang.Exception
- if command execution failsprotected R getFallback()
HystrixCommand.execute()
or HystrixCommand.queue()
fails in any way then this method will be invoked to provide an opportunity to return a fallback response.
This should do work that does not require network transport to produce.
In other words, this should be a static or cached result that can immediately be returned upon failure.
If network traffic is wanted for fallback (such as going to MemCache) then the fallback implementation should invoke another HystrixCommand
instance that protects against that network
access and possibly has another level of fallback that does not involve network access.
DEFAULT BEHAVIOR: It throws UnsupportedOperationException.
public HystrixCommandGroupKey getCommandGroup()
HystrixCommandGroupKey
used to group together multiple HystrixCommand
objects.
The HystrixCommandGroupKey
is used to represent a common relationship between commands. For example, a library or team name, the system all related commands interace with,
common business purpose etc.
public HystrixCommandKey getCommandKey()
HystrixCommandKey
identifying this command instance for statistics, circuit-breaker, properties, etc.public HystrixThreadPoolKey getThreadPoolKey()
HystrixThreadPoolKey
identifying which thread-pool this command uses (when configured to run on separate threads via
HystrixCommandProperties.executionIsolationStrategy()
).public HystrixCommandMetrics getMetrics()
HystrixCommandMetrics
associated with this HystrixCommand
instance.
public HystrixCommandProperties getProperties()
HystrixCommandProperties
associated with this HystrixCommand
instance.
public R execute()
execute
in interface HystrixExecutable<R>
HystrixCommand.run()
execution or a fallback from HystrixCommand.getFallback()
if the command fails for any reason.
HystrixRuntimeException
- if a failure occurs and a fallback cannot be retrieved
HystrixBadRequestException
- if invalid arguments or state were used representing a user failure, not a system failure
java.lang.IllegalStateException
- if invoked more than oncepublic java.util.concurrent.Future<R> queue()
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 HystrixCommand.execute()
and will block.
We don't throw an exception but just flip to synchronous execution so code doesn't need to change in order to switch a command from running on a separate thread to the calling thread.
queue
in interface HystrixExecutable<R>
Future<R>
Result of HystrixCommand.run()
execution or a fallback from HystrixCommand.getFallback()
if the command fails for any reason.
HystrixRuntimeException
- if a fallback does not exist
Future.get()
in Throwable.getCause()
if a failure occursHystrixBadRequestException
- via Future.get()
in Throwable.getCause()
if invalid arguments or state were used representing a user failure, not a system failure
java.lang.IllegalStateException
- if invoked more than onceprotected java.lang.RuntimeException decomposeException(java.lang.Exception e)
This will only throw an HystrixRuntimeException, HystrixBadRequestException or IllegalStateException
e
-
public rx.Observable<R> observe()
Observable
.
This eagerly starts execution of the command the same as HystrixCommand.queue()
and HystrixCommand.execute()
.
A lazy Observable
can be obtained from HystrixCommand.toObservable()
.
Callback Scheduling
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD
this defaults to using Schedulers.threadPoolForComputation()
for callbacks.HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE
this defaults to using Schedulers.immediate()
for callbacks.HystrixCommand.toObservable(rx.Scheduler)
to schedule the callback differently.
See https://github.com/Netflix/RxJava/wiki for more information.
observe
in interface HystrixExecutable<R>
Observable<R>
that executes and calls back with the result of HystrixCommand.run()
execution or a fallback from HystrixCommand.getFallback()
if the command fails for any reason.
HystrixRuntimeException
- if a fallback does not exist
Observer#onError
if a failure occursHystrixBadRequestException
- 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 oncepublic rx.Observable<R> toObservable()
Observable
that will execute the command when subscribed to.
Callback Scheduling
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD
this defaults to using Schedulers.threadPoolForComputation()
for callbacks.HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE
this defaults to using Schedulers.immediate()
for callbacks.See https://github.com/Netflix/RxJava/wiki for more information.
Observable<R>
that lazily executes and calls back with the result of HystrixCommand.run()
execution or a fallback from HystrixCommand.getFallback()
if the command fails for any reason.
HystrixRuntimeException
- if a fallback does not exist
Observer#onError
if a failure occursHystrixBadRequestException
- 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 oncepublic rx.Observable<R> toObservable(rx.Scheduler observeOn)
Observable
that will execute the command when subscribed to.
See https://github.com/Netflix/RxJava/wiki for more information.
observeOn
- The Scheduler
to execute callbacks on.
Observable<R>
that lazily executes and calls back with the result of HystrixCommand.run()
execution or a fallback from HystrixCommand.getFallback()
if the command fails for any reason.
HystrixRuntimeException
- if a fallback does not exist
Observer#onError
if a failure occursHystrixBadRequestException
- 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 oncepublic boolean isCircuitBreakerOpen()
execute()
will immediately return
the getFallback()
response and not attempt a HystrixCommand execution.
public boolean isExecutionComplete()
public boolean isExecutedInThread()
This should be called only once execute()/queue()/fireOrForget() are called otherwise it will always return false.
This specifies if a thread execution actually occurred, not just if it is configured to be executed in a thread.
public boolean isSuccessfulExecution()
run()
or from cache.
public boolean isFailedExecution()
run()
resulted in a failure (exception).
public java.lang.Throwable getFailedExecutionException()
If isFailedExecution() == true
then this would represent the Exception thrown by the run()
method.
If isFailedExecution() == false
then this would return null.
public boolean isResponseFromFallback()
getFallback()
being called.
public boolean isResponseTimedOut()
getFallback()
being called.
public boolean isResponseShortCircuited()
isCircuitBreakerOpen() == true
) and getFallback()
being called.
public boolean isResponseFromCache()
run()
was not invoked.
public boolean isResponseRejected()
getFallback()
being called.
public java.util.List<HystrixEventType> getExecutionEvents()
Examples of events are SUCCESS, FAILURE, TIMEOUT, and SHORT_CIRCUITED
List<HystrixEventType>
public int getExecutionTimeInMilliseconds()
protected java.lang.String getCacheKey()
By default this returns null which means "do not cache".
To enable caching override this method and return a string key uniquely representing the state of a command instance.
If multiple command instances in the same request scope match keys then only the first will be executed and all others returned from cache.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |