SmartLifecycle class abstract

An extension of the Lifecycle interface for those objects that require to be started upon ApplicationContext refresh and/or shutdown in a particular order.

The isAutoStartup return value indicates whether this object should be started at the time of a context refresh. The callback-accepting stop method is useful for objects that have an asynchronous shutdown process.

This interface extends Phased, and the getPhase method's return value indicates the phase within which this Lifecycle component should be started and stopped. The startup process begins with the lowest phase value and ends with the highest phase value. The shutdown process applies the reverse order.

Phase-based Startup/Shutdown

  1. Startup: Phase -1000 → -100 → 0 → 100 → 1000
  2. Shutdown: Phase 1000 → 100 → 0 → -100 → -1000

Usage Example

class MessageBroker implements SmartLifecycle {
  bool _running = false;

  @override
  bool isAutoStartup() => true;

  @override
  int getPhase() => 0;

  @override
  void start() {
    if (!_running) {
      connectToBroker();
      _running = true;
    }
  }

  @override
  void stop(Runnable? callback) {
    if (_running) {
      disconnectFromBroker(() {
        _running = false;
        callback?.run();
      });
    } else {
      callback?.run();
    }
  }

  @override
  bool isRunning() => _running;
}
Implemented types
Implementers

Constructors

SmartLifecycle()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

getPhase() int
Interface for objects that may participate in a phased process such as lifecycle management.
override
isAutoStartup() bool
An extension of the Lifecycle interface for those objects that require to be started upon ApplicationContext refresh and/or shutdown in a particular order.
isRunning() bool
Check whether this component is currently running.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
start() FutureOr<void>
A common interface defining methods for start/stop lifecycle control.
inherited
stop([Runnable? callback]) FutureOr<void>
Stop this component, typically in a synchronous fashion.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Constants

DEFAULT_PHASE → const int
Default phase for SmartLifecycle implementations.