Class Service

java.lang.Object
org.nanonative.nano.core.model.Service
Direct Known Subclasses:
FileWatcher, HttpClient, HttpServer, LogService, MetricService

public abstract class Service extends Object
Abstract base class for all services in the Nano framework. Provides core functionality for service lifecycle management, event handling, and configuration. Services can implement only the methods they need - all methods are optional and have safe default behaviors. This flexibility allows for minimal service implementations while still providing a robust framework for more complex services when needed.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Context
     
    protected final long
     
    protected final AtomicBoolean
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new Service instance and records its creation timestamp.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    configure(berlin.yuna.typemap.model.TypeMapI<?> config)
    Configures the service with the provided configuration.
    abstract void
    configure(berlin.yuna.typemap.model.TypeMapI<?> changes, berlin.yuna.typemap.model.TypeMapI<?> merged)
    Configures the service with changes while maintaining merged state.
    Gets the current context of the service.
    context(Context context)
    Sets the context for this service.
    long
    Gets the creation timestamp of the service.
    boolean
    Checks if the service is ready to handle requests.
    Gets the ready state of the service as an AtomicBoolean.
    Returns the simple name of the service class.
    Creates and configures a new NanoThread for this service.
    abstract void
    onEvent(Event<?,?> event)
    Processes incoming events for the service.
    abstract Object
    onFailure(Event<?,?> error)
    Handles service failures and errors.
    receiveEvent(Event<?,?> event)
    Processes received events and handles configuration changes.
    abstract void
    Starts the service.
    abstract void
    Stops the service gracefully.
    static NanoThread[]
    threadsOf(Context context, Service... services)
    Creates NanoThreads for multiple services.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • createdAtNs

      protected final long createdAtNs
    • isReady

      protected final AtomicBoolean isReady
    • context

      protected Context context
  • Constructor Details

    • Service

      protected Service()
      Creates a new Service instance and records its creation timestamp. The timestamp can be used for service uptime tracking and performance metrics.
  • Method Details

    • start

      public abstract void start()
      Starts the service. This method is called during service initialization. Optional implementation - services can leave this empty if no startup logic is needed. Common uses include: - Initializing resources - Setting up connections - Starting background tasks See also configure(TypeMapI, TypeMapI) for configuration setup.
    • stop

      public abstract void stop()
      Stops the service gracefully. This method is called during service shutdown. Optional implementation - services can leave this empty if no cleanup is needed. Common uses include: - Closing connections - Releasing resources - Stopping background tasks
    • onFailure

      public abstract Object onFailure(Event<?,?> error)
      Handles service failures and errors. Optional implementation - services can return null if no specific error handling is needed. Null means the error will be logged automatically if no other listener or service handles it. Useful for: - Custom error recovery strategies - Error logging - Notifying other components of failures
      Parameters:
      error - The error event to handle
      Returns:
      Response object from error handling, can be null
    • onEvent

      public abstract void onEvent(Event<?,?> event)
      Processes incoming events for the service. Optional implementation - services can leave this empty if they don't need to handle events. Useful for: - Responding to system events - Inter-service communication - State updates based on external triggers
      Parameters:
      event - The event to process
    • configure

      public void configure(berlin.yuna.typemap.model.TypeMapI<?> config)
      Configures the service with the provided configuration. This is a convenience method that calls configure(config, config). Optional override - default implementation handles basic configuration needs.
      Parameters:
      config - The configuration to apply
    • configure

      public abstract void configure(berlin.yuna.typemap.model.TypeMapI<?> changes, berlin.yuna.typemap.model.TypeMapI<?> merged)
      Configures the service with changes while maintaining merged state. Optional implementation - services can leave this empty if no configuration is needed. Useful for: - Service initialization with configuration - Handling dynamic configuration updates - Managing service state - Applying configuration changes without service restart
      Parameters:
      changes - The new configuration changes to apply
      merged - The complete merged configuration state - this will be represented in context after the method is done
    • name

      public String name()
      Returns the simple name of the service class. This method provides a default naming convention for services. Can be overridden if a custom naming scheme is needed.
      Returns:
      Service name derived from class name
    • context

      public Context context()
      Gets the current context of the service. The context provides access to the service's runtime environment and shared resources. Services typically don't need to override this method.
      Returns:
      The service context
    • isReady

      public boolean isReady()
      Checks if the service is ready to handle requests. Used by the framework to determine if the service has completed initialization. Services typically don't need to override this method.
      Returns:
      true if the service is ready, false otherwise
    • isReadyState

      public AtomicBoolean isReadyState()
      Gets the ready state of the service as an AtomicBoolean. Provides thread-safe access to the service's ready state. Services typically don't need to override this method.
      Returns:
      AtomicBoolean representing the ready state
    • context

      public Service context(Context context)
      Sets the context for this service. Called by the framework during service initialization. Services typically don't need to override this method.
      Parameters:
      context - The context to set
      Returns:
      This service instance for method chaining
    • createdAtNs

      public long createdAtNs()
      Gets the creation timestamp of the service. Useful for monitoring and debugging purposes. Services typically don't need to override this method.
      Returns:
      Creation timestamp in milliseconds since epoch
    • receiveEvent

      public Service receiveEvent(Event<?,?> event)
      Processes received events and handles configuration changes. This is a core framework method that: - Handles configuration update events - Manages configuration merging - Delegates other events to onEvent() Services typically don't need to override this method.
      Parameters:
      event - The event to process
      Returns:
      This service instance for method chaining
    • nanoThread

      public NanoThread nanoThread(Context context)
      Creates and configures a new NanoThread for this service. This is a framework method that handles: - Service initialization in a separate thread - Context setup - Service startup sequence - Ready state management - Error handling Services typically don't need to override this method.
      Parameters:
      context - The context for the thread
      Returns:
      Configured NanoThread instance
    • threadsOf

      public static NanoThread[] threadsOf(Context context, Service... services)
      Creates NanoThreads for multiple services. A utility method that simplifies bulk service thread creation. Useful for: - Starting multiple services in parallel - Managing service groups - Orchestrating service startup
      Parameters:
      context - The context for the threads
      services - Array of services to create threads for
      Returns:
      Array of configured NanoThreads