3

I'm trying to read some records from a database using loops then do some calculations on the records (updating a field called total).

But i'm new to spring batch so please can anyone provide me with some tips.

mridula
  • 3,203
  • 3
  • 32
  • 55
user1744446
  • 119
  • 1
  • 2
  • 10

1 Answers1

6

this sounds like something the chunk pattern would address. you can use re-use existing Spring Batch components to read from the database, compose your own processor, then pass back to a Spring Batch component to store.

say the use case is like this; - read a record - record.setTotalColumn(record.getColumn2() + record.getColumn3()) - update

this configuration might look like this

<batch:job id="recordProcessor">
  <batch:step id="recordProcessor.step1">
    <batch:tasklet>
      <batch:chunk reader="jdbcReader" processor="calculator" writer="jdbcWriter" commit-interval="10"/>
      </batch:tasklet>
  </batch:step>
</batch:job>

<bean id="jdbcReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
  <property name="dataSource" ref="dataSource"/>
  <property name="sql" value="select idColumn,column1,column2,totalColumn from my_table"/>
  <property name="rowMapper" ref="myRowMapper"/>
</bean>

<bean id="jdbcWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
  <property name="dataSource" ref="dataSource"/>
  <property name="sql" value="update my_table set totalColumn = :value where idColumn = :id"/>
</bean>

<bean id="calculator" class="de.incompleteco.spring.batch.step.item.CalculationProcessor"/>

this means that the only thing you have to 'write' from scratch would be the calculator processor which could e something like this;

package de.incompleteco.spring.batch.step.item;

import org.springframework.batch.item.ItemProcessor;

public class CalculationProcessor implements ItemProcessor<MyObject, MyObject> {

    @Override
    public MyObject process(MyObject item) throws Exception {
        //do the math
        item.setTotalColumn(item.getColumn1() + item.getColumn2());
        //return
        return item;
    }

}
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
incomplete-co.de
  • 2,137
  • 18
  • 23
  • thank you very much. one last thing, do i have to do anything about the springContext.xml ??? – user1744446 Apr 23 '13 at 14:17
  • the batch job xml above would go in your springContext.xml along with the other minimum setup for batch – incomplete-co.de Apr 23 '13 at 14:26
  • I have a similar sceanario, what to do when each jobinstance has to read/update 100 rows out of 1000 (i.e. 10 job executions). The 'sql' param needs to be fed dynamically?how? – UserBSS1 Jan 27 '14 at 16:46