Class TimedCallback

java.lang.Object
com.kosdev.kos.commons.util.TimedCallback

public class TimedCallback extends Object
Wrapper for a callback that handles exceptions and measures the time the callback took and logs a warning if it takes longer than expected. Since most callbacks are intended to be completed quickly, this provides an easy way to warn users when their code is unexpectedly using event threads for long running tasks.
Since:
10
Version:
10
  • Field Details

    • DEFAULT_WARNING_TIME_MS

      public static final int DEFAULT_WARNING_TIME_MS
      See Also:
  • Constructor Details

    • TimedCallback

      public TimedCallback()
  • Method Details

    • getDefaultWarningTimeMs

      public static int getDefaultWarningTimeMs()
      Return the current default warning time.
      Since:
      10
    • call

      public static void call(String name, Object listener, TimedCallback.Callback callback)
      Call the specified callback with the default warning time.
      Parameters:
      name - name of the call for use in log messages
      listener - the listener being called, the class name is used in log messages
      callback - the callback to run
      Since:
      10
    • call

      public static void call(String name, Object listener, int warningTimeMs, TimedCallback.Callback callback)
      Call the specified callback in the current thread. Log an error if the callback throws an exception or a warning if it takes too long to run.
      Parameters:
      name - name of the call for use in log messages
      listener - the listener being called, the class name is used in log messages
      warningTimeMs - how long until a warning is generated
      callback - the callback to run
      Since:
      10
    • execute

      public static void execute(String name, Object listener, int warningTimeMs, TimedCallback.Callback callback)
      Call the specified callback in a thread pool thread. Log an error if the callback throws an exception or a warning if it takes too long to run.
      Parameters:
      name - name of the call for use in log messages
      listener - the listener being called, the class name is used in log messages
      warningTimeMs - how long until a warning is generated
      callback - the callback to run
      Since:
      10
    • execute

      public static void execute(RunnableQueues queues, Object queueKey, String name, Object listener, int warningTimeMs, TimedCallback.Callback callback)
      Call the specified callback in a thread pool thread using the specified RunnableQueues and queue key. Log an error if the callback throws an exception or a warning if it takes too long to run. This allows callbacks to be executed in strict order, using concurrency where possible based on the queue key provided.
      Parameters:
      queues - the RunnableQueues to execute from
      queueKey - the key for the queue to use
      name - name of the call for use in log messages
      listener - the listener being called, the class name is used in log messages
      warningTimeMs - how long until a warning is generated
      callback - the callback to run
      Since:
      10