Class CallableWrapper<V>

java.lang.Object
host.anzo.commons.threading.CallableWrapper<V>
All Implemented Interfaces:
Callable<V>

public class CallableWrapper<V> extends Object implements Callable<V>
A wrapper for Callable tasks that provides error handling, default return value and metrics collection.
  • Constructor Details

    • CallableWrapper

      public CallableWrapper(Callable<V> c, V defaultReturnValue)
      Creates a new CallableWrapper for the given Callable task.
      Parameters:
      c - the Callable task to wrap
      defaultReturnValue - the default value to return in case of error
    • CallableWrapper

      public CallableWrapper(Callable<V> c, V defaultReturnValue, boolean virtual)
      Creates a new CallableWrapper for the given Callable task.
      Parameters:
      c - the Callable task to wrap
      defaultReturnValue - the default value to return in case of error
      virtual - whether the task should be treated as virtual for metrics collection
    • CallableWrapper

      public CallableWrapper(Callable<V> c, V defaultReturnValue, String name)
      Creates a new CallableWrapper for the given Callable task.
      Parameters:
      c - the Callable task to wrap
      defaultReturnValue - the default value to return in case of error
      name - the name to use for metrics collection
    • CallableWrapper

      public CallableWrapper(Callable<V> c, V defaultReturnValue, boolean virtual, String name)
      Creates a new CallableWrapper for the given Callable task.
      Parameters:
      c - the Callable task to wrap
      defaultReturnValue - the default value to return in case of error
      virtual - whether the task should be treated as virtual for metrics collection
      name - the name to use for metrics collection
  • Method Details

    • call

      public V call() throws Exception
      Executes the wrapped Callable task. Returns the result of the wrapped Callable or the default value if an error occurs. Collects metrics on execution time and errors if metrics are enabled.
      Specified by:
      call in interface Callable<V>
      Returns:
      the result of the wrapped Callable or the default value
      Throws:
      Exception - if an error occurs during execution of the wrapped Callable. The original exception thrown by the wrapped Callable is preserved and rethrown.
    • getMetric

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

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