Class FaultToleranceMethodContextImpl
- java.lang.Object
-
- fish.payara.microprofile.faulttolerance.service.FaultToleranceMethodContextImpl
-
- All Implemented Interfaces:
FaultToleranceMethodContext
public final class FaultToleranceMethodContextImpl extends Object implements FaultToleranceMethodContext
The execution context for a FT annotated method. Each specificMethod
on a specificClass
has a corresponding instance of thisFaultToleranceMethodContext
. Multiple instances of that class share a context (since MP FT 3.0). When the annotatedMethod
is invoked this implementation is bound to that context byboundTo(InvocationContext, FaultTolerancePolicy)
with a fresh instance of this class. It shares all the state with other invocations for the same method except theInvocationContext
and theFaultTolerancePolicy
which are specific for each invocation. This way the full FT invocation state for each method invocation is determined at the beginning of applying FT semantics and cannot change during execution (except for those counters and queues that are meant to track the shared state changes of course).- Author:
- Jan Bernitt
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface fish.payara.microprofile.faulttolerance.FaultToleranceMethodContext
FaultToleranceMethodContext.AsyncFuture
-
-
Constructor Summary
Constructors Constructor Description FaultToleranceMethodContextImpl(jakarta.enterprise.context.control.RequestContextController requestContext, FaultToleranceRequestTracing requestTracing, FaultToleranceMetrics metrics, ExecutorService asyncExecution, ScheduledExecutorService delayedExecution, String appName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FaultToleranceMethodContext
boundTo(jakarta.interceptor.InvocationContext context, FaultTolerancePolicy policy)
void
delay(long delayMillis)
Delays the current thread by the given duration.void
endTrace()
Ends the innermost trace.Object
fallbackHandle(Class<? extends org.eclipse.microprofile.faulttolerance.FallbackHandler<?>> fallbackClass, Throwable ex)
Object
fallbackInvoke(Method fallbackMethod)
Invokes the given fallbackMethod
in the given context.String
getAppName()
BlockingQueue<Thread>
getConcurrentExecutions()
Get or create theBlockingQueue
for bulkhead.FaultToleranceMetrics
getMetrics()
Returns theFaultToleranceMetrics
to use.AtomicInteger
getQueuingOrRunningPopulation()
Get the bulkhead thread count.CircuitBreakerState
getState()
Get or create theCircuitBreakerState
.Object
proceed()
Proceeds execution to the annotated method body.void
runAsynchronous(FaultToleranceMethodContext.AsyncFuture asyncResult, Callable<Object> task)
OBS! Unit tests implement a stub context with a simplified version of this implementation that needs to be updated properly whenever this method is changed in order to have comparable behaviour in tests.Future<?>
runDelayed(long delayMillis, Runnable task)
Runs a given task after a certain waiting time.String
toString()
void
trace(String method)
Starts tracing the given context named with the given method label.
-
-
-
Constructor Detail
-
FaultToleranceMethodContextImpl
public FaultToleranceMethodContextImpl(jakarta.enterprise.context.control.RequestContextController requestContext, FaultToleranceRequestTracing requestTracing, FaultToleranceMetrics metrics, ExecutorService asyncExecution, ScheduledExecutorService delayedExecution, String appName)
-
-
Method Detail
-
getAppName
public String getAppName()
-
boundTo
public FaultToleranceMethodContext boundTo(jakarta.interceptor.InvocationContext context, FaultTolerancePolicy policy)
- Specified by:
boundTo
in interfaceFaultToleranceMethodContext
-
proceed
public Object proceed() throws Exception
Description copied from interface:FaultToleranceMethodContext
Proceeds execution to the annotated method body.- Specified by:
proceed
in interfaceFaultToleranceMethodContext
- Returns:
- result returned by the annotated method
- Throws:
Exception
- in case the annotated method threw anException
.
-
getMetrics
public FaultToleranceMetrics getMetrics()
Description copied from interface:FaultToleranceMethodContext
Returns theFaultToleranceMetrics
to use.- Specified by:
getMetrics
in interfaceFaultToleranceMethodContext
- Returns:
- the
FaultToleranceMetrics
to use,FaultToleranceMetrics.DISABLED
when not enabled.
-
getState
public CircuitBreakerState getState()
Description copied from interface:FaultToleranceMethodContext
Get or create theCircuitBreakerState
.- Specified by:
getState
in interfaceFaultToleranceMethodContext
- Returns:
- the created or existing state, or null if non existed and requestVolumeThreshold was null
-
getConcurrentExecutions
public BlockingQueue<Thread> getConcurrentExecutions()
Description copied from interface:FaultToleranceMethodContext
Get or create theBlockingQueue
for bulkhead.- Specified by:
getConcurrentExecutions
in interfaceFaultToleranceMethodContext
- Returns:
- the created or existing queue, or null if non existed and requestVolumeThreshold was null
-
getQueuingOrRunningPopulation
public AtomicInteger getQueuingOrRunningPopulation()
Description copied from interface:FaultToleranceMethodContext
Get the bulkhead thread count.- Specified by:
getQueuingOrRunningPopulation
in interfaceFaultToleranceMethodContext
- Returns:
- This are number of threads that are either waiting or running in the bulkhead.
-
delay
public void delay(long delayMillis) throws InterruptedException
Description copied from interface:FaultToleranceMethodContext
Delays the current thread by the given duration. The delay is traced.- Specified by:
delay
in interfaceFaultToleranceMethodContext
- Parameters:
delayMillis
- the time to sleep in milliseconds- Throws:
InterruptedException
- In case waiting is interrupted
-
runAsynchronous
public void runAsynchronous(FaultToleranceMethodContext.AsyncFuture asyncResult, Callable<Object> task) throws RejectedExecutionException
OBS! Unit tests implement a stub context with a simplified version of this implementation that needs to be updated properly whenever this method is changed in order to have comparable behaviour in tests.- Specified by:
runAsynchronous
in interfaceFaultToleranceMethodContext
- Parameters:
asyncResult
- a not yet completedCompletableFuture
that should receive the result of the operation when it is executedtask
- an operation that must compute a value of typeFuture
orCompletionStage
.- Throws:
RejectedExecutionException
- In case the task could not be accepted for execution. Usually due to too many work in progress.
-
runDelayed
public Future<?> runDelayed(long delayMillis, Runnable task) throws Exception
Description copied from interface:FaultToleranceMethodContext
Runs a given task after a certain waiting time.- Specified by:
runDelayed
in interfaceFaultToleranceMethodContext
- Parameters:
delayMillis
- time to wait in milliseconds before running the given tasktask
- operation to run- Returns:
- A future that can be cancelled if the operation should no longer be run
- Throws:
Exception
-
fallbackHandle
public Object fallbackHandle(Class<? extends org.eclipse.microprofile.faulttolerance.FallbackHandler<?>> fallbackClass, Throwable ex) throws Exception
Description copied from interface:FaultToleranceMethodContext
Invokes the instance of the givenFallbackHandler
Class
defined in the given context to handle the givenException
.- Specified by:
fallbackHandle
in interfaceFaultToleranceMethodContext
- Parameters:
fallbackClass
- the type ofFallbackHandler
to resolve or instantiate and useex
- theException
thrown by the FT processing to handle by theFallbackHandler
- Returns:
- the result returned by the invoked
FallbackHandler
- Throws:
Exception
- in case resolving, instantiating or invoking the handler method fails
-
fallbackInvoke
public Object fallbackInvoke(Method fallbackMethod) throws Exception
Description copied from interface:FaultToleranceMethodContext
Invokes the given fallbackMethod
in the given context.- Specified by:
fallbackInvoke
in interfaceFaultToleranceMethodContext
- Parameters:
fallbackMethod
- theMethod
to invoke- Returns:
- the result returned by the invoked fallback method
- Throws:
Exception
- in case invoking the method fails or the invoked method threw anException
-
trace
public void trace(String method)
Description copied from interface:FaultToleranceMethodContext
Starts tracing the given context named with the given method label.- Specified by:
trace
in interfaceFaultToleranceMethodContext
- Parameters:
method
- the label to use for the trace
-
endTrace
public void endTrace()
Description copied from interface:FaultToleranceMethodContext
Ends the innermost trace.- Specified by:
endTrace
in interfaceFaultToleranceMethodContext
-
-