原文: https://howtodoinjava.com/spring-batch/job-scheduler-example/

在企业应用程序中,您将需要将 cron 表达式传递给 Spring TaskScheduler,在固定的时间表上定期执行 Spring Batch 作业。 在此示例中,我们将使用 spring 的内置调度功能执行示例 Spring Batch 作业

配置批处理作业调度程序

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

  1. 启用带有@EnableScheduling注解的调度。
  2. 创建带有@Scheduled注解的方法,并使用 cron 作业提供重复详细信息。 在此方法内添加作业执行逻辑。

Cron scheduling on spring batch job

  1. package com.howtodoinjava.demo;
  2. import org.springframework.batch.core.Job;
  3. import org.springframework.batch.core.JobParameters;
  4. import org.springframework.batch.core.JobParametersBuilder;
  5. import org.springframework.batch.core.launch.JobLauncher;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.SpringApplication;
  8. import org.springframework.boot.autoconfigure.SpringBootApplication;
  9. import org.springframework.scheduling.annotation.EnableScheduling;
  10. import org.springframework.scheduling.annotation.Scheduled;
  11. @SpringBootApplication
  12. @EnableScheduling
  13. public class App
  14. {
  15. @Autowired
  16. JobLauncher jobLauncher;
  17. @Autowired
  18. Job job;
  19. public static void main(String[] args)
  20. {
  21. SpringApplication.run(App.class, args);
  22. }
  23. @Scheduled(cron = "0 */1 * * * ?")
  24. public void perform() throws Exception
  25. {
  26. JobParameters params = new JobParametersBuilder()
  27. .addString("JobID", String.valueOf(System.currentTimeMillis()))
  28. .toJobParameters();
  29. jobLauncher.run(job, params);
  30. }
  31. }

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

要引用作业和任务源代码,请阅读 Spring batch Java 配置示例

示例

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

Console

  1. 2018-07-04 15:57:00.073 INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher
  2. : Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1530700020003}]
  3. 2018-07-04 15:57:00.097 INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler
  4. : Executing step: [stepOne]
  5. MyTaskOne start..
  6. MyTaskOne done..
  7. 2018-07-04 15:57:00.118 INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler
  8. : Executing step: [stepTwo]
  9. MyTaskTwo start..
  10. MyTaskTwo done..
  11. 2018-07-04 15:57:00.125 INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher
  12. : Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1530700020003}]
  13. and the following status: [COMPLETED]
  14. 2018-07-04 15:58:00.007 INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher
  15. : Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1530700080002}]
  16. 2018-07-04 15:58:00.011 INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler
  17. : Executing step: [stepOne]
  18. MyTaskOne start..
  19. MyTaskOne done..
  20. 2018-07-04 15:58:00.021 INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler
  21. : Executing step: [stepTwo]
  22. MyTaskTwo start..
  23. MyTaskTwo done..
  24. 2018-07-04 15:58:00.029 INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher
  25. : Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1530700080002}]
  26. and the following status: [COMPLETED]

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

学习愉快!