gpt4 book ai didi

java - 使用监听器捕获 Spring Batch 中步骤中的错误

转载 作者:行者123 更新时间:2023-12-02 11:45:44 25 4
gpt4 key购买 nike

我是 Spring 和 Spring Batch 的新手。我写了一个基本的工作,应该每 5 秒重复一次。它有两个步骤,第一个步骤 (step1) 每次都会失败。我的目的是查看作业是否会在 step1 中报告这些错误并继续到 step2。我用来捕获 step1 中的错误的方法如下(使用 listener)。我想要一些关于我的方法正确/错误的批评。

这是我的工作配置。它的一项工作有两个步骤:

@Configuration
public class JobConfig {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
private JobLauncher jobLauncher;

@Autowired
private JobRepository jobRepository;

@Autowired
private StepExecutionListenerImpl stepExecutionListener;

@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.listener(this.stepExecutionListener)
.tasklet(new Tasklet() throws Exception {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
throw new Exception("Step1 caused an exception");
return RepeatStatus.FINISHED;
}
})
.build();
}

@Bean
public Step step2() {
return stepBuilderFactory.get("step2")
.tasklet(new Tasklet() {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws InterruptedException {
System.out.println("Step2 is executing");
return RepeatStatus.FINISHED;
}
})
.build();
}

@Bean
public Job job() throws Exception {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.start(step1())
.on("COMPLETED").to(step2()).end()
.build();
}

@Scheduled(cron = "*/5 * * * * *")
public void runJob() throws Exception {

System.out.println("Job Started at :" + new Date());

JobParameters param = new JobParametersBuilder().addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();

JobExecution execution = jobLauncher.run(job(), param);

System.out.println("Job finished with status :" + execution.getStatus());
}

@Bean
public JobLauncher getJobLauncher() {
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(this.jobRepository);
return simpleJobLauncher;
}
}

为了在step1中导致错误,我在该步骤中引发了异常。我还在 step1 中添加了一个 listener,其 afterStep() 方法检查该步骤中是否发生任何异常,如果有,则返回 ExitStatus.FAILED。如果没有发生异常,则返回ExitStatus.COMPLETED。下面是代码:

@Component
public class StepExecutionListenerImpl implements StepExecutionListener {

@Override
public void beforeStep(StepExecution stepExecution) {
System.out.println("Before starting step");
}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
List<Throwable> exceptions = stepExecution.getFailureExceptions();
if(exceptions.isEmpty()) {
return ExitStatus.COMPLETED;
} else {
System.out.println("This step has encountered exceptions");
exceptions.forEach(th -> System.out.println("Exception has occurred in job"));
return ExitStatus.FAILED;
}
}
}

这似乎有效,因为 step1 在作业的每次迭代中都失败,然后开始新的迭代。所以我的问题是,这是捕获 Spring Batch 作业中错误的好方法吗?我是否正确使用了监听器?

最佳答案

我们已经实现了 ItemReadListenerItemProcessListenerItemWriteListener 接口(interface),其中包含 onReadErroronProcessError 等方法,onWriteError。如果在读取/处理/写入记录的过程中出现异常,就会调用这些方法。

关于java - 使用监听器捕获 Spring Batch 中步骤中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48216590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com