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 only requires two dependencies: a JobRepository and a JobRegistry.

  • Java

  • XML

The following example shows a TaskExecutorJobLauncher in Java:

Java Configuration
...
@Bean
public JobOperator jobOperator(JobRepository jobRepository, JobRegistry jobRegistry) throws Exception {
	TaskExecutorJobJobOperator jobOperator = new TaskExecutorJobOperator();
	jobOperator.setJobRepository(jobRepository);
	jobOperator.setJobRegistry(jobRegistry);
	jobOperator.afterPropertiesSet();
	return jobOperator;
}
...

The following example shows a TaskExecutorJobOperator in XML:

XML Configuration
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.TaskExecutorJobOperator">
    <property name="jobRepository" ref="jobRepository" />
    <property name="jobRegistry" ref="jobRegistry" />
</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:

Job Launcher Sequence
Figure 1. Job Launcher Sequence

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:

Async Job Launcher Sequence
Figure 2. Asynchronous Job Launcher 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:

Java Configuration
@Bean
public JobOperator jobOperator(JobRepository jobRepository, JobRegistry jobRegistry) throws Exception {
	TaskExecutorJobJobOperator jobOperator = new TaskExecutorJobOperator();
	jobOperator.setJobRepository(jobRepository);
	jobOperator.setJobRegistry(jobRegistry);
	jobOperator.setTaskExecutor(new SimpleAsyncTaskExecutor());
	jobOperator.afterPropertiesSet();
	return jobOperator;
}

The following XML example configures a TaskExecutorJobOperator to return immediately:

XML Configuration
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.TaskExecutorJobOperator">
    <property name="jobRepository" ref="jobRepository" />
    <property name="jobRegistry" ref="jobRegistry" />
    <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.