本文介绍了Spring Batch - TaskletStep中的可跳过异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果发生某种异常,我试图让工作没有 BatchStatus.FAILED
。
I am trying to cause a job not to have BatchStatus.FAILED
if a certain exception occurs.
文档讨论在< chunk>
中使用 skippable-exception-classes
,但我怎样才能在a TaskletStep
?以下代码不起作用:
The docs talk about using skippable-exception-classes
within <chunk>
, but how can I do the same within a 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>
推荐答案
我在Tasklet中实现了这个功能,正如Michael Minella建议的那样:
I implemented this functionality in the Tasklet as Michael Minella suggested:
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;
}
}
示例中的Spring XML配置 SkippableTasklet
:
And the Spring XML configuration for an example SkippableTasklet
:
<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>
这篇关于Spring Batch - TaskletStep中的可跳过异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!