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_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_NANO_NAME

      public static final String APP_NANO_NAME
      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_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 Channel<Void,Void> EVENT_APP_START
    • EVENT_APP_SHUTDOWN

      public static final Channel<Void,Void> EVENT_APP_SHUTDOWN
    • EVENT_APP_SERVICE_REGISTER

      public static final Channel<Service,Void> EVENT_APP_SERVICE_REGISTER
    • EVENT_APP_SERVICE_UNREGISTER

      public static final Channel<Service,Void> EVENT_APP_SERVICE_UNREGISTER
    • EVENT_APP_SCHEDULER_REGISTER

      public static final Channel<Scheduler,Void> EVENT_APP_SCHEDULER_REGISTER
    • EVENT_APP_SCHEDULER_UNREGISTER

      public static final Channel<Scheduler,Void> EVENT_APP_SCHEDULER_UNREGISTER
    • EVENT_APP_ERROR

      public static final Channel<Object,Void> EVENT_APP_ERROR
    • EVENT_APP_OOM

      public static final Channel<Double,Void> EVENT_APP_OOM
    • EVENT_APP_HEARTBEAT

      public static final Channel<Void,Void> EVENT_APP_HEARTBEAT
    • EVENT_CONFIG_CHANGE

      public static final Channel<Map,Void> 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.
    • fatal

      public Context fatal(Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • fatal

      public Context fatal(Throwable thrown, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      thrown - The exception to log.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • error

      public Context error(Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • error

      public Context error(Throwable thrown, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      thrown - The exception to log.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • warn

      public Context warn(Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • warn

      public Context warn(Throwable thrown, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      thrown - The exception to log.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • info

      public Context info(Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • info

      public Context info(Throwable thrown, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      thrown - The exception to log.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • debug

      public Context debug(Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • debug

      public Context debug(Throwable thrown, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      thrown - The exception to log.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • trace

      public Context trace(Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • trace

      public Context trace(Throwable thrown, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      thrown - The exception to log.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • log

      public Context log(LogLevel level, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      level - The log level to use.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • log

      public Context log(LogLevel level, Throwable thrown, Supplier<String> message, Object... params)
      Logs a message with the specified log level.
      Parameters:
      level - The log level to use.
      thrown - The exception to log.
      message - The message to log.
      params - The parameters to replace in the message.
      Returns:
      self for chaining
    • 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.
    • 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.
    • putR

      public Context putR(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 <C, R> Context subscribeEvent(Channel<C,R> channel, Consumer<Event<C,R>> listener)
      Registers an event listener with a typed payload.
      Type Parameters:
      C - The payload
      R - The return payload
      Parameters:
      channel - The channel to be subscribed.
      listener - The bi-consumer to receive the Event and its payload.
      Returns:
      Self for chaining
    • subscribeEvent

      public <C, R> Consumer<Event<C,R>> subscribeEvent(Channel<C,R> channel, Predicate<Event<C,R>> filter, Consumer<Event<C,R>> listener)
      Registers an event listener with a typed payload.
      Type Parameters:
      C - The payload
      R - The return payload
      Parameters:
      channel - The channel to be subscribed.
      filter - A predicate to filter events before passing them to the listener.
      listener - The bi-consumer to receive the Event and its payload.
      Returns:
      A consumer function that can be used to unsubscribe the listener later.
    • subscribeEvent

      public <C, R> Consumer<Event<C,R>> subscribeEvent(Channel<C,R> channel, BiConsumer<Event<C,R>,C> listener)
      Registers an event listener with a typed payload.
      Type Parameters:
      C - The payload
      R - The return payload
      Parameters:
      channel - The channel to be subscribed.
      listener - The bi-consumer to receive the Event and 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 payload
      R - The return payload
      Parameters:
      channel - The channel which should be handled on error.
      listener - The bi-consumer to receive the Event and its payload.
      Returns:
      A consumer function that can be used to unsubscribe the listener later.
    • subscribeError

      public <C, R> Consumer<Event<Object,Void>> subscribeError(Consumer<Event<Object,Void>> listener)
      Registers for global error handling.
      Parameters:
      listener - The consumer to receive the Event and its payload.
      Returns:
      A consumer function that can be used to unsubscribe the listener later.
    • unsubscribeEvent

      public <C, R> Context unsubscribeEvent(Channel<C,R> channel, Consumer<Event<C,R>> listener)
      Removes a registered event listener for a specific event payload.
      Parameters:
      channel - The channel to be unsubscribed
      listener - The consumer function to be removed.
      Returns:
      Self for chaining
    • unsubscribeEvent

      public <C, R> Context unsubscribeEvent(int channelId, Consumer<Event<C,R>> listener)
      Removes a registered event listener for a specific event payload.
      Parameters:
      channelId - The channel id to be unsubscribed
      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. nano.run(() -> myMethod(), LocalTime.of(7, 0, 0))
      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. nano.run(() -> myMethod(), LocalTime.of(7, 0, 0))
      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
    • run

      public Context run(ExRunnable task, LocalTime atTime, DayOfWeek dow, BooleanSupplier until)
      Executes a task periodically, starting after an initial delay. nano.run(() -> myMethod(), LocalTime.of(7, 0, 0))
      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
    • runDaily

      public Context runDaily(ExRunnable task, LocalTime... atTime)
      Executes a task daily at the specified wall-clock time in the server's default time zone. Runs indefinitely.
      Parameters:
      task - the task to execute
      atTime - the daily wall-clock time (hour, minute, second)
      Returns:
      self for chaining
    • runDaily

      public Context runDaily(ExRunnable task, BooleanSupplier until, LocalTime... atTime)
      Executes a task daily at the specified wall-clock time in the server's default time zone until the stop condition returns true.
      Parameters:
      task - the task to execute
      until - stop condition; when true, cancels further runs
      atTime - the daily wall-clock time (hour, minute, second)
      Returns:
      self for chaining
    • runWeekly

      public Context runWeekly(ExRunnable task, LocalTime atTime, DayOfWeek... dow)
      Executes a task weekly at the given day of week and wall-clock time in the server's default time zone. Runs indefinitely.
      Parameters:
      task - the task to execute
      atTime - the weekly wall-clock time (hour, minute, second)
      dow - the day of the week
      Returns:
      self for chaining
    • runWeekly

      public Context runWeekly(ExRunnable task, LocalTime atTime, BooleanSupplier until, DayOfWeek... dow)
      Executes a task weekly at the given day of week and wall-clock time in the server's default time zone until the stop condition returns true.
      Parameters:
      task - the task to execute
      atTime - the weekly wall-clock time (hour, minute, second)
      until - stop condition; when true, cancels further runs
      dow - the day of the week
      Returns:
      self for chaining
    • run

      public Context run(ExRunnable task, LocalTime atTime, DayOfWeek dow, ZoneId zone, BooleanSupplier until)
      Core scheduling method for daily or weekly execution at a fixed wall-clock time. Uses ZonedDateTime in the given zone to account for daylight saving changes.
      Parameters:
      task - the task to execute
      atTime - the wall-clock time (hour, minute, second)
      dow - optional day of week; if null, runs every day
      zone - the time zone (usually ZoneId.systemDefault())
      until - stop condition; when true, cancels further runs
      Returns:
      self for chaining
    • 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<Event<Object,Void>> 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.
    • runR

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

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

      public NanoThread[] runR(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<Event<Object,Void>> 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.
    • runAwaitR

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

      public final NanoThread[] runAwaitRHandled(Consumer<Event<Object,Void>> 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
    • runAwaitR

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

      public Context sendEventError(Object payloadOrEvent, 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:
      payloadOrEvent - The payload of the unhandled event or object, 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
    • newEvent

      public <C, R> Event<C,R> newEvent(Channel<C,R> channel)
      Creates a new Event instance with the specified event payload.
      Parameters:
      channel - The Channel for the event.
      Returns:
      An instance of Event that represents the event being processed. This object can be used for further operations or tracking.
    • newEvent

      public <C, R> Event<C,R> newEvent(Channel<C,R> channel, Supplier<C> payload)
      Creates a new Event instance with the specified event payload.
      Parameters:
      channel - The Channel for the event.
      payload - A supplier that provides the payload for the event. This allows for lazy evaluation of the payload, which can be useful for performance or when the payload is not immediately available.
      Returns:
      An instance of Event that represents the event being processed. This object can be used for further operations or tracking.
    • registerChannelId

      public <C> Channel<C,Void> registerChannelId(String name, Class<C> payload)
      Registers a new Channel with a given name if it does not already exist. If the Channel payload already exists, it returns the existing Channel.
      Parameters:
      name - The name of the Channel payload to register.
      payload - The class type of the payload for the Channel.
      Returns:
      The Channel of the newly registered event payload, or the Channel of the existing event payload if it already exists. Returns null if the input is null or empty.
    • registerChannelId

      public <C, R> Channel<C,R> registerChannelId(String name, Class<C> payload, Class<R> response)
      Registers a new Channel with a given name if it does not already exist. If the Channel payload already exists, it returns the existing Channel.
      Parameters:
      name - The name of the Channel payload to register.
      payload - The class type of the payload for the Channel.
      response - The class type of the response for the Channel.
      Returns:
      The Channel of the newly registered event payload, or the Channel of the existing event payload if it already exists. Returns null if the input is null or empty.
    • service

      public <S extends Service> S service(Class<S> serviceClass)
      Retrieves a Service of a specified payload.
      Type Parameters:
      S - The payload 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.
    • services

      public <S extends Service> List<S> services(Class<S> serviceClass)
      Retrieves a list of services of a specified payload.
      Type Parameters:
      S - The payload 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 payload. If no services of this payload 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>