Package org.nanonative.nano.core.model
Class Service
java.lang.Object
org.nanonative.nano.core.model.Service
- Direct Known Subclasses:
FileWatcher,HttpClient,HttpServer,LogService,MetricService
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
FieldsModifier and TypeFieldDescriptionprotected Contextprotected final longprotected final AtomicBoolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedService()Creates a new Service instance and records its creation timestamp. -
Method Summary
Modifier and TypeMethodDescriptionvoidconfigure(berlin.yuna.typemap.model.TypeMapI<?> config) Configures the service with the provided configuration.abstract voidconfigure(berlin.yuna.typemap.model.TypeMapI<?> changes, berlin.yuna.typemap.model.TypeMapI<?> merged) Configures the service with changes while maintaining merged state.context()Gets the current context of the service.Sets the context for this service.longGets the creation timestamp of the service.booleanisReady()Checks if the service is ready to handle requests.Gets the ready state of the service as an AtomicBoolean.name()Returns the simple name of the service class.nanoThread(Context context) Creates and configures a new NanoThread for this service.abstract voidProcesses incoming events for the service.abstract ObjectHandles service failures and errors.receiveEvent(Event<?, ?> event) Processes received events and handles configuration changes.abstract voidstart()Starts the service.abstract voidstop()Stops the service gracefully.static NanoThread[]Creates NanoThreads for multiple services.
-
Field Details
-
createdAtNs
protected final long createdAtNs -
isReady
-
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 alsoconfigure(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
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
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 applymerged- The complete merged configuration state - this will be represented incontextafter the method is done
-
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
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
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
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
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
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
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 threadsservices- Array of services to create threads for- Returns:
- Array of configured NanoThreads
-