我有一個spring批處理程序。關於跳過策略執行的一批奇怪行爲
跳躍限制設置爲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");
}
對於一些項目數量寫字段爲空的,因此上面的代碼塊拋出「 StringIndexOutofBounds「異常。
但我看到一個奇怪的行爲,我不明白它爲什麼會發生。
總共有6個項目有錯誤,即Number字段爲空。
如果跳過限制大於錯誤數量(即> 6),則跳過監聽器類中的系統輸出將被調用,並且將報告跳過的錯誤。
然而,如果跳過上限不(比如說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)
這種行爲背後的原因是什麼?我該怎麼做才能解決這個問題?
感謝您的閱讀!