springbatch自学之路-04(job的创建与使用)

蔚落 2023-02-13 11:26 120阅读 0赞

目录

1.使用start。。。next。。。的方式

2.使用from。。。on。。。to。。。的方式


1.使用start。。。next。。。的方式

直接上代码:

配置类:

  1. package com.springbatch._02new_job;
  2. import org.springframework.batch.core.Job;
  3. import org.springframework.batch.core.Step;
  4. import org.springframework.batch.core.StepContribution;
  5. import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
  6. import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
  7. import org.springframework.batch.core.scope.context.ChunkContext;
  8. import org.springframework.batch.core.step.tasklet.Tasklet;
  9. import org.springframework.batch.repeat.RepeatStatus;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. /**
  14. * 任务配置器
  15. *
  16. * @Package: com.springbatch._01helloworld
  17. * @ClassName: jobConfig
  18. * @author: zq
  19. * @since: 2020/5/22 21:00
  20. * @version: 1.0
  21. * @Copyright: 2020 zq. All rights reserved.
  22. */
  23. @Configuration
  24. public class JobConfig {
  25. //创建 创建job的对象
  26. @Autowired
  27. private JobBuilderFactory jobBuilderFactory;
  28. //创建 创建step的对象
  29. @Autowired
  30. private StepBuilderFactory stepBuilderFactory;
  31. @Bean
  32. public Job newJob() {
  33. return jobBuilderFactory.get("newJob")
  34. .start(step1())
  35. .next(step2())
  36. .next(step3())
  37. .build();
  38. }
  39. @Bean
  40. public Step step3() {
  41. return stepBuilderFactory.get("step3")
  42. .tasklet(new Tasklet() {
  43. @Override
  44. public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
  45. System.out.println("step3");
  46. return RepeatStatus.FINISHED;
  47. }
  48. }).build();
  49. }
  50. @Bean
  51. public Step step2() {
  52. return stepBuilderFactory.get("step2")
  53. .tasklet(new Tasklet() {
  54. @Override
  55. public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
  56. System.out.println("step2");
  57. return RepeatStatus.FINISHED;
  58. }
  59. }).build();
  60. }
  61. @Bean
  62. public Step step1() {
  63. return stepBuilderFactory.get("step1")
  64. .tasklet(new Tasklet() {
  65. @Override
  66. public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
  67. System.out.println("step1");
  68. return RepeatStatus.FINISHED;
  69. }
  70. }).build();
  71. }
  72. }

启动类

  1. package com.springbatch._02new_job;
  2. import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. /**
  6. * SpringBatchDemo项目启动类
  7. *
  8. * @Package: PACKAGE_NAME
  9. * @ClassName: com.springbatch._01helloworld.SpringBatchConfig
  10. * @author: zq
  11. * @since: 2020/5/22 20:31
  12. * @version: 1.0
  13. * @Copyright: 2020 zq. All rights reserved.
  14. */
  15. @SpringBootApplication
  16. @EnableBatchProcessing
  17. public class SpringBatchConfig {
  18. public static void main(String[] args) {
  19. SpringApplication.run(SpringBatchConfig.class, args);
  20. }
  21. }

2.使用from。。。on。。。to。。。的方式

配置类

  1. package com.springbatch._02new_job;
  2. import org.springframework.batch.core.Job;
  3. import org.springframework.batch.core.Step;
  4. import org.springframework.batch.core.StepContribution;
  5. import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
  6. import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
  7. import org.springframework.batch.core.scope.context.ChunkContext;
  8. import org.springframework.batch.core.step.tasklet.Tasklet;
  9. import org.springframework.batch.repeat.RepeatStatus;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. /**
  14. * 任务配置器
  15. *
  16. * @Package: com.springbatch._01helloworld
  17. * @ClassName: jobConfig
  18. * @author: zq
  19. * @since: 2020/5/22 21:00
  20. * @version: 1.0
  21. * @Copyright: 2020 zq. All rights reserved.
  22. */
  23. @Configuration
  24. public class JobConfig {
  25. //创建 创建job的对象
  26. @Autowired
  27. private JobBuilderFactory jobBuilderFactory;
  28. //创建 创建step的对象
  29. @Autowired
  30. private StepBuilderFactory stepBuilderFactory;
  31. @Bean
  32. public Job newJob2() {
  33. return jobBuilderFactory.get("newJob2")
  34. .start(step1())
  35. .on("COMPLETED").to(step2())
  36. .from(step2()).on("COMPLETED").to(step3())
  37. .from(step3()).end()
  38. .build();
  39. }
  40. @Bean
  41. public Step step3() {
  42. return stepBuilderFactory.get("step3")
  43. .tasklet(new Tasklet() {
  44. @Override
  45. public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
  46. System.out.println("step3");
  47. return RepeatStatus.FINISHED;
  48. }
  49. }).build();
  50. }
  51. @Bean
  52. public Step step2() {
  53. return stepBuilderFactory.get("step2")
  54. .tasklet(new Tasklet() {
  55. @Override
  56. public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
  57. System.out.println("step2");
  58. return RepeatStatus.FINISHED;
  59. }
  60. }).build();
  61. }
  62. @Bean
  63. public Step step1() {
  64. return stepBuilderFactory.get("step1")
  65. .tasklet(new Tasklet() {
  66. @Override
  67. public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
  68. System.out.println("step1");
  69. return RepeatStatus.FINISHED;
  70. }
  71. }).build();
  72. }
  73. }

启动类

  1. package com.springbatch._02new_job;
  2. import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. /**
  6. * SpringBatchDemo项目启动类
  7. *
  8. * @Package: PACKAGE_NAME
  9. * @ClassName: com.springbatch._01helloworld.SpringBatchConfig
  10. * @author: zq
  11. * @since: 2020/5/22 20:31
  12. * @version: 1.0
  13. * @Copyright: 2020 zq. All rights reserved.
  14. */
  15. @SpringBootApplication
  16. @EnableBatchProcessing
  17. public class SpringBatchConfig {
  18. public static void main(String[] args) {
  19. SpringApplication.run(SpringBatchConfig.class, args);
  20. }
  21. }

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读