【发布时间】:2012-02-07 10:23:06
【问题描述】:
我有一个春季批处理程序。
跳过限制设置为 5,块大小为 1000。
我的工作有两个步骤:
<step id="myFileGenerator" next="myReportGenerator">
<tasklet transaction-manager="jobRepository-transactionManager">
<chunk reader="myItemReader" processor="myItemProcessor" writer="myItemWriter" commit-interval="1000" skip-policy="skipPolicy"/>
</tasklet>
<listeners>
<listener ref="mySkipListener"/>
</listeners>
</step>
<step id="myReportGenerator">
<tasklet ref="myReportTasklet" transaction-manager="jobRepository-transactionManager"/>
</step>
跳过策略如下:
<beans:bean id="skipPolicy" class="com.myPackage.util.Skip_Policy">
<beans:property name="skipLimit" value="5"/>
</beans:bean>
SkipPolicy 类如下:
public class Skip_Policy implements SkipPolicy {
private int skipLimit;
public void setSkipLimit(final int skipLimit) {
this.skipLimit = skipLimit;
}
public boolean shouldSkip(final Throwable t, final int skipCount) throws SkipLimitExceededException {
if (skipCount < this.skipLimit) {
return true;
}
return false;
}
}
因此,对于在达到跳过限制之前发生的任何错误,跳过策略将忽略该错误(返回 true)。达到跳过限制后,作业将因任何错误而失败。
mySkipListener 类如下:
public class mySkipListener implements SkipListener<MyItem, MyItem> {
public void onSkipInProcess(final MyItem item, final Throwable t) {
// TODO Auto-generated method stub
System.out.println("Skipped details during PROCESS is: " + t.getMessage());
}
public void onSkipInRead(final Throwable t) {
System.out.println("Skipped details during READ is: " + t.getMessage());
}
public void onSkipInWrite(final MyItem item, final Throwable t) {
// TODO Auto-generated method stub
System.out.println("Skipped details during WRITE is: " + t.getMessage());
}
}
现在在 myItemProcessor 我有以下代码块:
if (item.getTheNumber().charAt(4) == '-') {
item.setProductNumber(item.getTheNumber().substring(0, 3));
} else {
item.setProductNumber("55");
}
对于某些项目,Number 字段为空,因此上面的代码块会引发“StringIndexOutofBounds”异常。
但是我看到了一种奇怪的行为,我不明白为什么会这样。
总共有 6 个项目有错误,即 theNumber 字段为空。
如果跳过限制超过错误数量(即 > 6),则跳过侦听器类中的 sys 输出将被调用并报告跳过的错误。
但是,如果跳过限制较少(例如我的示例中的 5),则跳过侦听器类中的 sys 输出根本不会被调用,我会直接在控制台上获得以下异常转储:
org.springframework.batch.retry.RetryException: Non-skippable exception in recoverer while processing; nested exception is java.lang.StringIndexOutOfBoundsException
at org.springframework.batch.core.step.item.FaultTolerantChunkProcessor$2.recover(FaultTolerantChunkProcessor.java:282)
at org.springframework.batch.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:416)
at org.springframework.batch.retry.support.RetryTemplate.doExecute(RetryTemplate.java:285)
at org.springframework.batch.retry.support.RetryTemplate.execute(RetryTemplate.java:187)
这种行为背后的原因是什么?我应该怎么做才能解决这个问题?
感谢阅读!
【问题讨论】:
标签: spring-batch