Class SystemdLifecycle

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

public class SystemdLifecycle extends Object implements ApplicationListener<ContextClosedEvent>
Sends the stopping signal to systemd when the application context starts closing, before its life cycle beans stop, such as the embedded web server, which then waits for the active requests.

Stopping the context without closing it does not send it, as when the context is stopped or paused and started again afterwards, such as by the pause endpoint of Spring Cloud, or for a CRaC checkpoint: the application keeps running, and systemd keeps enforcing the watchdog.

Register it as a bean of the application context it is given, for that context to publish its events to it.

Author:
Joao Silva
See Also:
  • Constructor Details

    • SystemdLifecycle

      public SystemdLifecycle(ApplicationContext context)
      Creates a new instance, which sends the stopping signal with Systemd.instance() when the given context starts closing.
      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)
      Sends the stopping signal, 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