R
- the return typepublic abstract class HystrixCommand<R> extends java.lang.Object implements HystrixExecutable<R>, HystrixInvokableInfo<R>, HystrixObservable<R>
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractCommand.CommandState |
protected static class |
AbstractCommand.ThreadState |
protected static class |
AbstractCommand.TimedOutStatus |
static class |
HystrixCommand.Setter
Fluent interface for arguments to the
HystrixCommand constructor. |
Modifier and Type | Field and Description |
---|---|
protected HystrixCircuitBreaker |
circuitBreaker |
protected HystrixCommandGroupKey |
commandGroup |
protected HystrixCommandKey |
commandKey |
protected long |
commandStartTimestamp |
protected java.util.concurrent.atomic.AtomicReference<AbstractCommand.CommandState> |
commandState |
protected HystrixConcurrencyStrategy |
concurrencyStrategy |
protected HystrixRequestLog |
currentRequestLog |
protected rx.functions.Action0 |
endCurrentThreadExecutingCommand |
protected HystrixEventNotifier |
eventNotifier
Plugin implementations
|
protected HystrixCommandExecutionHook |
executionHook |
protected ExecutionResult |
executionResult |
protected ExecutionResult |
executionResultAtTimeOfCancellation |
protected com.netflix.hystrix.AbstractCommand.TryableSemaphore |
executionSemaphoreOverride |
protected static java.util.concurrent.ConcurrentHashMap<java.lang.String,com.netflix.hystrix.AbstractCommand.TryableSemaphore> |
executionSemaphorePerCircuit |
protected com.netflix.hystrix.AbstractCommand.TryableSemaphore |
fallbackSemaphoreOverride |
protected static java.util.concurrent.ConcurrentHashMap<java.lang.String,com.netflix.hystrix.AbstractCommand.TryableSemaphore> |
fallbackSemaphorePerCircuit |
protected java.util.concurrent.atomic.AtomicReference<AbstractCommand.TimedOutStatus> |
isCommandTimedOut |
protected boolean |
isResponseFromCache |
protected HystrixCommandMetrics |
metrics |
protected HystrixCommandProperties |
properties |
protected HystrixRequestCache |
requestCache
Instance of RequestCache logic
|
protected HystrixThreadPool |
threadPool |
protected HystrixThreadPoolKey |
threadPoolKey |
protected java.util.concurrent.atomic.AtomicReference<AbstractCommand.ThreadState> |
threadState |
protected java.util.concurrent.atomic.AtomicReference<java.lang.ref.Reference<com.netflix.hystrix.util.HystrixTimer.TimerListener>> |
timeoutTimer |
Modifier | Constructor and Description |
---|---|
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 . |
protected |
HystrixCommand(HystrixCommandGroupKey group,
int executionIsolationThreadTimeoutInMilliseconds)
Construct a
HystrixCommand with defined HystrixCommandGroupKey and thread timeout |
protected |
HystrixCommand(HystrixCommandGroupKey group,
HystrixThreadPoolKey threadPool)
|
protected |
HystrixCommand(HystrixCommandGroupKey group,
HystrixThreadPoolKey threadPool,
int executionIsolationThreadTimeoutInMilliseconds)
Construct a
HystrixCommand with defined HystrixCommandGroupKey , HystrixThreadPoolKey , and thread timeout. |
Modifier and Type | Method and Description |
---|---|
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() |
long |
getCommandRunStartTimeInNanos()
Time in Nanos when this command instance's run method was called, or -1 if not executed
for e.g., command threw an exception
|
ExecutionResult.EventCounts |
getEventCounts() |
protected java.lang.Exception |
getExceptionFromThrowable(java.lang.Throwable t) |
java.util.List<HystrixEventType> |
getExecutionEvents()
List of HystrixCommandEventType enums representing events that occurred during execution.
|
java.lang.Throwable |
getExecutionException()
Get the Throwable/Exception emitted by this command instance prior to checking the fallback.
|
protected rx.Observable<R> |
getExecutionObservable() |
protected com.netflix.hystrix.AbstractCommand.TryableSemaphore |
getExecutionSemaphore()
Get the TryableSemaphore this HystrixCommand should use for execution if not running in a separate thread.
|
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()
|
protected java.lang.String |
getFallbackMethodName()
Each concrete implementation of AbstractCommand should return the name of the fallback method as a String
This will be used to determine if the fallback "exists" for firing the onFallbackStart/onFallbackError hooks
|
protected rx.Observable<R> |
getFallbackObservable() |
protected com.netflix.hystrix.AbstractCommand.TryableSemaphore |
getFallbackSemaphore()
Get the TryableSemaphore this HystrixCommand should use if a fallback occurs.
|
protected java.lang.String |
getLogMessagePrefix() |
HystrixCommandMetrics |
getMetrics()
The
HystrixCommandMetrics associated with this AbstractCommand instance. |
int |
getNumberCollapsed() |
int |
getNumberEmissions()
Number of emissions of the execution of a command.
|
int |
getNumberFallbackEmissions()
Number of emissions of the execution of a fallback.
|
HystrixCollapserKey |
getOriginatingCollapserKey() |
HystrixCommandProperties |
getProperties()
The
HystrixCommandProperties associated with this AbstractCommand instance. |
java.lang.String |
getPublicCacheKey() |
HystrixThreadPoolKey |
getThreadPoolKey() |
protected void |
handleThreadEnd(com.netflix.hystrix.AbstractCommand<R> _cmd) |
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). |
protected boolean |
isRequestCachingEnabled() |
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 (either via threadpool or semaphore)
|
boolean |
isResponseSemaphoreRejected()
Whether the response received was a fallback as result of being rejected via sempahore
|
boolean |
isResponseShortCircuited()
Whether the response received was a fallback as result of being
short-circuited (meaning
isCircuitBreakerOpen() == true ) and getFallback() being called. |
boolean |
isResponseThreadPoolRejected()
Whether the response received was a fallback as result of being rejected via threadpool
|
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()
|
protected boolean |
shouldOutputOnNextEvents() |
rx.Observable<R> |
toObservable()
Used for asynchronous execution of command with a callback by subscribing to the
Observable . |
observe
getCommandGroup, getCommandKey, getCommandRunStartTimeInNanos, getEventCounts, getExecutionEvents, getExecutionTimeInMilliseconds, getFailedExecutionException, getMetrics, getNumberCollapsed, getNumberEmissions, getNumberFallbackEmissions, getOriginatingCollapserKey, getProperties, getPublicCacheKey, getThreadPoolKey, isCircuitBreakerOpen, isExecutedInThread, isExecutionComplete, isFailedExecution, isResponseFromCache, isResponseFromFallback, isResponseRejected, isResponseSemaphoreRejected, isResponseShortCircuited, isResponseThreadPoolRejected, isResponseTimedOut, isSuccessfulExecution
observe, toObservable
protected final HystrixCircuitBreaker circuitBreaker
protected final HystrixCommandGroupKey commandGroup
protected final HystrixCommandKey commandKey
protected volatile long commandStartTimestamp
protected java.util.concurrent.atomic.AtomicReference<AbstractCommand.CommandState> commandState
protected final HystrixConcurrencyStrategy concurrencyStrategy
protected final HystrixRequestLog currentRequestLog
protected volatile rx.functions.Action0 endCurrentThreadExecutingCommand
protected final HystrixEventNotifier eventNotifier
protected final HystrixCommandExecutionHook executionHook
protected volatile ExecutionResult executionResult
protected volatile ExecutionResult executionResultAtTimeOfCancellation
protected final com.netflix.hystrix.AbstractCommand.TryableSemaphore executionSemaphoreOverride
protected static final java.util.concurrent.ConcurrentHashMap<java.lang.String,com.netflix.hystrix.AbstractCommand.TryableSemaphore> executionSemaphorePerCircuit
protected final com.netflix.hystrix.AbstractCommand.TryableSemaphore fallbackSemaphoreOverride
protected static final java.util.concurrent.ConcurrentHashMap<java.lang.String,com.netflix.hystrix.AbstractCommand.TryableSemaphore> fallbackSemaphorePerCircuit
protected final java.util.concurrent.atomic.AtomicReference<AbstractCommand.TimedOutStatus> isCommandTimedOut
protected volatile boolean isResponseFromCache
protected final HystrixCommandMetrics metrics
protected final HystrixCommandProperties properties
protected final HystrixRequestCache requestCache
protected final HystrixThreadPool threadPool
protected final HystrixThreadPoolKey threadPoolKey
protected java.util.concurrent.atomic.AtomicReference<AbstractCommand.ThreadState> threadState
protected final java.util.concurrent.atomic.AtomicReference<java.lang.ref.Reference<com.netflix.hystrix.util.HystrixTimer.TimerListener>> timeoutTimer
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 argumentsprotected 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 interact with,
common business purpose etc.
protected HystrixCommand(HystrixCommandGroupKey group, int executionIsolationThreadTimeoutInMilliseconds)
HystrixCommand
with defined HystrixCommandGroupKey
and thread timeout
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 interact with,
common business purpose etc.
executionIsolationThreadTimeoutInMilliseconds
- Time in milliseconds at which point the calling thread will timeout (using Future.get()
) and walk away from the executing thread.protected HystrixCommand(HystrixCommandGroupKey group, HystrixThreadPoolKey threadPool)
HystrixCommand
with defined HystrixCommandGroupKey
and HystrixThreadPoolKey
.
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 interact with,
common business purpose etc.
threadPool
- HystrixThreadPoolKey
used to identify the thread pool in which a HystrixCommand
executes.protected HystrixCommand(HystrixCommandGroupKey group, HystrixThreadPoolKey threadPool, int executionIsolationThreadTimeoutInMilliseconds)
HystrixCommand
with defined HystrixCommandGroupKey
, HystrixThreadPoolKey
, and thread timeout.
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 interact with,
common business purpose etc.
threadPool
- HystrixThreadPool
used to identify the thread pool in which a HystrixCommand
executes.executionIsolationThreadTimeoutInMilliseconds
- Time in milliseconds at which point the calling thread will timeout (using Future.get()
) and walk away from the executing thread.protected abstract R run() throws java.lang.Exception
java.lang.Exception
- if command execution failsprotected R getFallback()
execute()
or 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.
protected final rx.Observable<R> getExecutionObservable()
protected final rx.Observable<R> getFallbackObservable()
public R execute()
execute
in interface HystrixExecutable<R>
run()
execution or a fallback from getFallback()
if the command fails for any reason.HystrixRuntimeException
- if a failure occurs and a fallback cannot be retrievedHystrixBadRequestException
- if invalid arguments or state were used representing a user failure, not a system failurejava.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 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 run()
execution or a fallback from 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 failurejava.lang.IllegalStateException
- if invoked more than onceprotected java.lang.String getFallbackMethodName()
public rx.Observable<R> observe()
Observable
.
This eagerly starts execution of the command the same as queue()
and execute()
.
A lazy Observable
can be obtained from toObservable()
.
See https://github.com/Netflix/RxJava/wiki for more information.
observe
in interface HystrixObservable<R>
Observable<R>
that executes and calls back with the result of command execution or a fallback 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 failurejava.lang.IllegalStateException
- if invoked more than oncepublic rx.Observable<R> toObservable()
Observable
.
This lazily starts execution of the command once the Observable
is subscribed to.
An eager Observable
can be obtained from observe()
.
See https://github.com/ReactiveX/RxJava/wiki for more information.
toObservable
in interface HystrixObservable<R>
Observable<R>
that executes and calls back with the result of command execution or a fallback 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 failurejava.lang.IllegalStateException
- if invoked more than onceprotected void handleThreadEnd(com.netflix.hystrix.AbstractCommand<R> _cmd)
protected boolean shouldOutputOnNextEvents()
HystrixRequestLog
, and HystrixEventNotifier
currently.protected com.netflix.hystrix.AbstractCommand.TryableSemaphore getFallbackSemaphore()
protected com.netflix.hystrix.AbstractCommand.TryableSemaphore getExecutionSemaphore()
public HystrixCommandGroupKey getCommandGroup()
getCommandGroup
in interface HystrixInvokableInfo<R>
HystrixCommandGroupKey
used to group together multiple AbstractCommand
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()
getCommandKey
in interface HystrixInvokableInfo<R>
HystrixCommandKey
identifying this command instance for statistics, circuit-breaker, properties, etc.public HystrixThreadPoolKey getThreadPoolKey()
getThreadPoolKey
in interface HystrixInvokableInfo<R>
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 AbstractCommand
instance.getMetrics
in interface HystrixInvokableInfo<R>
public HystrixCommandProperties getProperties()
HystrixCommandProperties
associated with this AbstractCommand
instance.getProperties
in interface HystrixInvokableInfo<R>
protected java.lang.RuntimeException decomposeException(java.lang.Exception e)
This will only throw an HystrixRuntimeException, HystrixBadRequestException or IllegalStateException
e
- initial exceptionprotected 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.
public java.lang.String getPublicCacheKey()
getPublicCacheKey
in interface HystrixInvokableInfo<R>
protected boolean isRequestCachingEnabled()
protected java.lang.String getLogMessagePrefix()
public boolean isCircuitBreakerOpen()
execute()
will immediately return
the getFallback()
response and not attempt a HystrixCommand execution.
4 columns are ForcedOpen | ForcedClosed | CircuitBreaker open due to health ||| Expected Result
T | T | T ||| OPEN (true)
T | T | F ||| OPEN (true)
T | F | T ||| OPEN (true)
T | F | F ||| OPEN (true)
F | T | T ||| CLOSED (false)
F | T | F ||| CLOSED (false)
F | F | T ||| OPEN (true)
F | F | F ||| CLOSED (false)isCircuitBreakerOpen
in interface HystrixInvokableInfo<R>
public boolean isExecutionComplete()
isExecutionComplete
in interface HystrixInvokableInfo<R>
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.
isExecutedInThread
in interface HystrixInvokableInfo<R>
public boolean isSuccessfulExecution()
run()
or from cache.isSuccessfulExecution
in interface HystrixInvokableInfo<R>
public boolean isFailedExecution()
run()
resulted in a failure (exception).isFailedExecution
in interface HystrixInvokableInfo<R>
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.
getFailedExecutionException
in interface HystrixInvokableInfo<R>
public java.lang.Throwable getExecutionException()
getFailedExecutionException()
.
2) timeout
3) short-circuit
4) rejection
5) bad request
If the command execution was successful, then this exception instance is null (there was no exception)
Note that the caller of the command may not receive this exception, as fallbacks may be served as a response to
the exception.public boolean isResponseFromFallback()
getFallback()
being called.isResponseFromFallback
in interface HystrixInvokableInfo<R>
public boolean isResponseTimedOut()
getFallback()
being called.isResponseTimedOut
in interface HystrixInvokableInfo<R>
public boolean isResponseShortCircuited()
isCircuitBreakerOpen() == true
) and getFallback()
being called.isResponseShortCircuited
in interface HystrixInvokableInfo<R>
public boolean isResponseFromCache()
run()
was not invoked.isResponseFromCache
in interface HystrixInvokableInfo<R>
public boolean isResponseSemaphoreRejected()
isResponseSemaphoreRejected
in interface HystrixInvokableInfo<R>
public boolean isResponseThreadPoolRejected()
isResponseThreadPoolRejected
in interface HystrixInvokableInfo<R>
public boolean isResponseRejected()
isResponseRejected
in interface HystrixInvokableInfo<R>
public java.util.List<HystrixEventType> getExecutionEvents()
Examples of events are SUCCESS, FAILURE, TIMEOUT, and SHORT_CIRCUITED
getExecutionEvents
in interface HystrixInvokableInfo<R>
List<HystrixEventType>
public int getNumberEmissions()
getNumberEmissions
in interface HystrixInvokableInfo<R>
OnNext
emissions by a streaming commandpublic int getNumberFallbackEmissions()
getNumberFallbackEmissions
in interface HystrixInvokableInfo<R>
OnNext
emissions by a streaming fallbackpublic int getNumberCollapsed()
getNumberCollapsed
in interface HystrixInvokableInfo<R>
public HystrixCollapserKey getOriginatingCollapserKey()
getOriginatingCollapserKey
in interface HystrixInvokableInfo<R>
public int getExecutionTimeInMilliseconds()
getExecutionTimeInMilliseconds
in interface HystrixInvokableInfo<R>
public long getCommandRunStartTimeInNanos()
getCommandRunStartTimeInNanos
in interface HystrixInvokableInfo<R>
public ExecutionResult.EventCounts getEventCounts()
getEventCounts
in interface HystrixInvokableInfo<R>
protected java.lang.Exception getExceptionFromThrowable(java.lang.Throwable t)