Class KosUtil

java.lang.Object
com.tccc.kos.commons.util.KosUtil

public final class KosUtil extends Object
Static accessor for a number of core kOS components that are so common it's inefficient to inject them everywhere they're needed.

Similar to KosCore except that all of these components have interfaces and default implementations, so that it's possible to inject them if desired, and it's also possible to override the implementation used in this class if desired. This balances the desire to write testable components that use core functionality (use injection) vs. pragmatic implementation where the goal is to minimize overhead and keep things simple (use the static accessors).

Since:
1.0
Version:
2023-03-06
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    Returns the current mono time in milliseconds.
    static long
    Returns the current time in milliseconds.
    static void
    execute(Runnable runnable)
    Shorthand access to scheduler.execute() , which runs a short duration task in a worker thread.
    Returns the shared JSON ObjectMapper .
    static Scheduler
    Returns the built-in system Scheduler , which provides access to standard thread pools used throughout the kOS system.
    Returns the shared JSON writer cache.
    static ScheduledFuture<?>
    scheduleCallback(Runnable command, long delay)
    Shorthand access to scheduler.scheduleCallback() , which schedules a Runnable that is executed delay number of milliseconds in the future.
    static void
    sleep(long ms)
    Shorthand access to scheduler.sleep() , which allows for sleeping without the need to handle interruptions.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getScheduler

      public static Scheduler getScheduler()
      Returns the built-in system Scheduler , which provides access to standard thread pools used throughout the kOS system.
    • getMapper

      public static ObjectMapper getMapper()
      Returns the shared JSON ObjectMapper .
    • getWriters

      public static JsonWriterCache getWriters()
      Returns the shared JSON writer cache.
    • currentTimeMs

      public static long currentTimeMs()
      Returns the current time in milliseconds. Returns the same value as System.currentTimeMillis() ; exists as a symmetric call to currentMonoMs() .
    • currentMonoMs

      public static long currentMonoMs()
      Returns the current mono time in milliseconds. Mono time is monotonically increasing and is not impacted by changing the current time/date. Mono time should be used anywhere there is a need for relative vs. absolute time.
    • sleep

      public static void sleep(long ms)
      Shorthand access to scheduler.sleep() , which allows for sleeping without the need to handle interruptions.
      Parameters:
      ms - the number of milliseconds to sleep
    • execute

      public static void execute(Runnable runnable)
      Shorthand access to scheduler.execute() , which runs a short duration task in a worker thread. There are a limited number of worker threads, so long-running tasks should be executed in a different pool.
      Parameters:
      runnable - the Runnable to execute
    • scheduleCallback

      public static ScheduledFuture<?> scheduleCallback(Runnable command, long delay)
      Shorthand access to scheduler.scheduleCallback() , which schedules a Runnable that is executed delay number of milliseconds in the future. A ScheduledFuture is returned, which can be used to cancel the callback.
      Parameters:
      command - the command to execute
      delay - delay before the command is executed (in msec)
      Returns:
      Future for use in cancelling the callback