Spring Batch中Step、Tasklet和Chunk的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40041334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:14:33  来源:igfitidea点击:

Difference between Step, Tasklet and Chunk in Spring Batch

springspring-batch

提问by Priyanka Mokashi

What is the difference between Step, Tasklet and Chunk in spring batch.?

Spring Batch 中的 Step、Tasklet 和 Chunk 有什么区别?

Also, How to execute step in parallel via Spring Batch. ?

此外,如何通过 Spring Batch 并行执行步骤。?

回答by Asoub

Well that's actually a good question. Here's an example of configuration:

嗯,这实际上是一个好问题。下面是一个配置示例:

<job id="sampleJob" job-repository="jobRepository">
    <step id="step1" next="step2">
        <tasklet transaction-manager="transactionManager">
            <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/>
        </tasklet>
    </step>
    <step id="step2">
         <tasklet ref="myTasklet"/>
    </step>
</job>

You have a Job, this job is made of steps. Most of the time, these steps are successive. You define in what order your work must be done with steps: you do step 1, then step 2, then step 3, you can do step 4 if step 3 failed, or go directly to step 5, etc.

你有一份工作,这份工作是由步骤组成的。大多数情况下,这些步骤是连续的。你定义你的工作必须按什么顺序完成:你先做第 1 步,然后是第 2 步,然后是第 3 步,如果第 3 步失败,你可以做第 4 步,或者直接转到第 5 步,等等。

What is done in the Step is represented by a tasklet, they do the task.

Step 中所做的事情由一个tasklet 表示,他们执行任务。

In spring batch, you'll mostly do chunk oriented processing: with a reader, a processor, and a writer. From the official documentation:

在 spring 批处理中,您将主要进行面向块的处理:使用读取器、处理器和写入器。来自官方文档:

Chunk oriented processing refers to reading the data one at a time, and creating 'chunks' that will be written out, within a transaction boundary

面向块的处理是指一次读取一个数据,并在事务边界内创建将被写出的“块”

But you can make your own tasklet, and set it in your step. For example, a tasklet that executes a SQL query. (example here: Tasklet to delete a table in spring batch)

但是您可以制作自己的 tasklet,并在您的步骤中进行设置。例如,一个执行 SQL 查询的 tasklet。(这里的例子:Tasklet to delete a table in spring batch

So, the steps are ordered in a job, each step contains a tasklet, which does a task. One of those tasklet (and probably the most used one) is the chunk oriented processing tasklet.

因此,步骤在作业中是有序的,每个步骤都包含一个 tasklet,它执行一个任务。这些 tasklet 之一(可能是最常用的)是面向块的处理 tasklet。

If you're curious, here is the ChunkOrientedTasklet's doc. As you can see, it implements the Taskletinterface.

如果你很好奇,这里是ChunkOrientedTasklet 的文档。如您所见,它实现了Tasklet接口。

For more information: http://docs.spring.io/spring-batch/reference/html/configureStep.html

更多信息:http: //docs.spring.io/spring-batch/reference/html/configureStep.html

And yes, spring batch is well made for parrallel processing, using flows: http://docs.spring.io/spring-batch/reference/html/scalability.html

是的,弹簧批处理非常适合并行处理,使用流程:http: //docs.spring.io/spring-batch/reference/html/scalability.html