# 使用 Spring `TaskScheduler`进行 Spring Batch 作业调度

> 原文： [https://howtodoinjava.com/spring-batch/job-scheduler-example/](https://howtodoinjava.com/spring-batch/job-scheduler-example/)

在企业应用程序中，您将需要将 **cron 表达式**传递给 Spring `TaskScheduler`，在固定的时间表上定期执行 Spring Batch 作业。 在此示例中，我们将使用 spring 的内置调度功能执行[示例 Spring Batch 作业](https://howtodoinjava.com/spring-batch/java-config-multiple-steps/)。

## 配置批处理作业调度程序

要配置，批处理作业调度分两个步骤完成：

1.  启用带有[`@EnableScheduling`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html)注解的调度。
2.  创建带有[`@Scheduled`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html)注解的方法，并使用 cron 作业提供重复详细信息。 在此方法内添加作业执行逻辑。

`Cron scheduling on spring batch job`

```java
package com.howtodoinjava.demo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class App
{
	@Autowired
	JobLauncher jobLauncher;

	@Autowired
	Job job;

	public static void main(String[] args) 
	{
		SpringApplication.run(App.class, args);
	}

	@Scheduled(cron = "0 */1 * * * ?")
    public void perform() throws Exception 
	{
		JobParameters params = new JobParametersBuilder()
				.addString("JobID", String.valueOf(System.currentTimeMillis()))
				.toJobParameters();
		jobLauncher.run(job, params);
	}
}

```

批处理作业将在应用程序启动后每隔一分钟运行一次。

> 要引用作业和任务源代码，请阅读[ Spring batch Java 配置示例](https://howtodoinjava.com/spring-batch/java-config-multiple-steps/)。

## 示例

现在，如果您运行该应用程序并验证日志，您将看到该作业每分钟都在运行。

`Console`

```java
2018-07-04 15:57:00.073  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1530700020003}]

2018-07-04 15:57:00.097  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepOne]

MyTaskOne start..
MyTaskOne done..

2018-07-04 15:57:00.118  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepTwo]

MyTaskTwo start..
MyTaskTwo done..

2018-07-04 15:57:00.125  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1530700020003}] 
and the following status: [COMPLETED]

2018-07-04 15:58:00.007  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1530700080002}]

2018-07-04 15:58:00.011  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepOne]

MyTaskOne start..
MyTaskOne done..

2018-07-04 15:58:00.021  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepTwo]

MyTaskTwo start..
MyTaskTwo done..

2018-07-04 15:58:00.029  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1530700080002}] 
and the following status: [COMPLETED]

```

将我的问题放在评论部分。

学习愉快！