springbatch自学之路-07(决策期decider的使用)

淡淡的烟草味﹌ 2023-02-14 07:57 81阅读 0赞

1.配置类

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

2.决策期

  1. package com.springbatch._05new_decider;
  2. import org.springframework.batch.core.JobExecution;
  3. import org.springframework.batch.core.StepExecution;
  4. import org.springframework.batch.core.job.flow.FlowExecutionStatus;
  5. import org.springframework.batch.core.job.flow.JobExecutionDecider;
  6. /**
  7. * @Package: com.springbatch._05new_decider
  8. * @ClassName: MyDecider
  9. * @author: zq
  10. * @since: 2020/6/1 21:25
  11. * @version: 1.0
  12. * @Copyright: 2020 zq. All rights reserved.
  13. */
  14. public class MyDecider implements JobExecutionDecider {
  15. int i = 0;
  16. @Override
  17. public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
  18. i++;
  19. if (i % 2 == 0) {
  20. return new FlowExecutionStatus("odd");
  21. } else {
  22. return new FlowExecutionStatus("even");
  23. }
  24. }
  25. }

3.启动类

  1. package com.springbatch._05new_decider;
  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 条评论,81人围观)

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

相关阅读