Spring Cloud Task provides an out of the box configuration as defined in the
DefaultTaskConfigurer
and SimpleTaskConfiguration
. This section will walk through
the defaults as well as how to customize Spring Cloud Task for your needs
Spring Cloud Task utilizes a datasource for storing the results of task executions. By
default, we provide an in memory instance of H2 to provide a simple method of
bootstrapping development. However, in a production environment, you’ll want to configure
your own DataSource
.
If your application utilizes only a single DataSource
and that will serve as both your
business schema as well as the task repository, all you need to do is provide any
DataSource
(via Spring Boot’s configuration conventions is the easiest way). This will
be automatically used by Spring Cloud Task for the repository.
If your application utilizes more than one DataSource
, you’ll need to configure the
task repository with the appropriate DataSource
. This customization can be done via an
implementation of the TaskConfigurer
.
The TaskConfigurer
is a strategy interface allowing for users to customize the way
components of Spring Cloud Task are configured. By default, we provide the
DefaultTaskConfigurer
that provides logical defaults (Map
based in memory components
useful for development if no DataSource
is provided and JDBC based components if there
is a DataSource
available.
The TaskConfigurer
allows the configuration of three main components:
Component | Description | Default (provided by DefaultTaskConfigurer ) |
---|---|---|
| The implementation of the |
|
| The implementation of the |
|
| A transaction manager to be used when executing updates for tasks. |
|
In most cases, the name of the task will be the application name as configured via Spring
Boot. However, there are some cases, where you may want to map the run of a task to a
different name. Spring Data Flow is an example of this (where you want the task to be run
with the name of the task definition). Because of this, we offer the ability to customize
how the task is named via the TaskNameResolver
interface.
By default, Spring Cloud Task provides the SimpleTaskNameResolver
which will use the
following options (in order of precedence):
spring.cloud.task.name
.ApplicationContext#getId
).Allows a user to register listeners for specific events that occur during the task
lifecycle. This is done by creating a class that implements the TaskExecutionListener
interface. The class that implements the TaskExecutionListener
interface will be
notified for the following events:
onTaskStartup
- prior to the storing the TaskExecution
into the TaskRepository
onTaskEnd
- prior to the updating of the TaskExecution
entry in the TaskRepository
marking the final state of the task.onTaskFailed
- prior to the onTaskEnd
method being invoked when an unhandled
exception is thrown by the task.Spring Cloud Task also allows a user add TaskExecution
Listeners to methods within a bean
by using the following method annotations:
@BeforeTask
- prior to the storing the TaskExecution
into the TaskRepository
@AfterTask
- prior to the updating of the TaskExecution
entry in the TaskRepository
marking the final state of the task.@FailedTask
- prior to the @AfterTask
method being invoked when an unhandled
exception is thrown by the task.public class MyBean { @BeforeTask public void methodA(TaskExecution taskExecution) { } @AfterTask public void methodB(TaskExecution taskExecution) { } @FailedTask public void methodC(TaskExecution taskExecution, Throwable throwable) { } }