Package com.tccc.kos.commons.util
Class KosUtil
java.lang.Object
com.tccc.kos.commons.util.KosUtil
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 TypeMethodDescriptionstatic long
Returns the current mono time in milliseconds.static long
Returns the current time in milliseconds.static void
Shorthand access toscheduler.execute()
, which runs a short duration task in a worker thread.static ObjectMapper
Returns the shared JSONObjectMapper
.static Scheduler
Returns the built-in systemScheduler
, which provides access to standard thread pools used throughout the kOS system.static JsonWriterCache
Returns the shared JSON writer cache.static ScheduledFuture<?>
scheduleCallback
(Runnable command, long delay) Shorthand access toscheduler.scheduleCallback()
, which schedules aRunnable
that is executeddelay
number of milliseconds in the future.static void
sleep
(long ms) Shorthand access toscheduler.sleep()
, which allows for sleeping without the need to handle interruptions.
-
Method Details
-
getScheduler
Returns the built-in systemScheduler
, which provides access to standard thread pools used throughout the kOS system. -
getMapper
Returns the shared JSONObjectMapper
. -
getWriters
Returns the shared JSON writer cache. -
currentTimeMs
public static long currentTimeMs()Returns the current time in milliseconds. Returns the same value asSystem.currentTimeMillis()
; exists as a symmetric call tocurrentMonoMs()
. -
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 toscheduler.sleep()
, which allows for sleeping without the need to handle interruptions.- Parameters:
ms
- the number of milliseconds to sleep
-
execute
Shorthand access toscheduler.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
- theRunnable
to execute
-
scheduleCallback
Shorthand access toscheduler.scheduleCallback()
, which schedules aRunnable
that is executeddelay
number of milliseconds in the future. AScheduledFuture
is returned, which can be used to cancel the callback.- Parameters:
command
- the command to executedelay
- delay before the command is executed (in msec)- Returns:
Future
for use in cancelling the callback
-