Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring batch read multiple record and process multiple records

I'm want my Spring batch application to read 50 record from the database at one time and then send those 50 records to the processor and then the writer.

Can someone please tell me how this can be done.

I've tried using JdbcPagingItemReader and setting the pageSize to 50 which reads 50 records but the rowMapper, processor and writer receives one record at a time instead of getting the 50 records.

How do I make it so that the processor and writer get the 50 record in a dto instead of receiving one record at a time?

xml spring config

<job id="indexJob" job-repository="jobRepository">
    <step id="job1">
        <tasklet transaction-manager="transactionManager">
            <chunk reader="reader" processor="processor" writer="writer" commit-interval="1"/>
        </tasklet>
    </step>
</job>

Java spring Configuration

@Bean
@Scope("step")
public JdbcPagingItemReader reader() throws Exception {

    MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
    provider.setSelectClause("select id");
    provider.setFromClause("from BATCH_CUSTOMER");
    provider.setSortKey("id");

    JdbcPagingItemReader reader = new JdbcPagingItemReader();
    reader.setDataSource(this.dataSource());
    reader.setQueryProvider(provider);
    reader.setPageSize(50);
    reader.setRowMapper(new MyRowMapper());
    reader.afterPropertiesSet();

    int counter = 0;
    ExecutionContext executionContext = new ExecutionContext();
    reader.open(executionContext);
    Object pageCredit = new Object();
    while (pageCredit != null) {
        pageCredit = reader.read();
        System.out.println("pageCredit:" + pageCredit);
        counter++;
    }
    reader.close();

    return reader;
}
like image 251
user794783 Avatar asked Feb 25 '26 00:02

user794783


1 Answers

In you xml configuration, change the commit-interval to 50.

like image 66
WilQu Avatar answered Feb 26 '26 12:02

WilQu