Configuring a JobLauncher
- Java
-
当使用
@EnableBatchProcessing
时,会为您提供一个JobRegistry
。本节介绍如何配置您自己的注册器。
When you use @EnableBatchProcessing
, a JobRegistry
is provided for you.
This section describes how to configure your own.
- XML
最基本的 JobLauncher
接口的实现是 TaskExecutorJobLauncher
。它唯一必需的依赖项是 JobRepository
(用于获取执行)。
The most basic implementation of the JobLauncher
interface is the TaskExecutorJobLauncher
.
Its only required dependency is a JobRepository
(needed to obtain an execution).
- Java
-
以下示例展示了 Java 中的
TaskExecutorJobLauncher
:
The following example shows a TaskExecutorJobLauncher
in Java:
...
@Bean
public JobLauncher jobLauncher() throws Exception {
TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
...
- XML
-
以下示例展示了 XML 中的
TaskExecutorJobLauncher
:
The following example shows a TaskExecutorJobLauncher
in XML:
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.TaskExecutorJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
获取 JobExecution 后,将其传递给 Job
的执行方法,最终将 JobExecution
返回给调用方,如下图所示:
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:
此顺序比较简单,当从调度器启动时运行良好。但是,当尝试从 HTTP 请求启动时会出现问题。在此场景中,启动需要异步完成,以便 TaskExecutorJobLauncher
立即返回给其调用者。这是因为对于长时间运行的进程(例如批处理作业)所需的时长将 HTTP 请求保持为开启状态并非一项良好的操作。以下图片显示了一个示例顺序:
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 TaskExecutorJobLauncher
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:
您可以通过配置一个 TaskExecutor
来配置 TaskExecutorJobLauncher
以允许这种情况发生。
You can configure the TaskExecutorJobLauncher
to allow for this scenario by configuring a
TaskExecutor
.
- Java
-
以下 Java 示例配置了一个
TaskExecutorJobLauncher
以立即返回:
The following Java example configures a TaskExecutorJobLauncher
to return immediately:
@Bean
public JobLauncher jobLauncher() {
TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
jobLauncher.setJobRepository(jobRepository());
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
- XML
-
以下 XML 示例配置了一个
TaskExecutorJobLauncher
以立即返回:
The following XML example configures a TaskExecutorJobLauncher
to return immediately:
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.TaskExecutorJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
您可以使用 Spring TaskExecutor
接口的任何实现来控制以异步方式对作业执行。
You can use any implementation of the spring TaskExecutor
interface to control how jobs are asynchronously
executed.