Package host.anzo.commons.threading
Class RunnableWrapper
java.lang.Object
host.anzo.commons.threading.RunnableWrapper
- All Implemented Interfaces:
Runnable
A wrapper for
Runnable tasks that provides error handling and metrics collection.-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new RunnableWrapper for the given Runnable task.RunnableWrapper(Runnable r, boolean virtual) Creates a new RunnableWrapper for the given Runnable task.RunnableWrapper(Runnable r, boolean virtual, String name) Creates a new RunnableWrapper for the given Runnable task.RunnableWrapper(Runnable r, String name) Creates a new RunnableWrapper for the given Runnable task. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull MetricResultReturns the metrics for non-virtual RunnableWrappers.static MetricResultReturns the metrics for virtual RunnableWrappers.voidrun()Executes the wrapped Runnable task.
-
Constructor Details
-
RunnableWrapper
Creates a new RunnableWrapper for the given Runnable task.- Parameters:
r- the Runnable task to wrap
-
RunnableWrapper
Creates a new RunnableWrapper for the given Runnable task.- Parameters:
r- the Runnable task to wrapvirtual- whether the task should be treated as virtual for metrics collection
-
RunnableWrapper
Creates a new RunnableWrapper for the given Runnable task.- Parameters:
r- the Runnable task to wrapname- the name to use for metrics collection
-
RunnableWrapper
Creates a new RunnableWrapper for the given Runnable task.- Parameters:
r- the Runnable task to wrapvirtual- whether the task should be treated as virtual for metrics collectionname- the name to use for metrics collection
-
-
Method Details
-
run
Executes the wrapped Runnable task. Collects metrics on execution time and errors if metrics are enabled. If an exception occurs during the execution of the wrapped Runnable, it is logged and wrapped in anUndeclaredThrowableExceptionbefore being rethrown.- Specified by:
runin interfaceRunnable- Throws:
UndeclaredThrowableException- if an exception occurs during execution of the wrapped Runnable. The original exception is wrapped within theUndeclaredThrowableExceptionand can be retrieved usingThrowable.getCause().
-
getMetric
Returns the metrics for non-virtual RunnableWrappers.- Returns:
- the metrics for non-virtual RunnableWrappers
-
getMetricVirtual
Returns the metrics for virtual RunnableWrappers.- Returns:
- the metrics for virtual RunnableWrappers
-