[英]Spring Batch - skippable exception in a TaskletStep
I am trying to cause a job not to have BatchStatus.FAILED
if a certain exception occurs. 如果发生某种异常,我试图让一个作业没有
BatchStatus.FAILED
。
The docs talk about using skippable-exception-classes
within <chunk>
, but how can I do the same within a TaskletStep
? 文档讨论了在
<chunk>
使用skippable-exception-classes
,但是如何在TaskletStep
做同样的TaskletStep
呢? The below code does not work: 以下代码不起作用:
<batch:step id="sendEmailStep">
<batch:tasklet>
<bean class="com.myproject.SendEmail" scope="step" autowire="byType">
<batch:skippable-exception-classes>
<batch:include class="org.springframework.mail.MailException" />
</batch:skippable-exception-classes>
</bean>
</batch:tasklet>
</batch:step>
I implemented this functionality in the Tasklet as Michael Minella suggested: 我在Tasklet中实现了这个功能,Michael Minella建议:
abstract class SkippableTasklet implements Tasklet {
//Exceptions that should not cause job status to be BatchStatus.FAILED
private List<Class<?>> skippableExceptions;
public void setSkippableExceptions(List<Class<?>> skippableExceptions) {
this.skippableExceptions = skippableExceptions;
}
private boolean isSkippable(Exception e) {
if (skippableExceptions == null) {
return false;
}
for (Class<?> c : skippableExceptions) {
if (e.getClass().isAssignableFrom(c)) {
return true;
}
}
return true;
}
protected abstract void run(JobParameters jobParameters) throws Exception;
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();
JobExecution jobExecution = stepExecution.getJobExecution();
JobParameters jobParameters = jobExecution.getJobParameters();
try {
run(prj);
} catch (Exception e) {
if (!isSkippable(e)) {
throw e;
} else {
jobExecution.addFailureException(e);
}
}
return RepeatStatus.FINISHED;
}
}
And the Spring XML configuration for an example SkippableTasklet
: 以及示例
SkippableTasklet
的Spring XML配置:
<batch:tasklet>
<bean class="com.MySkippableTasklet" scope="step" autowire="byType">
<property name="skippableExceptions">
<list>
<value>org.springframework.mail.MailException</value>
</list>
</property>
</bean>
</batch:tasklet>
Within a Tasklet
, the responsibility for exception handling is on the implementation of the Tasklet
. 在
Tasklet
,异常处理的责任在于Tasklet
的实现。 The skip logic available in chunk oriented processing is due to the exception handling provided by the ChunkOrientedTasklet
. 面向块的处理中可用的跳过逻辑是由
ChunkOrientedTasklet
提供的异常处理ChunkOrientedTasklet
。 If you want to skip exceptions in your own Tasklet
implementation, you need to write the code to do so in within your own implementation. 如果要在自己的
Tasklet
实现中跳过异常,则需要在自己的实现中编写代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.