springbatch自学之路-05(flow的创建与使用)

短命女 2023-02-13 11:30 231阅读 0赞

1.配置类

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

2.启动类

  1. package com.springbatch._03new_flow;
  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. }

3.执行结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTIwNDUwNDU_size_16_color_FFFFFF_t_70

发表评论

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

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

相关阅读