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:
  • Field Details

    • CONTEXT_TRACE_ID_KEY

      public static final String CONTEXT_TRACE_ID_KEY
      See Also:
    • CONTEXT_LOGGER_KEY

      public static final String CONTEXT_LOGGER_KEY
      See Also:
    • CONTEXT_PARENT_KEY

      public static final String CONTEXT_PARENT_KEY
      See Also:
    • CONTEXT_CLASS_KEY

      public static final String CONTEXT_CLASS_KEY
      See Also:
    • CONTEXT_NANO_KEY

      public static final String CONTEXT_NANO_KEY
      See Also:
    • CONTEXT_LOG_QUEUE_KEY

      public static final String CONTEXT_LOG_QUEUE_KEY
      See Also:
    • APP_HELP

      public static final String APP_HELP
    • APP_PARAMS

      public static final String APP_PARAMS
    • CONFIG_PROFILES

      public static final String CONFIG_PROFILES
    • CONFIG_LOG_LEVEL

      public static final String CONFIG_LOG_LEVEL
    • CONFIG_LOG_FORMATTER

      public static final String CONFIG_LOG_FORMATTER
    • CONFIG_LOG_QUEUE_SIZE

      public static final String CONFIG_LOG_QUEUE_SIZE
    • CONFIG_THREAD_POOL_TIMEOUT_MS

      public static final String CONFIG_THREAD_POOL_TIMEOUT_MS
    • CONFIG_PARALLEL_SHUTDOWN

      public static final String CONFIG_PARALLEL_SHUTDOWN
    • CONFIG_OOM_SHUTDOWN_THRESHOLD

      public static final String CONFIG_OOM_SHUTDOWN_THRESHOLD
    • CONFIG_ENV_PROD

      public static final String 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

      protected transient Nano nano
  • Constructor Details

    • Context

      protected Context(Class<?> clazz)
    • Context

      protected Context(Context parent, Class<?> clazz)
    • Context

      protected Context(Context parent, Class<?> clazz, boolean empty)
  • Method Details

    • createRootContext

      public static Context createRootContext(Class<?> clazz)
      Creates a new root context with a unique trace ID.
      Returns:
      The newly created root context.
    • nano

      public Nano nano()
      Retrieves the Nano instance associated with this context.
      Returns:
      The Nano instance associated with this context.
    • parent

      public Context parent()
      Retrieves the Context parent associated with this context.
      Returns:
      Parent Context or null
    • traceId

      public String traceId()
      Retrieves the last created trace ID of the context.
      Returns:
      The last created trace ID of the context.
    • traceId

      public String traceId(int index)
      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

      public List<String> traceIds()
      Retrieves all trace IDs associated with this context.
      Returns:
      A list of all trace IDs associated with this context.
    • logger

      public NanoLogger logger()
      Retrieves the logger associated with this context.
      Returns:
      The logger associated with this context.
    • newContext

      public Context newContext(Class<?> clazz)
      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

      public Context newEmptyContext(Class<?> clazz)
      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

      public LogLevel logLevel()
      Retrieves the log level of the context logger.
      Returns:
      The log level of the context logger.
    • putAll

      public void putAll(Map<?,?> map)
      Specified by:
      putAll in interface Map<Object,Object>
      Overrides:
      putAll in class ConcurrentHashMap<Object,Object>
    • put

      public Context put(Object key, Object value)
      Puts a key-value pair into the context.
      Specified by:
      put in interface Map<Object,Object>
      Overrides:
      put in class ConcurrentHashMap<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 Context instance, allowing for method chaining and further configuration.
    • putReturn

      public Context putReturn(Object key, Object value)
      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 ConcurrentTypeMap instance for chaining.
    • subscribeEvent

      public Context subscribeEvent(int channelId, Consumer<Event> listener)
      Registers an event listener for a specific event type.
      Parameters:
      channelId - The integer identifier of the event type.
      listener - The consumer function that processes the Event.
      Returns:
      Self for chaining
    • unsubscribeEvent

      public Context unsubscribeEvent(int channelId, Consumer<Event> listener)
      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

      public Context run(ExRunnable task, long delay, TimeUnit timeUnit)
      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

      public Context run(ExRunnable task, long delay, long period, TimeUnit unit)
      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

      public Context run(ExRunnable task, long delay, long period, TimeUnit unit, BooleanSupplier until)
      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. true stops the next execution.
      Returns:
      Self for chaining
    • run

      public Context run(ExRunnable task, LocalTime atTime)
      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

      public Context run(ExRunnable task, LocalTime atTime, BooleanSupplier until)
      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. true stops the next execution.
      Returns:
      Self for chaining
    • initLogger

      protected NanoLogger initLogger()
      Sets the logger name for the context logger.
      Returns:
      The created NanoLogger
    • run

      public final Context run(ExRunnable... runnable)
      Executes one or multiple runnable asynchronously.
      Parameters:
      runnable - function to execute.
      Returns:
      The Context object for chaining further operations.
    • runHandled

      public final Context runHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable)
      Executes one or multiple runnable asynchronously.
      Parameters:
      onFailure - function to execute on failure
      runnable - function to execute.
      Returns:
      The Context object for chaining further operations.
    • run

      public Context run(Service... services)
      Executes one or multiple Service asynchronously.
      Parameters:
      services - The Service to be appended.
      Returns:
      The Context object for chaining further operations.
    • runReturn

      public final NanoThread[] runReturn(ExRunnable... runnable)
      Executes one or multiple runnable asynchronously.
      Parameters:
      runnable - function to execute.
      Returns:
      NanoThreads
    • runReturnHandled

      public final NanoThread[] runReturnHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable)
      Executes one or multiple runnable asynchronously.
      Parameters:
      onFailure - function to execute on failure
      runnable - function to execute.
      Returns:
      NanoThreads
    • runReturn

      public NanoThread[] runReturn(Service... services)
      Executes one or multiple Service asynchronously.
      Parameters:
      services - The Service to be appended.
      Returns:
      NanoThreads
    • runAwait

      public final Context runAwait(ExRunnable... runnable)
      Executes asynchronously and waits for all runnable to be ready
      Parameters:
      runnable - function to execute.
      Returns:
      The Context object for chaining further operations.
    • runAwaitHandled

      public final Context runAwaitHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable)
      Executes asynchronously and waits for all runnable to be ready
      Parameters:
      onFailure - function to execute on failure
      runnable - function to execute.
      Returns:
      The Context object for chaining further operations.
    • runAwait

      public Context runAwait(Service... services)
      Executes asynchronously and waits for all Service to be ready
      Returns:
      The Context object for chaining further operations.
    • runAwaitReturn

      public final NanoThread[] runAwaitReturn(ExRunnable... runnable)
      Executes asynchronously and waits for all Service to be ready
      Parameters:
      runnable - function to execute.
      Returns:
      NanoThreads
    • runAwaitReturnHandled

      public final NanoThread[] runAwaitReturnHandled(Consumer<Unhandled> onFailure, ExRunnable... runnable)
      Executes and waits for all Service to be ready
      Parameters:
      onFailure - function to execute on failure
      runnable - function to execute.
      Returns:
      NanoThreads
    • runAwaitReturn

      public NanoThread[] runAwaitReturn(Service... services)
      Executes and waits for all Service to be ready
      Returns:
      NanoThreads
    • sendEventError

      public Context sendEventError(Object payload, Throwable throwable)
      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

      public Context sendEventError(Event event, Service service, Throwable throwable)
      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

      public Context sendEvent(int channelId, Object payload)
      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 Context instance, allowing for method chaining and further configuration.
    • sendEvent

      public Context sendEvent(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. 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 Context instance, facilitating method chaining and further actions.
    • broadcastEvent

      public Context broadcastEvent(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. 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 Context instance, enabling method chaining and additional configurations.
    • broadcastEvent

      public Context 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. 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 Context instance, allowing for method chaining and further actions.
    • sendEventReturn

      public Event sendEventReturn(int channelId, Object payload)
      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 Event that represents the event being processed. This object can be used for further operations or tracking.
    • sendEventReturn

      public Event 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. 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 Event that represents the event being processed. This object can be used for further operations or tracking.
    • broadcastEventReturn

      public Event 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. 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 Event that represents the event being processed. This object can be used for further operations or tracking.
    • broadcastEventReturn

      public Event 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. 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 Event that represents the event being processed. This object can be used for further operations or tracking.
    • registerChannelId

      public int registerChannelId(String channelName)
      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

      public String eventNameOf(int channelId)
      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

      public Optional<Integer> channelIdOf(String channelName)
      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 Optional containing the ID of the event type if found, or empty if not found or if the input is null or empty.
    • service

      public <S extends Service> S service(Class<S> serviceClass)
      Retrieves a Service of a specified type.
      Type Parameters:
      S - The type of the service to retrieve, which extends Service.
      Parameters:
      serviceClass - The class of the Service to retrieve.
      Returns:
      The first instance of the specified Service, or null if not found.
    • service

      public <S extends Service> S service(Class<S> serviceClass, long timeoutMs)
      Waits for a Service of a specified type to be available within the given timeout.
      Type Parameters:
      S - The type of the service to retrieve, which extends Service.
      Parameters:
      serviceClass - The class of the Service to retrieve.
      timeoutMs - The maximum time to wait, in milliseconds.
      Returns:
      The first instance of the specified Service, or null if not found within the timeout.
    • services

      public <S extends Service> List<S> services(Class<S> serviceClass)
      Retrieves a list of services of a specified type.
      Type Parameters:
      S - The type of the service to retrieve, which extends Service.
      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

      public List<Service> services()
      Provides an unmodifiable list of all registered Service.
      Returns:
      An unmodifiable list of Service instances.
    • tryExecute

      public void tryExecute(ExRunnable operation)
    • tryExecute

      public void tryExecute(ExRunnable operation, Consumer<Throwable> consumer)
    • toString

      public String toString()
      Overrides:
      toString in class ConcurrentHashMap<Object,Object>