java spring 批处理:没有 ItemWriter 的 Tasklet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17191889/
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-11-01 01:16:07  来源:igfitidea点击:

spring batch : Tasklet without ItemWriter

javaspringspring-batch

提问by Yassine EL AYACHI

I defined my tasklet without ItemWriter like this :

我在没有 ItemWriter 的情况下定义了我的 tasklet,如下所示:

<b:tasklet>
    <b:chunk reader="baseReader" processor="baseProcessor"  commit-interval="100" />
</b:tasklet>

and i got this error :

我收到了这个错误:

Configuration problem: The <b:chunk/>element has neither a 'writer' attribute nor a <writer/>element.

配置问题:该<b:chunk/>元素既没有“writer”属性也没有<writer/>元素。

Do you have any idea ? Thanks

你有什么主意吗 ?谢谢

回答by Cygnusx1

Well, in a chunk, A reader and a Writer are MANDATORY! however, The ItemProcessor is optional.

好吧,总而言之,读者和作家是强制性的!但是,ItemProcessor 是可选的。

This is from the official doc :

这是来自官方文档:

5.1.1. Configuring a Step

Despite the relatively short list of required dependencies for a Step, it is an extremely complex class that can potentially contain many collaborators. In order to ease configuration, the Spring Batch namespace can be used:

5.1.1. 配置步骤

尽管 Step 所需的依赖项列表相对较短,但它是一个极其复杂的类,可能包含许多协作者。为了简化配置,可以使用 Spring Batch 命名空间:

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

The configuration above represents the only required dependencies to create a item-oriented step:

reader - The ItemReader that provides items for processing.

writer - The ItemWriter that processes the items provided by the ItemReader.

transaction-manager - Spring's PlatformTransactionManager that will be used to begin and commit transactions during processing.

job-repository - The JobRepository that will be used to periodically store the StepExecution and ExecutionContext during processing (just before committing). For an in-line (one defined within a ) it is an attribute on the element; for a standalone step, it is defined as an attribute of the .

commit-interval - The number of items that will be processed before the transaction is committed.

It should be noted that, job-repository defaults to "jobRepository" and transaction-manager defaults to "transactionManger". Furthermore, the ItemProcessor is optional, not required, since the item could be directly passed from the reader to the writer.

上面的配置表示创建面向项目的步骤所需的唯一依赖项:

reader - 提供处理项目的 ItemReader。

writer - 处理 ItemReader 提供的项目的 ItemWriter。

事务管理器 - Spring 的 PlatformTransactionManager 将用于在处理期间开始和提交事务。

job-repository - JobRepository 将用于在处理期间(就在提交之前)定期存储 StepExecution 和 ExecutionContext。对于内联(在 a 中定义的),它是元素上的一个属性;对于独立步骤,它被定义为 .

commit-interval - 在提交事务之前将处理的项目数。

需要注意的是,job-repository 默认为“jobRepository”,而transaction-manager 默认为“transactionManger”。此外,ItemProcessor 是可选的,不是必需的,因为项目可以直接从读取器传递给写入器。

回答by Yassine EL AYACHI

We can define a chunk without writer (just a reader + processor), i managed to do so. It seems that in order to pass the writer step containing the chunk must inherit abstract step parents, like this :

我们可以定义一个没有写入器的块(只是一个读取器 + 处理器),我设法做到了。似乎为了通过包含块的 writer 步骤必须继承抽象步骤的父级,如下所示:

    <b:step id="task" parent="Task">
        <b:tasklet>
            <b:chunk reader="baseReader" processor="baseProcessor" commit- interval="100" />
        </b:tasklet>
    </b:step>

    <b:job id="batch" parent="Batch">
        <b:step id="etape" parent="task" />
    </b:job>

Problem solved, thanks!

问题已解决,谢谢!