<job id="pullPurgeProcessStoreFiles" xmlns="http://www.springframework.org/schema/batch">
<bean id="PullFilesTasklet" class="com.example.PullFilesTasklet" />
<step id="pullFiles" next="validation" >
<tasklet ref="PullFilesTasklet">
<skippable-exception-classes>
<include class="java.io.FileNotFoundException"/>
</skippable-exception-classes>
</tasklet>
</step>
</job>
出现以下错误:
发现从元素 skippable-exception-classes
开始的无效内容。
在研究中,我发现 skippable-exception-classes
可以在 block 中使用。但我需要使用 ref tasklet 来实现相同的目标。
最佳答案
如果您想在自己的 Tasklet
实现中Skip Exception
,则需要在您自己的 Tasklet
中编写代码来执行此操作实现。
关注此原创thread如果此解决方案适合您,请在原始帖子上投票。
你可以做类似的事情
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;
}
}
并在 SpringXML 配置中
<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>
关于java - 在 Tasklet 实现上添加可跳过的异常类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206873/