Class RunnableWrapper

java.lang.Object
host.anzo.commons.threading.RunnableWrapper
All Implemented Interfaces:
Runnable

public class RunnableWrapper extends Object implements Runnable
A wrapper for Runnable tasks that provides error handling and metrics collection.
  • Constructor Details

    • RunnableWrapper

      public RunnableWrapper(Runnable r)
      Creates a new RunnableWrapper for the given Runnable task.
      Parameters:
      r - the Runnable task to wrap
    • RunnableWrapper

      public RunnableWrapper(Runnable r, boolean virtual)
      Creates a new RunnableWrapper for the given Runnable task.
      Parameters:
      r - the Runnable task to wrap
      virtual - whether the task should be treated as virtual for metrics collection
    • RunnableWrapper

      public RunnableWrapper(Runnable r, String name)
      Creates a new RunnableWrapper for the given Runnable task.
      Parameters:
      r - the Runnable task to wrap
      name - the name to use for metrics collection
    • RunnableWrapper

      public RunnableWrapper(Runnable r, boolean virtual, String name)
      Creates a new RunnableWrapper for the given Runnable task.
      Parameters:
      r - the Runnable task to wrap
      virtual - whether the task should be treated as virtual for metrics collection
      name - the name to use for metrics collection
  • Method Details

    • run

      public void run() throws UndeclaredThrowableException
      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 an UndeclaredThrowableException before being rethrown.
      Specified by:
      run in interface Runnable
      Throws:
      UndeclaredThrowableException - if an exception occurs during execution of the wrapped Runnable. The original exception is wrapped within the UndeclaredThrowableException and can be retrieved using Throwable.getCause().
    • getMetric

      @NotNull public static @NotNull MetricResult getMetric()
      Returns the metrics for non-virtual RunnableWrappers.
      Returns:
      the metrics for non-virtual RunnableWrappers
    • getMetricVirtual

      public static MetricResult getMetricVirtual()
      Returns the metrics for virtual RunnableWrappers.
      Returns:
      the metrics for virtual RunnableWrappers