SmartLifecycle class abstract mixin

A Lifecycle bean that the context starts by itself at the end of the refresh and stops in order at shutdown.

This is the usual way to run background work with the application: a client that connects at startup, a scheduler, a server. phase orders the beans: lower phases start first and stop last, so a bean that others need gets a lower phase. stopWithCallback lets a bean stop asynchronously (closing a socket), and closeAsync() waits for it. isPauseable keeps a bean running during pause().

Mix it in (with SmartLifecycle) or extend it to get Spring's defaults: isAutoStartup true, phase defaultPhase and stopWithCallback calling stop, then the callback. implements works too; then every member is yours to write.

// Connects when the application starts, disconnects at shutdown,
// before the beans in lower phases stop.
@Component()
class WebSocketClient with SmartLifecycle {
  bool _running = false;

  @override
  void start() => _running = true; // connect

  @override
  void stop() => _running = false; // disconnect

  @override
  bool get isRunning => _running;

  @override
  int get phase => 10;
}

Spring (Java):

@Component
class WebSocketClient implements SmartLifecycle {
  public void start() { ... }
  public void stop() { ... }
  public boolean isRunning() { ... }
  public int getPhase() { return 10; }
}
Implemented types

Constructors

SmartLifecycle()

Properties

hashCode → int
The hash code for this object.
no setterinherited
isAutoStartup → bool
Whether the context starts the bean at the end of the refresh; true by default. With false it starts only on an explicit context.start().
no setter
isPauseable → bool
Whether context.pause() stops the bean (and context.restart() starts it again); true by default, as Spring 7's isPauseable(). Return false for work that must go on while the application is in the background; the bean still stops on stop() and close().
no setter
isRunning → bool
Whether the background work is running; the context asks before starting or stopping the bean.
no setterinherited
phase → int
The start and stop order: lower phases start earlier and stop later; defaultPhase by default. Plain Lifecycle beans count as phase 0. A bean's dependencies start before it whatever their phase.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
start() → void
Starts the background work. The context does not call it when isRunning is already true; an error thrown here fails the start as an ApplicationContextException.
inherited
stop() → void
Stops the background work. The context calls it only when isRunning is true; an error thrown here is logged and the other beans are still stopped.
inherited
stopWithCallback(void callback()) → void
Stops the bean and calls callback once it has really stopped; override it when stopping is asynchronous. By default it calls stop, then callback.
toString() → String
A string representation of this object.
inherited

Operators

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

Constants

defaultPhase → const int
The default phase: the highest value, so the bean starts after all others and stops before them. Spring's SmartLifecycle.DEFAULT_PHASE (Integer.MAX_VALUE).