Spring batch: processing multiple record at once

两盒软妹~` 提交于 2019-12-11 12:49:40

问题


I am using spring batch and as normally used I have reader , processor and writer .

I have 2 questions

1> Reader queries all 200 records (total record size in table is 200 and I have given pagesize=200 )and thus it gets me all 200 records, and in processor we want list of all these record because we have to compare each record with other 199 records to group them in different tiers . Thus I am thinking if we can get that list in processing step , I can manipulate them .how should I approach .

2> In processing stage I need some master data from database depending on which all input records will be processed .i m thinking of injection of data source in processing bean and fetch all master table data and process all records. Is it good approach or please suggest otherwise .

<job id="sampleJob">
    <step id="step1">
        <tasklet>
            <chunk reader="itemReader" processor="processor" writer="itemWriter" commit-interval="20"/>
        </tasklet>
    </step>
</job>

And the processor is

@Override
public User process(Object item) throws Exception {
    // transform item to user
    return user;
}

And I want something like

public List<User> process(List<Object> item) throws Exception {
    // transform item to user
    return user;
}

I found some post here but they say to get the list in writer .But i dont like to process anything in writer, because that kills the defination of writer and processor. Is there any configuration to get the list inside this process method.

Thank you


回答1:


Since the ItemProcessor receives whatever you return from the ItemReader, you need your ItemReader to return the List. That List is really the "item" you're processing. There is an example of this in the Spring Batch Samples. The AggregateItemReader reads all the items from a delegate ItemReader and returns them as a single list. You can take a look at it on Github here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/multiline/AggregateItemReader.java



来源:https://stackoverflow.com/questions/35049613/spring-batch-processing-multiple-record-at-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!