Inheriting from a Parent Step

如果一组 Steps 共享类似的配置,定义一个“parent`" `Step 可能会有所帮助,具体 Steps 可以从中继承属性。类似于 Java 中的类继承,“child`" `Step 将其元素和属性与父级元素和属性进行组合。子级还将覆盖任何父级的 Steps

If a group of Steps share similar configurations, then it may be helpful to define a “parent” Step from which the concrete Steps may inherit properties. Similar to class inheritance in Java, the “child” Step combines its elements and attributes with the parent’s. The child also overrides any of the parent’s Steps.

在以下示例中,StepconcreteStep1 继承自 parentStep。它使用 itemReaderitemProcessoritemWriterstartLimit=5allowStartIfComplete=true 进行实例化。此外,commitInterval5,因为它已被 concreteStep1 Step 覆盖,如下例所示:

In the following example, the Step, concreteStep1, inherits from parentStep. It is instantiated with itemReader, itemProcessor, itemWriter, startLimit=5, and allowStartIfComplete=true. Additionally, the commitInterval is 5, since it is overridden by the concreteStep1 Step, as the following example shows:

<step id="parentStep">
    <tasklet allow-start-if-complete="true">
        <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/>
    </tasklet>
</step>

<step id="concreteStep1" parent="parentStep">
    <tasklet start-limit="5">
        <chunk processor="itemProcessor" commit-interval="5"/>
    </tasklet>
</step>

id 属性在工作元素中的步骤上仍然是必需的。原因有二:

The id attribute is still required on the step within the job element. This is for two reasons:

  • The id is used as the step name when persisting the StepExecution. If the same standalone step is referenced in more than one step in the job, an error occurs.

  • When creating job flows, as described later in this chapter, the next attribute should refer to the step in the flow, not the standalone step.

Abstract Step

有时,可能需要定义一个不是完整 Step 配置的父 Step。例如,如果 Step 配置中去除了 readerwritertasklet 属性,则初始化将失败。如果必须定义一个不包含其中一个或多个这些属性的父级,则应使用 abstract 属性。abstract Step 仅被扩展,从未被实例化。

Sometimes, it may be necessary to define a parent Step that is not a complete Step configuration. If, for instance, the reader, writer, and tasklet attributes are left off of a Step configuration, then initialization fails. If a parent must be defined without one or more of these properties, the abstract attribute should be used. An abstract Step is only extended, never instantiated.

在下例中,如果未声明为抽象,则不会实例化 Step (abstractParentStep)。Step (concreteStep2) 中含有 itemReaderitemWritercommit-interval=10

In the following example, the Step (abstractParentStep) would not be instantiated if it were not declared to be abstract. The Step, (concreteStep2) has itemReader, itemWriter, and commit-interval=10.

<step id="abstractParentStep" abstract="true">
    <tasklet>
        <chunk commit-interval="10"/>
    </tasklet>
</step>

<step id="concreteStep2" parent="abstractParentStep">
    <tasklet>
        <chunk reader="itemReader" writer="itemWriter"/>
    </tasklet>
</step>

Merging Lists

Steps 上的一些可配置元素是列表,例如 <listeners/> 元素。如果父级和子级 Steps 都声明了 <listeners/> 元素,则子级的列表将覆盖父级的列表。为了允许子级向父级定义的列表中添加其他监听器,每个列表元素都有一个 merge 属性。如果该元素指定 merge="true",则子级的列表将与父级的列表组合,而不是覆盖它。

Some of the configurable elements on Steps are lists, such as the <listeners/> element. If both the parent and child Steps declare a <listeners/> element, the child’s list overrides the parent’s. To allow a child to add additional listeners to the list defined by the parent, every list element has a merge attribute. If the element specifies that merge="true", then the child’s list is combined with the parent’s instead of overriding it.

在下例中,Step “concreteStep3” 使用两个监听器来创建:“listenerOne” 和 “listenerTwo”:

In the following example, the Step "concreteStep3", is created with two listeners: listenerOne and listenerTwo:

<step id="listenersParentStep" abstract="true">
    <listeners>
        <listener ref="listenerOne"/>
    <listeners>
</step>

<step id="concreteStep3" parent="listenersParentStep">
    <tasklet>
        <chunk reader="itemReader" writer="itemWriter" commit-interval="5"/>
    </tasklet>
    <listeners merge="true">
        <listener ref="listenerTwo"/>
    <listeners>
</step>