英文:
Spring batch: get exitStatusDescription in afterJob
问题
我对Spring Batch还不熟悉。
我已经使用这个作业配置了一个新项目:
@Configuration
@EnableBatchProcessing
@Log4j2
public class BatchConfiguration {
/* ... */
@Bean
public Job myJob(JobBuilderFactory jobBuilderFactory, Step stepInit, Step stepMain, Step stepReport) {
return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer()).start(stepInit).on(ExitStatus.FAILED.getExitCode()).fail().from(stepInit).on("*").to(stepMain).next(stepReport).end().listener(new BatchListener()).build();
}
@Bean
public Step stepInit(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("stepInit").tasklet(BatchInitialize.builder().build()).build();
}
/* ... */
}
在BatchInitialize的tasklet中,我已经定义了一个带有错误描述的退出状态:
@Builder
@AllArgsConstructor
@Log4j2
public class BatchInitialize implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
/* ... */
if (CONDITION) {
contribution.setExitStatus(new ExitStatus(ExitStatus.FAILED.getExitCode(), "ERROR DESC"));
return RepeatStatus.FINISHED;
}
/* ... */
}
}
在实现JobExecutionListener的类中,我如何获取错误描述?
@Builder
@AllArgsConstructor
@Log4j2
public class BatchListener implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
//
}
@Override
public void afterJob(JobExecution jobExecution) {
// jobExecution.getExitStatus().getExitCode() -> FAILED
// jobExecution.getExitStatus().getExitDescription() -> ""
}
}
英文:
I am new to Spring batch.
I've configurated a new project with this job:
@Configuration
@EnableBatchProcessing
@Log4j2
public class BatchConfiguration {
/* ... */
@Bean
public Job myJob(JobBuilderFactory jobBuilderFactory, Step stepInit, Step stepMain, Step stepReport) {
return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer()).start(stepInit).on(ExitStatus.FAILED.getExitCode()).fail().from(stepInit).on("*").to(stepMain).next(stepReport).end().listener(new BatchListener()).build();
}
@Bean
public Step stepInit(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("stepInit").tasklet(BatchInitialize.builder().build()).build();
}
/* ... */
}
In BatchInitialize tasklet I've defined an exit status with errorDesc:
@Builder
@AllArgsConstructor
@Log4j2
public class BatchInitialize implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
/* ... */
if(CONDITION) {
contribution.setExitStatus(new ExitStatus(ExitStatus.FAILED.getExitCode(), "ERROR DESC"));
return RepeatStatus.FINISHED;
}
/* ... */
}
}
How can I get the error description in a class that implements JobExecutionListener?
@Builder
@AllArgsConstructor
@Log4j2
public class BatchListener implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
//
}
@Override
public void afterJob(JobExecution jobExecution) {
// jobExecution.getExitStatus().getExitCode() -> FAILED
// jobExecution.getExitStatus().getExitDescription() -> ""
}
}
答案1
得分: 2
将步骤的退出状态设置为FAILED
并返回RepeatStatus.FINISHED
对我来说没有意义。如果任务失败,我会设置其退出状态并抛出异常(另一种选项是使用异常的消息并将其设置为ERROR DESC
)。
现在,如果您想在监听器中获取异常,可以这样做:
@Override
public void afterJob(JobExecution jobExecution) {
List<Throwable> allExceptions = jobExecution.getAllFailureExceptions();
// 获取异常及其消息
}
如果您想获取特定步骤的退出状态,可以这样做:
@Override
public void afterJob(JobExecution jobExecution) {
List<StepExecution> stepExecutions = jobExecution.getStepExectuions();
// 遍历并获取您想要的步骤执行,然后获取其退出状态
}
英文:
Setting the exit status of the step to FAILED
and returning RepeatStatus.FINISHED
does not make sense to me. If the tasklet failed, I would set its exit status and throw an exception instead (another option is to use the exception's message and set it to ERROR DESC
).
Now if you want to get the exception in the listener, you can do something like:
@Override
public void afterJob(JobExecution jobExecution) {
List<Throwable> allExceptions = jobExecution.getAllFailureExceptions();
// get the exception and its message
}
If you want to get the exist status of a particular step, you can do something like:
@Override
public void afterJob(JobExecution jobExecution) {
List<StepExecution> stepExecutions = jobExecution.getStepExectuions();
// iterate and get the step execution you want then get its exit status
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论