R
- the return typepublic abstract class HystrixObservableCommand<R> extends Object implements HystrixObservable<R>, HystrixInvokableInfo<R>
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractCommand.CachedObservableOriginal<R>
Wraps a source Observable and remembers the original HystrixCommand.
|
protected static class |
AbstractCommand.CachedObservableResponse<R>
Wraps a CachedObservableOriginal as it is being returned from cache.
|
protected static class |
AbstractCommand.ExecutionResult
Immutable holder class for the status of command execution.
|
protected static class |
AbstractCommand.ObservableCommand<R> |
protected static class |
AbstractCommand.TimedOutStatus |
static class |
HystrixObservableCommand.Setter
Fluent interface for arguments to the
HystrixObservableCommand constructor. |
Modifier | Constructor and Description |
---|---|
protected |
HystrixObservableCommand(HystrixCommandGroupKey group)
Construct a
HystrixObservableCommand with defined HystrixCommandGroupKey . |
protected |
HystrixObservableCommand(HystrixObservableCommand.Setter setter)
Construct a
HystrixObservableCommand with defined HystrixObservableCommand.Setter that allows injecting property and strategy overrides and other optional arguments. |
Modifier and Type | Method and Description |
---|---|
protected abstract rx.Observable<R> |
construct()
Implement this method with code to be executed when
observe() or toObservable() are invoked. |
protected RuntimeException |
decomposeException(Exception e)
Take an Exception and determine whether to throw it, its cause or a new HystrixRuntimeException.
|
protected 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
|
protected Exception |
getExceptionFromThrowable(Throwable t) |
List<HystrixEventType> |
getExecutionEvents()
List of HystrixCommandEventType enums representing events that occurred during execution.
|
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.
|
Throwable |
getFailedExecutionException()
Get the Throwable/Exception thrown that caused the failure.
|
protected 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 String |
getLogMessagePrefix() |
HystrixCommandMetrics |
getMetrics()
The
HystrixCommandMetrics associated with this AbstractCommand instance. |
int |
getNumberEmissions()
Number of emissions of the execution of a command.
|
int |
getNumberFallbackEmissions()
Number of emissions of the execution of a fallback.
|
HystrixCommandProperties |
getProperties()
The
HystrixCommandProperties associated with this AbstractCommand instance. |
HystrixThreadPoolKey |
getThreadPoolKey() |
protected void |
handleThreadEnd() |
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 (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 . |
protected void |
recordExecutedCommand()
Record that this command was executed in the HystrixRequestLog.
|
protected void |
recordTotalExecutionTime(long startTime)
Record the duration of execution as response or exception is being returned to the caller.
|
protected rx.Observable<R> |
resumeWithFallback()
If
observe() or toObservable() fails in any way then this method will be invoked to provide an opportunity to return a fallback response. |
protected boolean |
shouldOutputOnNextEvents()
Overridden to true so that all onNext emissions are captured
|
rx.Observable<R> |
toObservable()
Used for asynchronous execution of command with a callback by subscribing to the
Observable . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
observe, toObservable
getCommandGroup, getCommandKey, getCommandRunStartTimeInNanos, getExecutionEvents, getExecutionTimeInMilliseconds, getFailedExecutionException, getMetrics, getNumberEmissions, getNumberFallbackEmissions, getProperties, getThreadPoolKey, isCircuitBreakerOpen, isExecutedInThread, isExecutionComplete, isFailedExecution, isResponseFromCache, isResponseFromFallback, isResponseRejected, isResponseShortCircuited, isResponseTimedOut, isSuccessfulExecution
protected final HystrixCircuitBreaker circuitBreaker
protected final HystrixThreadPool threadPool
protected final HystrixThreadPoolKey threadPoolKey
protected final HystrixCommandProperties properties
protected final HystrixCommandMetrics metrics
protected final HystrixCommandKey commandKey
protected final HystrixCommandGroupKey commandGroup
protected final HystrixEventNotifier eventNotifier
protected final HystrixConcurrencyStrategy concurrencyStrategy
protected final HystrixCommandExecutionHook executionHook
protected final com.netflix.hystrix.AbstractCommand.TryableSemaphore fallbackSemaphoreOverride
protected static final ConcurrentHashMap<String,com.netflix.hystrix.AbstractCommand.TryableSemaphore> fallbackSemaphorePerCircuit
protected final com.netflix.hystrix.AbstractCommand.TryableSemaphore executionSemaphoreOverride
protected static final ConcurrentHashMap<String,com.netflix.hystrix.AbstractCommand.TryableSemaphore> executionSemaphorePerCircuit
protected final AtomicReference<Reference<HystrixTimer.TimerListener>> timeoutTimer
protected AtomicBoolean started
protected volatile long invocationStartTime
protected volatile AbstractCommand.ExecutionResult executionResult
protected final AtomicReference<AbstractCommand.TimedOutStatus> isCommandTimedOut
protected final AtomicBoolean isExecutionComplete
protected final AtomicBoolean isExecutedInThread
protected final AtomicReference<rx.functions.Action0> endCurrentThreadExecutingCommand
protected final HystrixRequestCache requestCache
protected final HystrixRequestLog currentRequestLog
protected HystrixObservableCommand(HystrixCommandGroupKey group)
HystrixObservableCommand
with defined HystrixCommandGroupKey
.
The HystrixCommandKey
will be derived from the implementing class name.
group
- HystrixCommandGroupKey
used to group together multiple HystrixObservableCommand
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 HystrixObservableCommand(HystrixObservableCommand.Setter setter)
HystrixObservableCommand
with defined HystrixObservableCommand.Setter
that allows injecting property and strategy overrides and other optional arguments.
NOTE: The HystrixCommandKey
is used to associate a HystrixObservableCommand
with HystrixCircuitBreaker
, HystrixCommandMetrics
and other objects.
Do not create multiple HystrixObservableCommand
implementations with the same HystrixCommandKey
but different injected default properties as the first instantiated will win.
Properties passed in via HystrixObservableCommand.Setter.andCommandPropertiesDefaults(com.netflix.hystrix.HystrixCommandProperties.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 boolean shouldOutputOnNextEvents()
HystrixRequestLog
, and HystrixEventNotifier
currently. Metrics/Hooks laterprotected String getFallbackMethodName()
protected abstract rx.Observable<R> construct()
observe()
or toObservable()
are invoked.protected rx.Observable<R> resumeWithFallback()
observe()
or toObservable()
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 HystrixObservableCommand
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 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 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 failureIllegalStateException
- 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 failureIllegalStateException
- if invoked more than onceprotected void handleThreadEnd()
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 void recordTotalExecutionTime(long startTime)
protected void recordExecutedCommand()
This can be treated as an async operation as it just adds a references to "this" in the log even if the current command is still executing.
protected RuntimeException decomposeException(Exception e)
This will only throw an HystrixRuntimeException, HystrixBadRequestException or IllegalStateException
e
- initial exceptionprotected 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.
protected boolean isRequestCachingEnabled()
protected 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 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 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 isResponseRejected()
getFallback()
being called.isResponseRejected
in interface HystrixInvokableInfo<R>
public 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 getExecutionTimeInMilliseconds()
getExecutionTimeInMilliseconds
in interface HystrixInvokableInfo<R>
public long getCommandRunStartTimeInNanos()
getCommandRunStartTimeInNanos
in interface HystrixInvokableInfo<R>
Copyright © 2015. All Rights Reserved.