我是 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 作业中错误的好方法吗?我是否正确使用了监听器?
最佳答案
我们已经实现了 ItemReadListener
、ItemProcessListener
、ItemWriteListener
接口(interface),其中包含 onReadError
、onProcessError 等方法
,onWriteError
。如果在读取/处理/写入记录的过程中出现异常,就会调用这些方法。
关于java - 使用监听器捕获 Spring Batch 中步骤中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48216590/