Java Reference

KosUtil

Introduction

KosUtil is a global object with static methods that makes it easier to access common components.

API Components

This section describes the KosUtil static object.

KosUtil

The KosUtil object is a static accessor for a number of core KOS components that are so common that it’s inefficient to inject them everywhere they’re needed.

Related classes are defined in more detail on their respective pages:

API: KosUtil class
public final class KosUtil {

    // Private constructor, as this is a utility class:
    private KosUtil();

    //--- Time and Sleep ---

    // Returns the current actual time (in msec); same as System.currentTimeMillis():
    public static long currentTimeMs();

    // Returns the current mono time (monotonically increasing, in msec):
    public static long currentMonoMs();

    // Shorthand access to scheduler.sleep(), which allows for sleeping without the need to handle interrupts:
    public static void sleep(long ms);

    //--- JSON handling ---

    // Returns the shared JSON (Jackson) object mapper:
    public static ObjectMapper getMapper();

    // Returns the shared JSON writer cache:
    public static JsonWriterCache getWriters();  // [discussed on its own page]

    //--- Executing and scheduling runnables ---

    // Returns the system component that provides access to standard thread pools:
    public static Scheduler getScheduler();      // [discussed on its own page]

    // Shorthand access to scheduler.execute(), which runs a short duration task in a worker thread:
    public static void execute(Runnable runnable);

    // Shorthand access to scheduler.scheduleCallback(), which schedules a Runnable
    //  that is executed "delay" number of msec in the future:
    public static ScheduledFuture<?> scheduleCallback(Runnable command, long delay);
}

Example Code

The following sections demonstrate some of the KosUtil methods.

Source code for this and most other Java reference pages is available on GitHub.

Ex: Time and sleep()

This example demonstrates the two different "time" methods and the sleep method:

KosUtilModule class
@Slf4j
public class KosUtilModule implements Module {

    // . . .

    private void logTimes() {
        log.info("> MonoMsec1: {}", KosUtil.currentMonoMs());
        log.info("> TimeMsec1: {}", KosUtil.currentTimeMs());
        log.info("> sleep(1000) called");
        KosUtil.sleep(1000);
        log.info("> MonoMsec2: {}", KosUtil.currentMonoMs());
        log.info("> TimeMsec2: {}", KosUtil.currentTimeMs());
    }
}

Executing this method gives the following log output:

KosUtilModule output
> ====== BEGIN =========================================
> MyKosApp.load()
> KosUtilModule.init()
> MyKosApp.start()
> KosUtilModule.run()
> MonoMsec1: 13420         (1)
> TimeMsec1: 1696961902018 (2)
> sleep(1000) called       (3)
> MonoMsec2: 14420         (4)
> TimeMsec2: 1696961903019 (5)
> ====== END ===========================================
1 The first call to currentMonoMs() is issued; note that mono time starts at zero when the JVM starts
2 The first call to currentTimeMs()
3 Call to sleep the current thread by one second
4 After waking, we see that the mono time has increased by 1000 msec, as expected
5 And that the system time has increased by 1001 msec, also as expected

Ex: getMapper()

Ex: execute()

Ex: scheduledCallback()

Summary

TODO

Previous
Next
On this page
Java Development
Seamlessly transition from Legacy+ systems to Freestyle microdosing and advanced distributed dispense systems.
UI Development
Using KOS SDKs, integrating Consumer and Non-consumer facing UIs becomes seamless, giving you less hassle and more time to create.
Video Library
Meet some of our development team, as they lead you through the tools, features, and tips and tricks of various KOS tools.
Resources
Familiarize yourself with KOS terminology, our reference materials, and explore additional resources that complement your KOS journey.
Copyright © 2024 TCCC. All rights reserved.