Class TaskLifecycleListener

java.lang.Object
org.springframework.cloud.task.listener.TaskLifecycleListener
All Implemented Interfaces:
EventListener, org.springframework.beans.factory.DisposableBean, org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>, org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle, org.springframework.core.Ordered

public class TaskLifecycleListener extends Object implements org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>, org.springframework.context.SmartLifecycle, org.springframework.beans.factory.DisposableBean, org.springframework.core.Ordered
Monitors the lifecycle of a task. This listener will record both the start and end of a task in the registered TaskRepository. The following events are used to identify the start and end of a task:
  • Lifecycle.start() - Used to identify the start of a task. A task is expected to contain a single application context.
  • ApplicationReadyEvent - Used to identify the successful end of a task.
  • ApplicationFailedEvent - Used to identify the failure of a task.
  • Lifecycle.stop() - Used to identify the end of a task, if the ApplicationReadyEvent or ApplicationFailedEvent is not emitted. This can occur if an error occurs while executing a BeforeTask.
Note: By default, the context will close at the completion of the task unless other non-daemon threads keep it running. Programatic closing of the context can be configured via the property spring.cloud.task.closecontext_enabled (defaults to false). If the spring.cloud.task.closecontext_enabled is set to true, then the context will be closed upon task completion regardless if non-daemon threads are still running. Also if the context did not start, the FailedTask and TaskEnd may not have all the dependencies met.
Author:
Michael Minella, Glenn Renfro
  • Constructor Details

  • Method Details

    • onApplicationEvent

      public void onApplicationEvent(org.springframework.context.ApplicationEvent applicationEvent)
      Utilizes ApplicationEvents to determine the end and failure of a task. Specifically:
      • ApplicationReadyEvent - Successful end of a task
      • ApplicationFailedEvent - Failure of a task
      Specified by:
      onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
      Parameters:
      applicationEvent - The application being listened for.
    • isAutoStartup

      public boolean isAutoStartup()
      Specified by:
      isAutoStartup in interface org.springframework.context.SmartLifecycle
    • stop

      public void stop(Runnable callback)
      Specified by:
      stop in interface org.springframework.context.SmartLifecycle
    • start

      public void start()
      Specified by:
      start in interface org.springframework.context.Lifecycle
    • stop

      public void stop()
      Specified by:
      stop in interface org.springframework.context.Lifecycle
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface org.springframework.context.Lifecycle
    • getPhase

      public int getPhase()
      Specified by:
      getPhase in interface org.springframework.context.Phased
      Specified by:
      getPhase in interface org.springframework.context.SmartLifecycle
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered