Package org.springframework.cloud.task.listener

# 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

- ## Field Summary

  ### Fields inherited from interface org.springframework.core.Ordered

  `HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE`

  ### Fields inherited from interface org.springframework.context.SmartLifecycle

  `DEFAULT_PHASE`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TaskLifecycleListener(TaskRepository taskRepository, TaskNameResolver taskNameResolver, org.springframework.boot.ApplicationArguments applicationArguments, TaskExplorer taskExplorer, TaskProperties taskProperties, TaskListenerExecutorObjectFactory taskListenerExecutorObjectFactory)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `destroy()`
  `int`
  `getOrder()`
  `int`
  `getPhase()`
  `boolean`
  `isAutoStartup()`
  `boolean`
  `isRunning()`
  `void`
  `onApplicationEvent(org.springframework.context.ApplicationEvent applicationEvent)`
  Utilizes `ApplicationEvent`s to determine the end and failure of a task.
  `void`
  `start()`
  `void`
  `stop()`
  `void`
  `stop(Runnable callback)`

  ### Methods inherited from class java.lang.Object

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

- ## Constructor Details

  - ### TaskLifecycleListener

    public TaskLifecycleListener(TaskRepository taskRepository, TaskNameResolver taskNameResolver, org.springframework.boot.ApplicationArguments applicationArguments, TaskExplorer taskExplorer, TaskProperties taskProperties, TaskListenerExecutorObjectFactory taskListenerExecutorObjectFactory)
    Parameters:
      `taskRepository` - `TaskRepository` to record executions.
      `taskNameResolver` - `TaskNameResolver` used to determine task name for task execution.
      `applicationArguments` - `ApplicationArguments` to be used for task execution.
      `taskExplorer` - `TaskExplorer` to be used for task execution.
      `taskProperties` - `TaskProperties` to be used for the task execution.
      `taskListenerExecutorObjectFactory` - `TaskListenerExecutorObjectFactory` to initialize TaskListenerExecutor for a task

- ## Method Details

  - ### onApplicationEvent

    public void onApplicationEvent(org.springframework.context.ApplicationEvent applicationEvent)
    Utilizes `ApplicationEvent`s 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`