【发布时间】:2021-09-07 10:47:00
【问题描述】:
public class TaskletConfiguration {
...
@Bean
public Step step() {
return steps.get("step")
.tasklet(tasklet)
.exceptionHandler(logExceptionHandler()) // Handler for logging exception to specific channel
.build();
}
@Bean
public Job job() {
return jobs.get("job")
.start(step())
.build();
}
}
public class ExampleTasklet implements Tasklet, StepExecutionListener {
...
@Override
public RepeatStatus execute(...) throws Exception {
// Do my tasklet
// Throw if it fails, and be handled by logExceptionHandler()
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
// Want to throw so that logExceptionHandler() can handle it as throwing in execute().
throwable_function();
}
}
这是我在 Spring Boot 中使用 tasklet 的示例代码。
我的问题是:我想从afterstep() 抛出异常,但是接口不允许。
尽管有这个限制,我为什么痴迷afterstep() 是因为我想制作抽象类来制作可以验证afterstep() 中的每个执行的Tasklet 模板。我希望在完成所有execute() 之后运行验证,这将被子类覆盖。所以我别无选择,只能使用afterstep()。
在每个execute() 与 throwable 或afterstep() 之后运行验证方法的任何想法都可以将异常传递给logExceptionHandler()?我希望在TaskletConfiguration 类中定义logExceptionHandler()。如果在Tasklet类中定义的话会很胖,因为我会做抽象类,它会被很多子类继承。
【问题讨论】:
标签: java exception spring-batch spring-batch-tasklet