Registering ItemStream
with a Step
-
当一个步骤失败且可能需要重新启动时,ItemStream 接口提供持久状态信息。
-
此外,如果 ItemReader、ItemProcessor 或 ItemWriter 本身未实现 ItemStream,则任何其他流都需要单独注册。可以通过在步骤上指定 stream 元素来实现注册。
步骤必须在其生命周期的必要点照顾`ItemStream`回调。(有关 `ItemStream`接口的更多信息,请参见ItemStream)。如果步骤失败且可能需要重新启动,此步骤至关重要,因为 `ItemStream`接口是步骤获取有关执行之间持久状态所需的信息。
The step has to take care of ItemStream
callbacks at the necessary points in its
lifecycle. (For more information on the ItemStream
interface, see
ItemStream). This is vital if a step fails and might
need to be restarted, because the ItemStream
interface is where the step gets the
information it needs about persistent state between executions.
如果 ItemReader
、ItemProcessor
或 ItemWriter
本身实现了 ItemStream
接口,则会自动注册这些内容。需要单独注册任何其他流。这种情况通常发生在将间接依赖项(例如,委托)注入到读取器和写入器中时。可以通过 stream
元素在 step
上注册流。
If the ItemReader
, ItemProcessor
, or ItemWriter
itself implements the ItemStream
interface, these are registered automatically. Any other streams need to be
registered separately. This is often the case where indirect dependencies, such as
delegates, are injected into the reader and writer. You can register a stream on the
step
through the stream
element.
- Java
-
以下示例展示了如何在 Java 中在
step
上注册stream
:
The following example shows how to register a stream
on a step
in Java:
@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("step1", jobRepository)
.<String, String>chunk(2, transactionManager)
.reader(itemReader())
.writer(compositeItemWriter())
.stream(fileItemWriter1())
.stream(fileItemWriter2())
.build();
}
/**
* In Spring Batch 4, the CompositeItemWriter implements ItemStream so this isn't
* necessary, but used for an example.
*/
@Bean
public CompositeItemWriter compositeItemWriter() {
List<ItemWriter> writers = new ArrayList<>(2);
writers.add(fileItemWriter1());
writers.add(fileItemWriter2());
CompositeItemWriter itemWriter = new CompositeItemWriter();
itemWriter.setDelegates(writers);
return itemWriter;
}
- XML
-
以下示例展示了如何在 XML 中在
step
上注册stream
:
The following example shows how to register a stream
on a step
in XML:
<step id="step1">
<tasklet>
<chunk reader="itemReader" writer="compositeWriter" commit-interval="2">
<streams>
<stream ref="fileItemWriter1"/>
<stream ref="fileItemWriter2"/>
</streams>
</chunk>
</tasklet>
</step>
<beans:bean id="compositeWriter"
class="org.springframework.batch.item.support.CompositeItemWriter">
<beans:property name="delegates">
<beans:list>
<beans:ref bean="fileItemWriter1" />
<beans:ref bean="fileItemWriter2" />
</beans:list>
</beans:property>
</beans:bean>
在前一个示例中,CompositeItemWriter
不是 ItemStream
,但其两个委托都是。因此,必须将两个委托写入器显式注册为流,以便框架正确处理它们。ItemReader
不需要显式注册为流,因为它 Step
的直接属性。该步骤现在是可重新启动的,并且在发生故障时将正确持久化读取器和写入器状态。
In the preceding example, the CompositeItemWriter
is not an ItemStream
, but both of its
delegates are. Therefore, both delegate writers must be explicitly registered as streams
for the framework to handle them correctly. The ItemReader
does not need to be
explicitly registered as a stream because it is a direct property of the Step
. The step
is now restartable, and the state of the reader and writer is correctly persisted in the
event of a failure.