Java Spring 批处理中的多个 itemwriter

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

Multiple itemwriters in Spring batch

javaspringspring-batch

提问by Pratik Shelar

I am currently writing a Spring batch where I am reading a chunk of data, processing it and then I wish to pass this data to 2 writers. One writer would simply update the database whereas the second writer will write to a csv file.

我目前正在编写一个 Spring 批处理,我正在读取一大块数据,对其进行处理,然后我希望将这些数据传递给 2 个编写者。一位作家将简单地更新数据库,而第二位作家将写入 csv 文件。

I am planning to write my own custom writer and inject the two itemWriters in the customItemWriter and call the write methods of both the item writers in the write method of customItemWriter. Is this approach correct? Are there any ItemWriter implementations available which meet my requirements?

我打算编写自己的自定义编写器,并在 customItemWriter 中注入两个 itemWriter,并在 customItemWriter 的 write 方法中调用两个项目编写器的 write 方法。这种方法是否正确?是否有任何 ItemWriter 实现可以满足我的要求?

Thanks in advance

提前致谢

采纳答案by Oussama Zoghlami

You can use Spring's CompositeItemWriterand delegate to it all your writers.
here is a configuration example.

您可以使用 Spring 的CompositeItemWriter并将您的所有作者委托给它。
这是一个配置示例

回答by Luca Basso Ricci

You were right. SB is heavly based on delegation so using a CompositeItemWriteris the right choice for your needs.

你是对的。SB 很大程度上基于委托,因此使用CompositeItemWriter是满足您需求的正确选择。

回答by MattC

Depending on your need, another option is to extend the Writer class and add functionality there. For example, I have a project where I am extending HibernateItemWriter and then overriding write(List items). I then send the objects I am writing along with my sessionFactory to the doWrite method of the Writer: doWrite(sessionFactory, filteredRecords).

根据您的需要,另一种选择是扩展 Writer 类并在那里添加功能。例如,我有一个项目,我正在扩展 HibernateItemWriter 然后覆盖 write(List items)。然后我将我正在编写的对象与我的 sessionFactory 一起发送到 Writer 的 doWrite 方法:doWrite(sessionFactory,filteredRecords)。

So in the example above, I could write to the csv file in my extended class and then the HibernateItemWriter would write to the database. Obviously this might not be ideal for this example, but for certain scenarios it is a nice option.

因此,在上面的示例中,我可以写入扩展类中的 csv 文件,然后 HibernateItemWriter 将写入数据库。显然,这对于此示例可能并不理想,但对于某些场景,这是一个不错的选择。

回答by Yilei

You don't necessarily have to use xml like the example. If the rest of your code uses annotation, you could simply do the following.

您不必像示例那样使用 xml。如果您的其余代码使用注释,您可以简单地执行以下操作。

public ItemWriter<T> writerOne(){
    ItemWriter<T> writer = new ItemWriter<T>();
    //your logic here
    return writer;
}

public ItemWriter<T> writerTwo(){
    ItemWriter<T> writer = new ItemWriter<T>();
    //your logic here
    return writer;
}

public CompositeItemWriter<T> compositeItemWriter(){
    CompositeItemWriter writer = new CompositeItemWriter();
    writer.setDelegates(Arrays.asList(writerOne(),writerTwo()));
    return writer;
}

回答by Niraj Sonawane

Java Config way SpringBatch4

Java配置方式SpringBatch4

@Bean
    public Step step1() {
            return this.stepBuilderFactory.get("step1")
                                    .<String, String>chunk(2)
                                    .reader(itemReader())
                                    .writer(compositeItemWriter())
                                    .stream(fileItemWriter1())
                                    .stream(fileItemWriter2())
                                    .build();
    }

    /**
     * In Spring Batch 4, the CompositeItemWriter implements ItemStream so this isn't
     * necessary, but used for an example.
     */
    @Bean
    public CompositeItemWriter compositeItemWriter() {
            List<ItemWriter> writers = new ArrayList<>(2);
            writers.add(fileItemWriter1());
            writers.add(fileItemWriter2());

            CompositeItemWriter itemWriter = new CompositeItemWriter();

            itemWriter.setDelegates(writers);

            return itemWriter;
    }