Module aeonics.core

Class Lifecycle

  • All Implemented Interfaces:
    Exportable, Snapshotable

    public abstract class Lifecycle
    extends Manager.Type
    The lifecycle manager should be considered as a global event bus for different application phases.

    Distinct Lifecycle.Phase will occur during the application lifecycle. Plugins or other components may register calback handlers to perform some business logic accordingly, either before the event is triggered, on (when) the event is triggered, or after the event has been trigered.

    Handlers must be instances of the Callback.Once class so that they will be executed only once. This is to prevent accidential executions in case a phase is triggered multiple times.

    A phase is complete when all handlers have completed, therefore it is not considered as an applicaiton "state" but more like an event. A phase could (but should not) be triggered multiple times over the entire application lifecycle. In regular conditions, only one phase will run at a time. Unless you have valid reasons to do so, it is always preferable to use the on methods to avoid unintended side effects.

    The lifecycle phases will always happen in this sequence:

    1. Lifecycle.Phase.BOOT: this is the initial state and it does not raise any events.
    2. Lifecycle.Phase.LOAD: when all plugins have been preloaded using Plugin.start(), the load phase begins.
      1. before: in this stage, you may perform some of your own initialization logic but it should not rely on any other plugin, managers, registry or factory.
      2. during: the Lifecycle, Executor and Config managers are available but not populated yet. At this stage, you can register Factory items and declare Config parameters.
      3. after: this step is the last moment to register an initial Snapshot.onRestore(aeonics.util.Functions.Consumer) handler. All remaining managers are available but not fully populated yet.
    3. Lifecycle.Phase.CONFIG: when managers and entity types are registered, the initial configuration loading begins.
      1. before: in this stage, the initial snapshot restore happens. It means that the default config and registry is being populated now and will only be available in the next stage. Although, you may perform some restore and initialization, or defer it for the next phase by keeping a reference to the restore data.
      2. during: The config manager is now fully populated thanks to the snapshot restore operation. The registry is also populated with restored entities and you can also register your own entities.
      3. after: if you need to act on existing registry items, it may be a convenient time to do so.
    4. Lifecycle.Phase.RUN: everything is setup and populated, so the system is entering the run phase.
      1. before: in this stage, the Logger manager is started instead of simple stdout. In case there is no security provider defined from the previous stage, a default one is created with basic permissive security rules. All the managers that have active components are starting in this phase.
      2. during: in this stage, you can start your own entities, use the network and executor managers. All Origin entities will be started automatically in this phase.
      3. after: this is the last stage for the normal startup sequence.
    5. Lifecycle.Phase.SHUTDOWN: when the system is requested to stop, the shutdown phase begins.
      1. before: you may handle some business logic before other elements apply their shutdown sequence.
      2. during: all executor tasks are being terminated, the network is closing and everything gets ready for shutdown.
      3. after: in this stage, you shall not use any managers, registry or factory anymore. The logger is falling back to stdout and all remaining Origin entities are stopped.
    • Constructor Detail

      • Lifecycle

        public Lifecycle()
    • Method Detail

      • manager

        public final java.lang.Class<? extends Manager.Type> manager()
        Hardcoded manager type
        Specified by:
        manager in class Manager.Type
        Returns:
        the type of manager
      • phase

        public abstract Lifecycle.Phase phase()
        Returns the current lifecycle phase
        Returns:
        the current lifecycle phase
      • boot

        public abstract void boot()
        Initiates the boot sequence. This method must return only after the Lifecycle.Phase.SHUTDOWN is complete.
      • before

        public static void before​(Lifecycle.Phase phase,
                                  Callback.Once<Lifecycle.Phase,​Lifecycle> handler)
        Registers a handler to run before other handlers in the specified phase. All handlers are global and shared for all instances of the Lifecycle manager.
        Parameters:
        phase - the application phase
        handler - the handler to run
      • before

        public static void before​(Lifecycle.Phase phase,
                                  Functions.Runnable handler)
        Registers a handler to run before other handlers in the specified phase. All handlers are global and shared for all instances of the Lifecycle manager.
        Parameters:
        phase - the application phase
        handler - the handler to run
      • on

        public static void on​(Lifecycle.Phase phase,
                              Callback.Once<Lifecycle.Phase,​Lifecycle> handler)
        Registers a handler to run in the specified phase. All handlers are global and shared for all instances of the Lifecycle manager.
        Parameters:
        phase - the application phase
        handler - the handler to run
      • on

        public static void on​(Lifecycle.Phase phase,
                              Functions.Runnable handler)
        Registers a handler to run in the specified phase. All handlers are global and shared for all instances of the Lifecycle manager.
        Parameters:
        phase - the application phase
        handler - the handler to run
      • after

        public static void after​(Lifecycle.Phase phase,
                                 Callback.Once<Lifecycle.Phase,​Lifecycle> handler)
        Registers a handler to run after other handlers in the specified phase. All handlers are global and shared for all instances of the Lifecycle manager.
        Parameters:
        phase - the application phase
        handler - the handler to run
      • after

        public static void after​(Lifecycle.Phase phase,
                                 Functions.Runnable handler)
        Registers a handler to run after other handlers in the specified phase. All handlers are global and shared for all instances of the Lifecycle manager.
        Parameters:
        phase - the application phase
        handler - the handler to run