【问题标题】:strange behavior in spring batch about skip policy implementation春季批次中有关跳过策略实施的奇怪行为
【发布时间】: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


    【解决方案1】:

    如果包含它的 tasklet 正常完成,则 SkipListener 仅在 Chunk 的末尾使用。当您有超过跳过限制的错误时,通过您看到的异常报告,并且 tasklet 被中止。

    如果错误数小于跳过限制,则 tasklet 正常完成,并且为每个跳过的行或项目调用一次 SkipListener - Spring Batch 在内部构建它们的列表,但只报告结束。

    这样的想法是,如果任务失败,您可能会重试它,因此知道在不完整的运行期间跳过了什么是没有用的,每次重试时都会收到相同的通知。只有当其他一切都成功时,你才能看到被跳过的内容。想象您正在记录跳过的项目,您不希望它们被一遍又一遍地记录为跳过。

    如您所见,简单的解决方案是使跳过限制足够大。同样的想法是,如果您必须跳过很多项目,可能会出现更严重的问题。

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多