12.5 处理模型( Processing Models)

JSR-352 和 Spring Batch 一样支持两种基本处理模型:

  • 基于 Item 的处理 - 通过 javax.batch.api.chunk.ItemReader, 以及可选的 javax.batch.api.chunk.ItemProcessor, 加上 javax.batch.api.chunk.ItemWriter.
  • 基于 Task 的处理 - 使用 javax.batch.api.Batchlet 的实现. 当前此模型和基于 org.springframework.batch.core.step.tasklet.Tasklet 的处理是一样的.

12.5.1 基于 Item 的处理

Item based processing in this context is a chunk size being set by the number of items read by an ItemReader. To configure a step this way, specify the item-count (which defaults to 10) and optionally configure the checkpoint-policy as item (this is the default).

这里说的 基于 Item 的处理是指定由ItemReader读取的 item 数量。如果以这种方式配置step, 指定item-count(默认值为10),还可以配置可选参数 checkpoint-policy 为 item (也是默认值)。

  1. ...
  2. <step id="step1">
  3. <chunk checkpoint-policy="item" item-count="3">
  4. <reader ref="fooReader"/>
  5. <processor ref="fooProcessor"/>
  6. <writer ref="fooWriter"/>
  7. </chunk>
  8. </step>
  9. ...

If item based checkpointing is chosen, an additional attribute time-limit is supported. This sets a time limit for how long the number of items specified has to be processed. If the timeout is reached, the chunk will complete with however many items have been read by then regardless of what the item-count is configured to be.

如果选择了基于item的检查点, 则可以使用附加属性 time-limit。时限设置一定时间内要处理的 item 数量。如果到达时限, 不管完成多少 item, chunk都会进入完成状态, 然后不管 item-count 配置。

12.5.2 自定义检查点(checkpointing)

JSR-352 calls the process around the commit interval within a step “checkpointing”. Item based checkpointing is one approach as mentioned above. However, this will not be robust enough in many cases. Because of this, the spec allows for the implementation of a custom checkpointing algorithm by implementing the javax.batch.api.chunk.CheckpointAlgorithm interface. This functionality is functionally the same as Spring Batch’s custom completion policy. To use an implementation of CheckpointAlgorithm, configure your step with the custom checkpoint-policy as shown below where fooCheckpointer refers to an implementation of CheckpointAlgorithm.

JSR-352 在一个step的“检查点”中调用提交间隔流程。基于 Item 的检查点是上面提到的一种方法。然而,在许多时候这不够健壮。因此,规范允许自定义的 javax.batch.api.chunk.CheckpointAlgorithm 实现。CheckpointAlgorithm接口。这也是 Spring Batch 的定制完成策略。使用 CheckpointAlgorithm 的实现, 配置自定义的 checkpoint-policy, 如下所示 ,其中 fooCheckpointer 指向 CheckpointAlgorithm 的实现。

  1. ...
  2. <step id="step1">
  3. <chunk checkpoint-policy="custom">
  4. <checkpoint-algorithm ref="fooCheckpointer"/>
  5. <reader ref="fooReader"/>
  6. <processor ref="fooProcessor"/>
  7. <writer ref="fooWriter"/>
  8. </chunk>
  9. </step>
  10. ...