简体   繁体   English

如果Item Reader不返回任何内容,如何跳过Spring Batch块中的Item Writer?

[英]How to skip Item Writer in Spring Batch chunk if Item Reader returns nothing?

So I have a Spring Batch Project has one repeated step. 所以我有一个Spring Batch Project,它有一个重复的步骤。

<batch:job id="fooJob">
    <batch:step id="barStep">
        <batch:tasklet>
            <batch:chunk reader="myReader" writer="myWriter"
                commit-interval="5">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
</batch:job>

The reader is a JdbcCursorItemReader that also has a RowMapper to store the information it retrieves from the database into a POJO. 该阅读器是一个JdbcCursorItemReader ,它也具有RowMapper来将其从数据库检索的信息存储到POJO中。 Then "myWriter" writes the fields set in the POJO to a text file. 然后, "myWriter"将POJO中设置的字段写入文本文件。 And this happens regularly (dictated by cron). 而且这种情况经常发生(由cron决定)。

How do I set a condition to skip the itemWriter altogether when the database is empty and the reader can't read anything? 当数据库为空并且阅读器无法读取任何内容时,如何设置条件以完全跳过itemWriter?

In a chunk oriented tasklet, everything is processed in chunks of items, not will all the items. 在面向块的Tasklet中,所有内容都以块的形式处理,而不是所有项目。 To skip when the reader didn't read anything, you would have to know that there are in no fact no items; 要在读者什么都没读时跳过,您必须知道实际上没有任何物品。 encountering an empty chunk of data would not suffice since there could have been another chunk before. 遇到空数据块是不够的,因为之前可能还有另一个数据块。

A simple way around that is to simply let the step continue if nothing is read. 一种简单的解决方法是,如果什么也没读,则让该步骤继续进行。 In that case, the writer will have nothing to write. 在这种情况下,作者将没有任何东西可写。 Supposing that you are using FlatFileItemWriter to write the file, you can configure it not to leave an empty file behind. 假设您正在使用FlatFileItemWriter写入文件,则可以对其进行配置,以不留空文件。 This is done through the shouldDeleteIfEmpty property: 这是通过shouldDeleteIfEmpty属性完成的:

Flag to indicate that the target file should be deleted if no lines have been written (other than header and footer) on close. 指示关闭时未写任何行(页眉和页脚除外)的目标文件的标记。 Defaults to false . 默认为false

As such, you just need to configure your writer with that property set to true . 这样,您只需要配置该属性为true writer即可。 It will delete the empty file if nothing was written. 如果未写入任何内容,它将删除空文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM