ApplicationShutdownHandlerHook class
The ApplicationShutdownHandlerHook is responsible for managing the graceful
shutdown of a JetLeaf application, ensuring that all registered
ConfigurableApplicationContext instances and shutdown handlers are executed
in a predictable and safe order when the Dart process exits or receives a
termination signal.
This class supports:
- Automatic shutdown hook registration using Dart's
ProcessSignalmechanism to handleSIGINTandSIGTERM. - Tracking of active and closed contexts to avoid double closures.
- Execution of registered shutdown handlers in reverse registration order to ensure that dependent services are shut down in a safe sequence.
- Environment cleanup such as disposing of logging listeners.
- Concurrency safety, with internal synchronization to avoid race conditions when multiple contexts or handlers are being registered or executed concurrently.
Usage Example:
final shutdownHook = ApplicationShutdownHandlerHook();
shutdownHook.enableShutdownHook(); // Allow automatic signal registration
shutdownHook.registerApplicationContext(appContext);
shutdownHook.handler.add(() => print("Custom shutdown action"));
References:
- ConfigurableApplicationContext: Represents an application context that can be started and closed.
- Runnable: Interface implemented by shutdown handlers.
_ApplicationContextClosedListener: Internal listener monitoring context closure.
- Implemented types
Constructors
- ApplicationShutdownHandlerHook()
-
The
ApplicationShutdownHandlerHookis responsible for managing the graceful shutdown of a JetLeaf application, ensuring that all registered ConfigurableApplicationContext instances and shutdown handlers are executed in a predictable and safe order when the Dart process exits or receives a termination signal.
Properties
- handler → ApplicationShutdownHandler
-
Returns the internal shutdown handlers manager.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addRuntimeShutdownHookIfNecessary(
) → void - Adds a runtime shutdown hook if necessary.
-
assertNotInProgress(
) → void - Asserts that the shutdown hook is not currently in progress.
-
enableShutdownHook(
) → void - Enables the addition of a runtime shutdown hook to the Dart VM.
-
isApplicationContextRegistered(
ConfigurableApplicationContext context) → bool - Checks if a ConfigurableApplicationContext is registered for shutdown.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
registerApplicationContext(
ConfigurableApplicationContext context) → void - Registers a ConfigurableApplicationContext to be shut down when the Dart process exits.
-
reset(
) → void - Resets the shutdown hook state, clearing all registered contexts, handlers, and closing any active contexts.
-
run(
) → Future< void> -
Executes the task encapsulated by this instance.
override
-
toString(
) → String -
A string representation of this object.
inherited
-
unregisterFailedApplicationContext(
ConfigurableApplicationContext context) → void - Removes a failed or inactive application context from tracking.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited