Package org.nanonative.nano.core
Class NanoBase<T extends NanoBase<T>>
java.lang.Object
org.nanonative.nano.core.NanoBase<T>
- Type Parameters:
T- The payload of theNanoBaseimplementation, used for method chaining.
- Direct Known Subclasses:
NanoThreads
The abstract base class for
Nano framework providing the core functionalities.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Contextprotected final longstatic final AtomicIntegerprotected final AtomicIntegerprotected final AtomicBooleanprotected final LogService -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongRetrieves the creation timestamp of the instance.protected voidDisplays a help menu with available configuration keys and their descriptions and exits.intPerforms the eventCount operation.doubleCalculates the memory usage of the application in percentage.booleanisReady()Checks whether the instance is ready for operations.Retrieves the registered event listeners.longpid()Retrieves the process ID of the current instance.protected ContextreadConfigs(String... args) Reads and initializesContextbased on provided arguments.static StringstandardiseKey(Object key) Standardizes a config key.abstract TInitiates the shutdown process for theNanoinstance.abstract TInitiates the shutdown process for theNanoinstance.subscribeError(Consumer<Event<Object, Void>> listener) Registers for global error handling.subscribeError(Channel<C, R> channel, Consumer<Event<C, R>> listener) Registers for global error handling.subscribeEvent(Channel<C, R> channel, BiConsumer<? super Event<C, R>, C> listener) Registers an event listener with a typed payload.<C,R> T subscribeEvent(Channel<C, R> channel, Consumer<? super Event<C, R>> listener) Registers an event listener with a typed payload.<C,R> T unsubscribeEvent(int channelId, Consumer<Event<C, R>> listener) Removes a registered event listener for a specific event payload.doubleCalculates the memory usage of the application in megabytes.
-
Field Details
-
context
-
createdAtNs
protected final long createdAtNs -
logService
-
listeners
-
isReady
-
eventCount
-
EVENT_CHANNELS
-
CONFIG_KEYS
-
EVENT_ID_COUNTER
-
-
Constructor Details
-
NanoBase
Initializes the NanoBase with provided configurations and arguments.- Parameters:
configs- Configuration settings in a key-value map.args- Command line arguments.
-
-
Method Details
-
stop
Initiates the shutdown process for theNanoinstance.- Parameters:
clazz- class for which theContextis to be created.- Returns:
- Self for chaining
-
stop
Initiates the shutdown process for theNanoinstance. -
listeners
Retrieves the registered event listeners.- Returns:
- A map of event types to their respective listeners.
-
subscribeEvent
Registers an event listener with a typed payload.- Type Parameters:
C- The payloadR- The return payload- Parameters:
channel- The channel to be subscribed.listener- The bi-consumer to receive theEventand its payload.- Returns:
- Self for chaining
-
subscribeEvent
public <C,R> Consumer<Event<C,R>> subscribeEvent(Channel<C, R> channel, BiConsumer<? super Event<C, R>, C> listener) Registers an event listener with a typed payload.- Type Parameters:
C- The payloadR- The return payload- Parameters:
channel- The channel to be subscribed.listener- The bi-consumer to receive theEventand its payload.- Returns:
- A consumer function that can be used to unsubscribe the listener later.
-
subscribeError
public <C,R> Consumer<Event<Object,Void>> subscribeError(Channel<C, R> channel, Consumer<Event<C, R>> listener) Registers for global error handling.- Type Parameters:
C- The payloadR- The return payload- Parameters:
channel- The channel which should be handled on error.listener- The bi-consumer to receive theEventand its payload.- Returns:
- A consumer function that can be used to unsubscribe the listener later.
-
subscribeError
Registers for global error handling.- Parameters:
listener- The consumer to receive theEventand its payload.- Returns:
- A consumer function that can be used to unsubscribe the listener later.
-
unsubscribeEvent
Removes a registered event listener for a specific event payload.- Parameters:
channelId- The integer identifier of the event payload.listener- The consumer function to be removed.- Returns:
- Self for chaining
-
pid
public long pid()Retrieves the process ID of the current instance.- Returns:
- The process ID.
-
usedMemoryMB
public double usedMemoryMB()Calculates the memory usage of the application in megabytes.- Returns:
- Memory usage in megabytes, rounded to two decimal places.
-
heapMemoryUsage
public double heapMemoryUsage()Calculates the memory usage of the application in percentage.- Returns:
- Memory usage in percentage, rounded to two decimal places.
-
createdAtMs
public long createdAtMs()Retrieves the creation timestamp of the instance.- Returns:
- The timestamp of creation in milliseconds.
-
isReady
public boolean isReady()Checks whether the instance is ready for operations.- Returns:
- readiness state.
-
eventCount
public int eventCount()Performs the eventCount operation.- Returns:
- the result
-
displayHelpMenu
protected void displayHelpMenu()Displays a help menu with available configuration keys and their descriptions and exits. -
readConfigs
Reads and initializesContextbased on provided arguments.- Parameters:
args- Command-line arguments.- Returns:
- The
Contextinitialized with the configurations.
-
standardiseKey
Standardizes a config key.- Parameters:
key- The config key to be standardized.
-