Configuring Skip Logic
有许多场景,其中在处理过程中遇到的错误不应该导致 Step
故障,而应该被跳过。通常,必须由理解数据本身及其含义的人来做出此决策。例如,财务数据可能不可跳过,因为它会导致资金转移,而这需要完全准确。另一方面,加载供应商列表可能允许跳过。如果未加载供应商,因为供应商格式不正确或缺少必要信息,则可能没有问题。通常,这些不良记录也会被记录下来,稍后在讨论侦听器时会对此进行介绍。
There are many scenarios where errors encountered while processing should not result in
Step
failure but should be skipped instead. This is usually a decision that must be
made by someone who understands the data itself and what meaning it has. Financial data,
for example, may not be skippable because it results in money being transferred, which
needs to be completely accurate. Loading a list of vendors, on the other hand, might
allow for skips. If a vendor is not loaded because it was formatted incorrectly or was
missing necessary information, there probably are not issues. Usually, these bad
records are logged as well, which is covered later when discussing listeners.
- Java
-
以下 Java 示例显示了使用跳过限制的示例:
The following Java example shows an example of using a skip limit:
@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("step1", jobRepository)
.<String, String>chunk(10, transactionManager)
.reader(flatFileItemReader())
.writer(itemWriter())
.faultTolerant()
.skipLimit(10)
.skip(FlatFileParseException.class)
.build();
}
- XML
-
以下 XML 示例显示了使用跳过限制的示例:
The following XML example shows an example of using a skip limit:
<step id="step1">
<tasklet>
<chunk reader="flatFileItemReader" writer="itemWriter"
commit-interval="10" skip-limit="10">
<skippable-exception-classes>
<include class="org.springframework.batch.item.file.FlatFileParseException"/>
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
在前面的示例中,使用了 FlatFileItemReader
。如果在任何时候抛出 FlatFileParseException
,则跳过该项并计入 10 的总跳过限制。在块处理的任何阶段(读取、处理或写入)期间可能抛出已声明的异常(及其子类)。在步骤执行内部对读取、处理和写入跳过进行单独计数,但该限制适用于所有跳过。一旦达到跳过限制,找到的下一个异常会导致步骤失败。换句话说,第 11 次跳过引发异常,而不是第 10 次跳过。
In the preceding example, a FlatFileItemReader
is used. If, at any point, a
FlatFileParseException
is thrown, the item is skipped and counted against the total
skip limit of 10. Exceptions (and their subclasses) that are declared might be thrown
during any phase of the chunk processing (read, process, or write). Separate counts
are made of skips on read, process, and write inside
the step execution, but the limit applies across all skips. Once the skip limit is
reached, the next exception found causes the step to fail. In other words, the eleventh
skip triggers the exception, not the tenth.
前一个示例的一个问题是,除了 FlatFileParseException
之外,任何其他异常都会导致 Job
失败。在某些情况下,这可能是正确的行为。然而,在其他情况下,可能更容易识别哪些异常应导致故障并跳过其他所有异常。
One problem with the preceding example is that any other exception besides a
FlatFileParseException
causes the Job
to fail. In certain scenarios, this may be the
correct behavior. However, in other scenarios, it may be easier to identify which
exceptions should cause failure and skip everything else.
- Java
-
以下 Java 示例显示了排除特定异常的示例:
The following Java example shows an example excluding a particular exception:
@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("step1", jobRepository)
.<String, String>chunk(10, transactionManager)
.reader(flatFileItemReader())
.writer(itemWriter())
.faultTolerant()
.skipLimit(10)
.skip(Exception.class)
.noSkip(FileNotFoundException.class)
.build();
}
- XML
-
以下 XML 示例显示了排除特定异常的示例:
The following XML example shows an example excluding a particular exception:
<step id="step1">
<tasklet>
<chunk reader="flatFileItemReader" writer="itemWriter"
commit-interval="10" skip-limit="10">
<skippable-exception-classes>
<include class="java.lang.Exception"/>
<exclude class="java.io.FileNotFoundException"/>
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
通过将 java.lang.Exception
标识为可跳过的异常类,该配置表明所有 Exceptions
都可跳过。但是,通过“排除
”java.io.FileNotFoundException
,配置将可跳过的异常类列表细化为所有 Exceptions
,除了 FileNotFoundException
。如果遇到任何已排除的异常类,它都是致命的(即,它们不会被跳过)。
By identifying java.lang.Exception
as a skippable exception class, the configuration
indicates that all Exceptions
are skippable. However, by “excluding”
java.io.FileNotFoundException
, the configuration refines the list of skippable
exception classes to be all Exceptions
except FileNotFoundException
. Any excluded
exception class is fatal if encountered (that is, they are not skipped).
对于遇到的任何异常,跳过是由类层次结构中最接近的超类确定的。任何未分类的异常都将被视为“致命”。
For any exception encountered, the skippability is determined by the nearest superclass in the class hierarchy. Any unclassified exception is treated as 'fatal'.
- Java
-
skip
和noSkip
方法调用的顺序无关紧要。
The order of the skip
and noSkip
method calls does not matter.
- XML
-
<include/>
和<exclude/>
元素的顺序无关紧要。
The order of the <include/>
and <exclude/>
elements does not matter.