4

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 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks . But this class is more of fetching records from file . Do we have similar class to fetch from database . As we can't override read method it's not possible to extend job paging item reader .
The same idea applies and you don't override the read method on the pagingitemreader...you wrap that reader with one that reads from the delegate until the input is exhausted and then return that list as a single item.
I created a delegate aggregate item reader , which seems working , I get a list of records in processing stage . But then it loops sith null values . I don't need to write any mapper and reader class , can u please confirm.
is AggregateItemReader part of spring batch artifact?
How do I know whether specific record is header/footer in a custom ItemReader when reading records from DB by pageSize each?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.