This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Batch Documentation 5.2.2! |
Configuring a JobOperator
The most basic implementation of the JobOperator
interface is the TaskExecutorJobOperator
.
It requires only one dependency: a JobRepository
. All other dependencies like JobRegistry
,
MeterRegistry
, TransactionManager
, etc are optional. Spring Batch provides a factory bean
to simplify the configuration of this operator: JobOperatorFactoryBean
. This factory bean
creates a transactional proxy around the TaskExecutorJobOperator
to ensure that all its public methods
are executed within a transaction.
-
Java
-
XML
The following example shows how to configure a TaskExecutorJobOperator
in Java:
...
@Bean
public JobOperatorFactoryBean jobOperator(JobRepository jobRepository) {
JobOperatorFactoryBean jobOperatorFactoryBean = new JobOperatorFactoryBean();
jobOperator.setJobRepository(jobRepository);
return jobOperatorFactoryBean;
}
...
The following example shows how to configure a TaskExecutorJobOperator
in XML:
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.JobOperatorFactoryBean">
<property name="jobRepository" ref="jobRepository" />
</bean>
Once a JobExecution is obtained, it is passed to the
execute method of Job
, ultimately returning the JobExecution
to the caller, as
the following image shows:

The sequence is straightforward and works well when launched from a scheduler. However,
issues arise when trying to launch from an HTTP request. In this scenario, the launching
needs to be done asynchronously so that the TaskExecutorJobOperator
returns immediately to its
caller. This is because it is not good practice to keep an HTTP request open for the
amount of time needed by long running processes (such as batch jobs). The following image shows
an example sequence:

You can configure the TaskExecutorJobOperator
to allow for this scenario by configuring a
TaskExecutor
.
-
Java
-
XML
The following Java example configures a TaskExecutorJobOperator
to return immediately:
@Bean
public JobOperatorFactoryBean jobOperator(JobRepository jobRepository) {
JobOperatorFactoryBean jobOperatorFactoryBean = new JobOperatorFactoryBean();
jobOperator.setJobRepository(jobRepository);
jobOperator.setTaskExecutor(new SimpleAsyncTaskExecutor());
return jobOperatorFactoryBean;
}
The following XML example configures a TaskExecutorJobOperator
to return immediately:
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.JobOperatorFactoryBean">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
You can use any implementation of the Spring TaskExecutor
interface to control how jobs are asynchronously
executed.