简体   繁体   English

春季批项目拆分器

[英]Spring batch item splitter

I have the following issue: I have a file where each line results in 1 or more items being written to a database. 我有以下问题:我有一个文件,其中每一行导致1个或多个项目被写入数据库。 I need to do a lookup from another system to find out how many items need to be written. 我需要从另一个系统进行查找,以找出需要写多少个项目。 Each of the items resulting from a single line must be transformed using a chain of item processors and finally written to multiple tables in a DB. 一行中的每个项目都必须使用一系列项目处理器进行转换,并最终写入数据库的多个表中。

Because each item needs to write to multiple tables, they must each be in their own transaction. 因为每个项目都需要写入多个表,所以每个项目都必须处于各自的事务中。 Because of this, I can't just have an ItemProcessor<Foo, List<Bar>> which handles it. 因此,我不能只拥有一个处理它的ItemProcessor<Foo, List<Bar>> In that case -- even with a commit-interval of 1 -- I would end up with multiple items in the same transaction. 在那种情况下-即使commit-interval为1-我最终也会在同一事务中遇到多个项目。

I have seen this stack overflow question already. 我已经看到了这个堆栈溢出问题。 The accepted answer doesn't help me because of the transaction issue. 由于交易问题,接受的答案对我没有帮助。 The other answer about using a Spring Integration splitter sounds intriguing. 关于使用Spring Integration splitter的另一个答案听起来很有趣。 However, it doesn't give a ton of details. 但是,它没有提供很多细节。 How do I define the reader's output as the channel input? 如何将阅读器的输出定义为通道输入? How would I define an output channel which goes to my item writer? 我将如何定义一个输出通道,该通道可用于我的项目编写器? How could I still run an item processor chain on each newly divided item? 我如何仍可以在每个新划分的物料上运行物料处理器链?

I haven't been able to find any examples using the splitter within a spring batch job. 我无法在春季批处理作业中使用拆分器找到任何示例。 Any recommendations would be appreciated. 任何建议,将不胜感激。

Had same problematic with FlatFileItemReader, minus the transaction issue though. FlatFileItemReader遇到了同样的问题,但是减去了事务问题。 Dealt with it that way : 这样处理:

public class FlatFileItemListWriter<T> extends FlatFileItemWriter<T> {

    /**
     * {@inheritDoc}
     * 
     * @see org.springframework.batch.item.file.FlatFileItemWriter#write(java.util.List)
     */
    @Override
    @SuppressWarnings("unchecked")
    public void write(List<? extends T> itemsLists) throws Exception {
        List<T> items = new ArrayList<T>();
        for (Object item : itemsLists) {
            items.addAll((List<T>) item);
        }
        super.write(items);
    }

}

That way, the FlatFileItemWriter deals with the list the way it would have if my processor didn't produce lists. 这样,FlatFileItemWriter处理列表的方式与我的处理器不生成列表时的方式相同。

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

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