Class ShutdownStatusProvider

java.lang.Object
com.github.jpmsilva.jsystemd.spring.ShutdownStatusProvider
All Implemented Interfaces:
StatusProvider, EventListener, ApplicationListener<ContextClosedEvent>

public class ShutdownStatusProvider extends Object implements StatusProvider, ApplicationListener<ContextClosedEvent>
Implementation of StatusProvider that provides the stage the application is at while it shuts down, such as Shutdown: waiting for active requests, so that a slow shutdown can be told apart from a hung one with systemctl status.

The stages follow the shutdown of the application context:

  1. Shutdown: stopping, once the context starts closing, while its life cycle beans stop, such as message listeners;
  2. Shutdown: waiting for active requests, while the embedded web server waits for the requests being handled to complete, when it shuts down gracefully, as it does by default;
  3. Shutdown: destroying beans, once all life cycle beans are stopped, while the beans are destroyed, such as connection pools.

Every change of stage is sent to systemd right away. The status of the other providers keeps being sent while the application shuts down, such as the connections of the web server, which drop as the requests complete. Nothing is reported until the application starts shutting down, nor when the context is stopped without being closed, as when it is paused, or for a CRaC checkpoint.

The stages are followed by life cycle beans, which the autoconfiguration registers along with this provider.

Author:
Joao Silva
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance, which sends the status to systemd on every change of stage.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the life cycle bean that reports when the beans start being destroyed: it is stopped after every other life cycle bean, after which the context destroys its beans.
    void
    Reports that the application started shutting down, when the event is about its own application context: the events of child contexts, such as the one of the management endpoints when they use a port of their own, are also published to their parents.
    Returns the life cycle bean that reports when the web server starts waiting for the active requests: it is stopped right before the web server shuts down gracefully.
    Exposes status information to send to systemd.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.context.ApplicationListener

    supportsAsyncExecution
  • Constructor Details

    • ShutdownStatusProvider

      public ShutdownStatusProvider(ApplicationContext context)
      Creates a new instance, which sends the status to systemd on every change of stage.

      The created instance is not registered as a status provider of Systemd.instance(): that is up to the caller.

      Parameters:
      context - the application context of the application, which this instance must be a bean of, rather than a parent or child context
  • Method Details

    • onApplicationEvent

      public void onApplicationEvent(ContextClosedEvent event)
      Reports that the application started shutting down, when the event is about its own application context: the events of child contexts, such as the one of the management endpoints when they use a port of their own, are also published to their parents.

      Spring publishes the event first when closing a context, before it stops the life cycle beans and destroys the beans, despite the wording of the documentation of ContextClosedEvent: the application is still running when the event is received.

      Specified by:
      onApplicationEvent in interface ApplicationListener<ContextClosedEvent>
      Parameters:
      event - the event of a context that started closing
    • requestsStage

      public SmartLifecycle requestsStage()
      Returns the life cycle bean that reports when the web server starts waiting for the active requests: it is stopped right before the web server shuts down gracefully. Register it as a bean, for the context to stop it.
      Returns:
      the life cycle bean of the stage of waiting for the active requests
    • beansStage

      public SmartLifecycle beansStage()
      Returns the life cycle bean that reports when the beans start being destroyed: it is stopped after every other life cycle bean, after which the context destroys its beans. Register it as a bean, for the context to stop it.
      Returns:
      the life cycle bean of the stage of destroying the beans
    • status

      public String status()
      Description copied from interface: StatusProvider
      Exposes status information to send to systemd.
      Specified by:
      status in interface StatusProvider
      Returns:
      the string message to add to the service status