Package org.nanonative.nano.core.model
Class Context
java.lang.Object
java.util.AbstractMap<Object,Object>
java.util.concurrent.ConcurrentHashMap<Object,Object>
berlin.yuna.typemap.model.ConcurrentTypeMap
org.nanonative.nano.core.model.Context
- All Implemented Interfaces:
berlin.yuna.typemap.model.TypeInfo<berlin.yuna.typemap.model.ConcurrentTypeMap>,berlin.yuna.typemap.model.TypeMapI<berlin.yuna.typemap.model.ConcurrentTypeMap>,Serializable,ConcurrentMap<Object,,Object> Map<Object,Object>
public class Context
extends berlin.yuna.typemap.model.ConcurrentTypeMap
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ConcurrentHashMap
ConcurrentHashMap.KeySetView<K,V> Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intprotected Nano -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbroadcastEvent(int channelId, Object payload) Broadcasts an event of the specified type with the provided payload to all listeners within this context without expecting a response.broadcastEvent(int channelId, Object payload, Consumer<Object> responseListener) Broadcasts an event of the specified type with the provided payload to all listeners within this context, expecting a response that is handled by the provided responseListener.broadcastEventReturn(int channelId, Object payload) Broadcasts an event of the specified type with the provided payload to all listeners within this context without expecting a response.broadcastEventReturn(int channelId, Object payload, Consumer<Object> responseListener) Broadcasts an event of the specified type with the provided payload to all listeners within this context, expecting a response that is handled by the provided responseListener.channelIdOf(String channelName) Attempts to find the ID of an event type based on its name.static ContextcreateRootContext(Class<?> clazz) Creates a new root context with a unique trace ID.eventNameOf(int channelId) Retrieves the name of an event type given its ID.protected NanoLoggerSets the logger name for the context logger.logger()Retrieves the logger associated with this context.logLevel()Retrieves the log level of the context logger.nano()Retrieves theNanoinstance associated with this context.newContext(Class<?> clazz) Creates new Context with a new logger and trace ID.newEmptyContext(Class<?> clazz) Creates new empty Context with a new logger and trace ID.parent()Retrieves theContextparent associated with this context.Puts a key-value pair into the context.voidAssociates the specified value with the specified key in this map.intregisterChannelId(String channelName) Registers a new event type with a given name if it does not already exist.Executes one or multipleServiceasynchronously.final Contextrun(ExRunnable... runnable) Executes one or multiple runnable asynchronously.run(ExRunnable task, long delay, long period, TimeUnit unit) Executes a task periodically, starting after an initial delay.run(ExRunnable task, long delay, long period, TimeUnit unit, BooleanSupplier until) Executes a task periodically, starting after an initial delay.run(ExRunnable task, long delay, TimeUnit timeUnit) Executes a task asynchronously after a specified delay.run(ExRunnable task, LocalTime atTime) Executes a task periodically, starting after an initial delay.run(ExRunnable task, LocalTime atTime, BooleanSupplier until) Executes a task periodically, starting after an initial delay.Executes asynchronously and waits for allServiceto be readyfinal ContextrunAwait(ExRunnable... runnable) Executes asynchronously and waits for all runnable to be readyfinal ContextrunAwaitHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable) Executes asynchronously and waits for all runnable to be readyrunAwaitReturn(Service... services) Executes and waits for allServiceto be readyfinal NanoThread[]runAwaitReturn(ExRunnable... runnable) Executes asynchronously and waits for allServiceto be readyfinal NanoThread[]runAwaitReturnHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable) Executes and waits for allServiceto be readyfinal ContextrunHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable) Executes one or multiple runnable asynchronously.Executes one or multipleServiceasynchronously.final NanoThread[]runReturn(ExRunnable... runnable) Executes one or multiple runnable asynchronously.final NanoThread[]runReturnHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable) Executes one or multiple runnable asynchronously.Sends an event of the specified type with the provided payload within this context without expecting a response.Sends an event of the specified type with the provided payload within this context, expecting a response that is handled by the provided responseListener.sendEventError(Object payload, Throwable throwable) Sends an unhandled event with the provided, nullable payload and exception.sendEventError(Event event, Service service, Throwable throwable) Sends an unhandled event with the provided, nullable payload and exception.sendEventReturn(int channelId, Object payload) Sends an event of the specified type with the provided payload within this context without expecting a response.sendEventReturn(int channelId, Object payload, Consumer<Object> responseListener) Sends an event of the specified type with the provided payload within this context, expecting a response that is handled by the provided responseListener.<S extends Service>
SRetrieves aServiceof a specified type.<S extends Service>
SWaits for aServiceof a specified type to be available within the given timeout.services()Provides an unmodifiable list of all registeredService.Retrieves a list of services of a specified type.subscribeEvent(int channelId, Consumer<Event> listener) Registers an event listener for a specific event type.toString()traceId()Retrieves the last created trace ID of the context.traceId(int index) Retrieves the trace ID at the specified index.traceIds()Retrieves all trace IDs associated with this context.voidtryExecute(ExRunnable operation) voidtryExecute(ExRunnable operation, Consumer<Throwable> consumer) unsubscribeEvent(int channelId, Consumer<Event> listener) Removes a registered event listener for a specific event type.Methods inherited from class berlin.yuna.typemap.model.ConcurrentTypeMap
addReturn, concurrentMapOfMethods inherited from class java.util.concurrent.ConcurrentHashMap
clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, forEach, forEach, forEachEntry, forEachEntry, forEachKey, forEachKey, forEachValue, forEachValue, get, getOrDefault, hashCode, isEmpty, keys, keySet, keySet, mappingCount, merge, newKeySet, newKeySet, putIfAbsent, reduce, reduceEntries, reduceEntries, reduceEntriesToDouble, reduceEntriesToInt, reduceEntriesToLong, reduceKeys, reduceKeys, reduceKeysToDouble, reduceKeysToInt, reduceKeysToLong, reduceToDouble, reduceToInt, reduceToLong, reduceValues, reduceValues, reduceValuesToDouble, reduceValuesToInt, reduceValuesToLong, remove, remove, replace, replace, replaceAll, search, searchEntries, searchKeys, searchValues, size, valuesMethods inherited from class java.util.AbstractMap
cloneMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, remove, size, valuesMethods inherited from interface berlin.yuna.typemap.model.TypeInfo
as, asArray, asArray, asAtomicBoolean, asAtomicBooleanOpt, asAtomicInteger, asAtomicIntegerOpt, asAtomicLong, asAtomicLongOpt, asBigDecimal, asBigDecimalOpt, asBigInteger, asBigIntegerOpt, asBoolean, asBooleanOpt, asBooleans, asByte, asByteOpt, asBytes, asCalendar, asCalendarOpt, asCalendars, asCharacter, asCharacterOpt, asCharacters, asCharset, asCharsetOpt, asCharsets, asDate, asDateOpt, asDates, asDouble, asDoubleOpt, asDoubles, asFile, asFileOpt, asFiles, asFloat, asFloatOpt, asFloats, asInet4Address, asInet4Addresses, asInet4AddressOpt, asInet6Address, asInet6Addresses, asInet6AddressOpt, asInetAddress, asInetAddresses, asInetAddressOpt, asInstant, asInstantOpt, asInstants, asInt, asIntOpt, asInts, asList, asList, asList, asLocalDate, asLocalDateOpt, asLocalDates, asLocalDateTime, asLocalDateTimeOpt, asLocalDateTimes, asLocalTime, asLocalTimeOpt, asLocalTimes, asLong, asLongOpt, asLongs, asMap, asMap, asMap, asMap, asMap, asMap, asNumber, asNumberOpt, asNumbers, asOffsetDateTime, asOffsetDateTimeOpt, asOffsetDateTimes, asPath, asPathOpt, asPaths, asShort, asShortOpt, asShorts, asSqlDate, asSqlDateOpt, asSqlDates, asString, asStringOpt, asStrings, asThrowable, asThrowableOpt, asThrowables, asTime, asTimeOpt, asTimes, asTimestamp, asTimestampOpt, asTimestamps, asURI, asURIOpt, asURIs, asURL, asURLOpt, asURLs, asUUID, asUUIDOpt, asUUIDs, asZonedDateTime, asZonedDateTimeOpt, asZonedDateTimes, get, getOpt, toJson, toJsonMethods inherited from interface berlin.yuna.typemap.model.TypeMapI
typeListOpt, typeMapOpt
-
Field Details
-
CONTEXT_TRACE_ID_KEY
- See Also:
-
CONTEXT_LOGGER_KEY
- See Also:
-
CONTEXT_PARENT_KEY
- See Also:
-
CONTEXT_CLASS_KEY
- See Also:
-
CONTEXT_NANO_KEY
- See Also:
-
CONTEXT_LOG_QUEUE_KEY
- See Also:
-
APP_HELP
-
APP_PARAMS
-
CONFIG_PROFILES
-
CONFIG_LOG_LEVEL
-
CONFIG_LOG_FORMATTER
-
CONFIG_LOG_QUEUE_SIZE
-
CONFIG_THREAD_POOL_TIMEOUT_MS
-
CONFIG_PARALLEL_SHUTDOWN
-
CONFIG_OOM_SHUTDOWN_THRESHOLD
-
CONFIG_ENV_PROD
-
EVENT_APP_START
public static final int EVENT_APP_START -
EVENT_APP_SHUTDOWN
public static final int EVENT_APP_SHUTDOWN -
EVENT_APP_SERVICE_REGISTER
public static final int EVENT_APP_SERVICE_REGISTER -
EVENT_APP_SERVICE_UNREGISTER
public static final int EVENT_APP_SERVICE_UNREGISTER -
EVENT_APP_SCHEDULER_REGISTER
public static final int EVENT_APP_SCHEDULER_REGISTER -
EVENT_APP_SCHEDULER_UNREGISTER
public static final int EVENT_APP_SCHEDULER_UNREGISTER -
EVENT_APP_UNHANDLED
public static final int EVENT_APP_UNHANDLED -
EVENT_APP_ERROR
public static final int EVENT_APP_ERROR -
EVENT_APP_OOM
public static final int EVENT_APP_OOM -
EVENT_APP_HEARTBEAT
public static final int EVENT_APP_HEARTBEAT -
EVENT_CONFIG_CHANGE
public static final int EVENT_CONFIG_CHANGE -
nano
-
-
Constructor Details
-
Context
-
Context
-
Context
-
-
Method Details
-
createRootContext
Creates a new root context with a unique trace ID.- Returns:
- The newly created root context.
-
nano
Retrieves theNanoinstance associated with this context.- Returns:
- The
Nanoinstance associated with this context.
-
parent
Retrieves theContextparent associated with this context.- Returns:
- Parent
Contextor null
-
traceId
Retrieves the last created trace ID of the context.- Returns:
- The last created trace ID of the context.
-
traceId
Retrieves the trace ID at the specified index.- Parameters:
index- The index of the trace ID to retrieve.- Returns:
- The trace ID at the specified index, or the last trace ID if the index is out of bounds.
-
traceIds
Retrieves all trace IDs associated with this context.- Returns:
- A list of all trace IDs associated with this context.
-
logger
Retrieves the logger associated with this context.- Returns:
- The logger associated with this context.
-
newContext
Creates new Context with a new logger and trace ID.- Parameters:
clazz- The class to use for the logger name. If null, the logger name will be the class of the context.- Returns:
- The newly created context.
-
newEmptyContext
Creates new empty Context with a new logger and trace ID.- Parameters:
clazz- The class to use for the logger name. If null, the logger name will be the class of the context.- Returns:
- The newly created context.
-
logLevel
Retrieves the log level of the context logger.- Returns:
- The log level of the context logger.
-
putAll
-
put
Puts a key-value pair into the context.- Specified by:
putin interfaceMap<Object,Object> - Overrides:
putin classConcurrentHashMap<Object,Object> - Parameters:
key- The key to put into the context. Null keys are interpreted as empty strings.value- The value to associate with the key.- Returns:
- The current
Contextinstance, allowing for method chaining and further configuration.
-
putReturn
Associates the specified value with the specified key in this map.- Parameters:
key- the key with which the specified value is to be associated.value- the value to be associated with the specified key.- Returns:
- the updated
ConcurrentTypeMapinstance for chaining.
-
subscribeEvent
Registers an event listener for a specific event type.- Parameters:
channelId- The integer identifier of the event type.listener- The consumer function that processes theEvent.- Returns:
- Self for chaining
-
unsubscribeEvent
Removes a registered event listener for a specific event type.- Parameters:
channelId- The integer identifier of the event type.listener- The consumer function to be removed.- Returns:
- Self for chaining
-
run
Executes a task asynchronously after a specified delay.- Parameters:
task- The task to execute.delay- The delay before executing the task.timeUnit- The time unit of the delay parameter.- Returns:
- Self for chaining
-
run
Executes a task periodically, starting after an initial delay.- Parameters:
task- The task to execute.delay- The initial delay before executing the task.period- The period between successive task executions.unit- The time unit of the initialDelay and period parameters.- Returns:
- Self for chaining
-
run
Executes a task periodically, starting after an initial delay.- Parameters:
task- The task to execute.delay- The initial delay before executing the task.period- The period between successive task executions.unit- The time unit of the initialDelay and period parameters.until- A BooleanSupplier indicating the termination condition.truestops the next execution.- Returns:
- Self for chaining
-
run
Executes a task periodically, starting after an initial delay.- Parameters:
task- The task to execute.atTime- The time of hour/minute/second to start the task.- Returns:
- Self for chaining
-
run
Executes a task periodically, starting after an initial delay.- Parameters:
task- The task to execute.atTime- The time of hour/minute/second to start the task.until- A BooleanSupplier indicating the termination condition.truestops the next execution.- Returns:
- Self for chaining
-
initLogger
Sets the logger name for the context logger.- Returns:
- The created
NanoLogger
-
run
Executes one or multiple runnable asynchronously.- Parameters:
runnable- function to execute.- Returns:
- The
Contextobject for chaining further operations.
-
runHandled
Executes one or multiple runnable asynchronously.- Parameters:
onFailure- function to execute on failurerunnable- function to execute.- Returns:
- The
Contextobject for chaining further operations.
-
run
Executes one or multipleServiceasynchronously. -
runReturn
Executes one or multiple runnable asynchronously.- Parameters:
runnable- function to execute.- Returns:
NanoThreads
-
runReturnHandled
Executes one or multiple runnable asynchronously.- Parameters:
onFailure- function to execute on failurerunnable- function to execute.- Returns:
NanoThreads
-
runReturn
Executes one or multipleServiceasynchronously.- Parameters:
services- TheServiceto be appended.- Returns:
NanoThreads
-
runAwait
Executes asynchronously and waits for all runnable to be ready- Parameters:
runnable- function to execute.- Returns:
- The
Contextobject for chaining further operations.
-
runAwaitHandled
Executes asynchronously and waits for all runnable to be ready- Parameters:
onFailure- function to execute on failurerunnable- function to execute.- Returns:
- The
Contextobject for chaining further operations.
-
runAwait
Executes asynchronously and waits for allServiceto be ready- Returns:
- The
Contextobject for chaining further operations.
-
runAwaitReturn
Executes asynchronously and waits for allServiceto be ready- Parameters:
runnable- function to execute.- Returns:
NanoThreads
-
runAwaitReturnHandled
public final NanoThread[] runAwaitReturnHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable) Executes and waits for allServiceto be ready- Parameters:
onFailure- function to execute on failurerunnable- function to execute.- Returns:
NanoThreads
-
runAwaitReturn
Executes and waits for allServiceto be ready- Returns:
NanoThreads
-
sendEventError
Sends an unhandled event with the provided, nullable payload and exception. If the event is not acknowledged, the error message is logged.- Parameters:
payload- The payload of the unhandled event, containing data relevant to the event's context and purpose.throwable- The exception that occurred during the event processing.- Returns:
- self for chaining
-
sendEventError
Sends an unhandled event with the provided, nullable payload and exception. If the event is not acknowledged, the error message is logged.- Parameters:
event- The unhandled event, containing data relevant to the event's context and purpose.service- The service which failed to handle the event.throwable- The exception that occurred during the event processing.- Returns:
- self for chaining
-
sendEvent
Sends an event of the specified type with the provided payload within this context without expecting a response. This method is used for sending targeted events that do not require asynchronous processing or response handling.- Parameters:
channelId- The integer representing the type of the event, identifying the nature or action of the event.payload- The payload of the event, containing data relevant to the event's context and purpose.- Returns:
- The current
Contextinstance, allowing for method chaining and further configuration.
-
sendEvent
Sends an event of the specified type with the provided payload within this context, expecting a response that is handled by the provided responseListener. This method allows for asynchronous event processing and response handling through the specified consumer.- Parameters:
channelId- The integer representing the type of the event.payload- The payload of the event, containing the data to be communicated.responseListener- A consumer that processes the response of the event. This allows for asynchronous event handling and response processing.- Returns:
- The current
Contextinstance, facilitating method chaining and further actions.
-
broadcastEvent
Broadcasts an event of the specified type with the provided payload to all listeners within this context without expecting a response. This method is ideal for notifying all interested parties of a particular event where no direct response is required.- Parameters:
channelId- The integer representing the type of the event, used to notify all listeners interested in this type of event.payload- The payload of the event, containing information relevant to the broadcast.- Returns:
- The current
Contextinstance, enabling method chaining and additional configurations.
-
broadcastEvent
Broadcasts an event of the specified type with the provided payload to all listeners within this context, expecting a response that is handled by the provided responseListener. This method allows for the broad dissemination of an event while also facilitating asynchronous response processing.- Parameters:
channelId- The integer representing the type of the event.payload- The payload associated with the event, intended for widespread distribution.responseListener- A consumer that handles the response of the event, enabling asynchronous processing and response handling across multiple listeners.- Returns:
- The current
Contextinstance, allowing for method chaining and further actions.
-
sendEventReturn
Sends an event of the specified type with the provided payload within this context without expecting a response. This method is used for sending targeted events that do not require asynchronous processing or response handling.- Parameters:
channelId- The integer representing the type of the event, identifying the nature or action of the event.payload- The payload of the event, containing data relevant to the event's context and purpose.- Returns:
- An instance of
Eventthat represents the event being processed. This object can be used for further operations or tracking.
-
sendEventReturn
Sends an event of the specified type with the provided payload within this context, expecting a response that is handled by the provided responseListener. This method allows for asynchronous event processing and response handling through the specified consumer.- Parameters:
channelId- The integer representing the type of the event.payload- The payload of the event, containing the data to be communicated.responseListener- A consumer that processes the response of the event. This allows for asynchronous event handling and response processing.- Returns:
- An instance of
Eventthat represents the event being processed. This object can be used for further operations or tracking.
-
broadcastEventReturn
Broadcasts an event of the specified type with the provided payload to all listeners within this context without expecting a response. This method is ideal for notifying all interested parties of a particular event where no direct response is required.- Parameters:
channelId- The integer representing the type of the event, used to notify all listeners interested in this type of event.payload- The payload of the event, containing information relevant to the broadcast.- Returns:
- An instance of
Eventthat represents the event being processed. This object can be used for further operations or tracking.
-
broadcastEventReturn
Broadcasts an event of the specified type with the provided payload to all listeners within this context, expecting a response that is handled by the provided responseListener. This method allows for the broad dissemination of an event while also facilitating asynchronous response processing.- Parameters:
channelId- The integer representing the type of the event.payload- The payload associated with the event, intended for widespread distribution.responseListener- A consumer that handles the response of the event, enabling asynchronous processing and response handling across multiple listeners.- Returns:
- An instance of
Eventthat represents the event being processed. This object can be used for further operations or tracking.
-
registerChannelId
Registers a new event type with a given name if it does not already exist. If the event type already exists, it returns the existing event type's ID.- Parameters:
channelName- The name of the event type to register.- Returns:
- The ID of the newly registered event type, or the ID of the existing event type if it already exists. Returns -1 if the input is null or empty.
-
eventNameOf
Retrieves the name of an event type given its ID.- Parameters:
channelId- The ID of the event type.- Returns:
- The name of the event type associated with the given ID, or null if not found.
-
channelIdOf
Attempts to find the ID of an event type based on its name. This method is primarily used for debugging purposes or startup and is not optimized for performance.- Parameters:
channelName- The name of the event type.- Returns:
- An
Optionalcontaining the ID of the event type if found, or empty if not found or if the input is null or empty.
-
service
Retrieves aServiceof a specified type. -
service
Waits for aServiceof a specified type to be available within the given timeout. -
services
Retrieves a list of services of a specified type.- Type Parameters:
S- The type of the service to retrieve, which extendsService.- Parameters:
serviceClass- The class of the service to retrieve.- Returns:
- A list of services of the specified type. If no services of this type are found, an empty list is returned.
-
services
Provides an unmodifiable list of all registeredService.- Returns:
- An unmodifiable list of
Serviceinstances.
-
tryExecute
-
tryExecute
-
toString
- Overrides:
toStringin classConcurrentHashMap<Object,Object>
-