springbatch自学之路-09(监听器)

超、凢脫俗 2023-02-17 14:55 130阅读 0赞

目录

1.启动类

2.Job监听器

3.chunk监听器

4.配置类


1.启动类

  1. package com.springbatch._07listener;
  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.Job监听器

  1. package com.springbatch._07listener;
  2. import org.springframework.batch.core.JobExecution;
  3. import org.springframework.batch.core.JobExecutionListener;
  4. /**
  5. * Job监听器
  6. *
  7. * @Package: com.springbatch._07listener
  8. * @ClassName: MyJobListener
  9. * @author: zq
  10. * @since: 2020/6/14 18:57
  11. * @version: 1.0
  12. * @Copyright: 2020 zq. All rights reserved.
  13. */
  14. public class MyJobListener implements JobExecutionListener {
  15. @Override
  16. public void beforeJob(JobExecution jobExecution) {
  17. System.out.println(jobExecution.getJobInstance().getJobName() + "before...");
  18. }
  19. @Override
  20. public void afterJob(JobExecution jobExecution) {
  21. System.out.println(jobExecution.getJobInstance().getJobName() + "after...");
  22. }
  23. }

3.chunk监听器

  1. package com.springbatch._07listener;
  2. import org.springframework.batch.core.annotation.AfterChunk;
  3. import org.springframework.batch.core.annotation.BeforeChunk;
  4. import org.springframework.batch.core.scope.context.ChunkContext;
  5. /**
  6. * Trunck监听器
  7. *
  8. * @Package: com.springbatch._07listener
  9. * @ClassName: MyChunkListener
  10. * @author: zq
  11. * @since: 2020/6/14 18:58
  12. * @version: 1.0
  13. * @Copyright: 2020 zq. All rights reserved.
  14. */
  15. public class MyChunkListener {
  16. @BeforeChunk
  17. public void before(ChunkContext chunkContext) {
  18. System.out.println(chunkContext.getStepContext().getStepName() + "before===");
  19. }
  20. @AfterChunk
  21. public void after(ChunkContext chunkContext) {
  22. System.out.println(chunkContext.getStepContext().getStepName() + "after===");
  23. }
  24. }

4.配置类

  1. package com.springbatch._07listener;
  2. import org.springframework.batch.core.Job;
  3. import org.springframework.batch.core.Step;
  4. import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
  5. import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
  6. import org.springframework.batch.item.ItemReader;
  7. import org.springframework.batch.item.ItemWriter;
  8. import org.springframework.batch.item.support.ListItemReader;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import java.util.Arrays;
  13. import java.util.List;
  14. @Configuration
  15. public class ListenerJobConfig {
  16. //创建 创建job的对象
  17. @Autowired
  18. private JobBuilderFactory jobBuilderFactory;
  19. //创建 创建step的对象
  20. @Autowired
  21. private StepBuilderFactory stepBuilderFactory;
  22. @Bean
  23. public Job listenerJob() {
  24. return jobBuilderFactory.get("listenerJob")
  25. .start(myChunkStep())
  26. .listener(new MyJobListener())
  27. .build();
  28. }
  29. @Bean
  30. public Step myChunkStep() {
  31. return stepBuilderFactory.get("myChunkStep")
  32. .<String, String>chunk(2)
  33. .faultTolerant()
  34. .listener(new MyChunkListener())
  35. .reader(read())
  36. .writer(write())
  37. .build();
  38. }
  39. @Bean
  40. public ItemWriter<String> write() {
  41. return new ItemWriter() {
  42. @Override
  43. public void write(List list) throws Exception {
  44. for (Object o : list) {
  45. System.out.println(o);
  46. }
  47. }
  48. };
  49. }
  50. @Bean
  51. public ItemReader<String> read() {
  52. return new ListItemReader<>(Arrays.asList("java", "c", "c++"));
  53. }
  54. }

5.执行结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTIwNDUwNDU_size_16_color_FFFFFF_t_70

发表评论

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

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

相关阅读