R
- the return type of real-method that is executed by circuit breakerpublic class LimitedCurrentAverageExecutionTimeStrategy<R> extends Object implements BreakStrategy<R>
BreakStrategy
which causes the circuit breaker to break (not execute the Task) in
case all tasks currently running inside the circuit breaker takes long time on average.
In constructor you define (in milliseconds) how long on average you expect the execution of single Task to take. If on average all currently running tasks take longer than this strategy will decide to break (not to execute another Task).
You may additionally define percentageOfMaxTimesToSkip
which is how many percent of the executions with
highest execution time (longest executions) should not be considered when taking calculating the average.
For example this strategy:
new LimitedCurrentAverageExecutionTimeStrategy(1000, 10);
breaks (not allows execution of target-method) in case 90% of other executions (currently in progress) takes more than one is 1 second (1000 ms). Example figures: there are 20 executions (threads) currently going through circuit breaker. 2 are running for 6 seconds, 18 are running for 900 milliseconds. We skip 10 % of longest executions - this is 2 executions (10% * 20 = 2) and we calculate average from the remaining 18 executions. The average is 900 ms. This is less than allowed maximum = 1000 ms therefore next execution will be allowed.
NOTE: in above example, if the average was above 1000ms than this strategy would return false to circuit breaker
and circuit breaker would execute BreakHandler
to handle this
situation
Modifier and Type | Field and Description |
---|---|
protected float |
maxAllowedExecutionTimeMillis
Protected access to allow extending classes to dynamically change it,
e.g. via MBean
|
protected float |
percentageOfMaxTimesToSkip
Protected access to allow extending classes to dynamically change it,
e.g. via MBean
|
Constructor and Description |
---|
LimitedCurrentAverageExecutionTimeStrategy(long maxAllowedAverageExecutionTimeMillis)
Constructs new strategy with given maximum allowed execution.
|
LimitedCurrentAverageExecutionTimeStrategy(long maxAllowedAverageExecutionTimeMillis,
int percentageOfLongestTimesToSkip)
Constructs new strategy with given maximum allowed execution.
|
Modifier and Type | Method and Description |
---|---|
boolean |
shouldBreak(Task<R> task,
ExecutionContext<R> executionContext)
Returns true if "break" should happen, so that the Task (target-method) does not execute and circuit
breaker executes fallback break handler.
|
protected float maxAllowedExecutionTimeMillis
protected float percentageOfMaxTimesToSkip
public LimitedCurrentAverageExecutionTimeStrategy(long maxAllowedAverageExecutionTimeMillis, int percentageOfLongestTimesToSkip)
maxAllowedAverageExecutionTimeMillis
- max time which does not result in "break" (which allows execution of
target-method)percentageOfLongestTimesToSkip
- how many percent of longest times should not be considered when
calculating the averagepublic LimitedCurrentAverageExecutionTimeStrategy(long maxAllowedAverageExecutionTimeMillis)
maxAllowedAverageExecutionTimeMillis
- max time which does not result in "break" (which allows execution ofpublic boolean shouldBreak(Task<R> task, ExecutionContext<R> executionContext)
BreakStrategy
shouldBreak
in interface BreakStrategy<R>
task
- the task for execution of which this break strategy was calledexecutionContext
- current execution contextCopyright © 2017. All rights reserved.