I have created a job and i am checking the condition for restarting the job in job decider. My problem is "if i restarted it then its fetching same records from 1 to 10" because my sql is "select * from userdetails limit 10". My problem is "I want to fetch from 11th record" when the particular job is restarted.
Code:<job id="batchJob" job-repository="jobRepository" incrementer="myJobParametersIncrementer" xmlns="http://www.springframework.org/schema/batch"> <step id="updateData" next="limitDecision"> <tasklet transaction-manager="transactionManager" allow-start-if-complete="true"> <chunk reader="userReader" writer="userWriter" commit-interval="10"> <listeners> <listener ref="stepExecListener" /> </listeners> </chunk> </tasklet> </step> <decision id="limitDecision" decider="limitDecider"> <next on="CONTINUE" to="updateData" /> <end on="COMPLETED" /> </decision> </job> <bean id="myJobParametersIncrementer" class="com.inf.repeat.MyJobParametersIncrementer" /> <bean id="limitDecider" class="com.inf.repeat.LimitDecider"> <constructor-arg name="dataSource" ref="jpaDataSource" /> </bean> <bean id="stepExecListener" class="com.inf.repeat.LoopStepExecutionListener"> <constructor-arg name="dataSource" ref="jpaDataSource" /> </bean> <bean id="dataTasklet" class="com.inf.mapper.DataTasklet" scope="step"> <constructor-arg name="dataSource" ref="jpaDataSource" /> </bean> <bean id="userReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step"> <property name="dataSource" ref="jpaDataSource" /> <property name="sql" value="select * from userdetails limit 10 " /> <property name="rowMapper" ref="dbObjectRowMapper" /> </bean> <bean id="userWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter" scope="step"> <property name="dataSource" ref="jpaDataSource" /> <property name="sql" value="insert into newuserdetails (name,gender) values (?,?)" /> <property name="itemPreparedStatementSetter" ref="newPreparedStatementSetter" /> </bean>Code:public class LimitDecider implements JobExecutionDecider { JdbcTemplate jdbcTemplate; private static final String SQL = "select count(*) from userdetails"; public LimitDecider(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } @Override public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) { int countTotalRows = jdbcTemplate.queryForInt(SQL); int oldValue = (Integer) jobExecution.getExecutionContext().get("newStart"); int newValue = (Integer) jobExecution.getExecutionContext().get("start"); int startValue = oldValue + newValue; stepExecution.getJobExecution().getExecutionContext().put("newStart", startValue); if (startValue <= countTotalRows) { return new FlowExecutionStatus("CONTINUE"); } else { return new FlowExecutionStatus("COMPLETED"); } } }Code:public class LoopStepExecutionListener extends ChunkListenerSupport implements StepExecutionListener{ private StepExecution stepExecution; @Override public void beforeStep(StepExecution stepExecution) { this.stepExecution = stepExecution; } @Override public void beforeChunk() { stepExecution.getJobExecution().getExecutionContext().put("start", stepExecution.getReadCount()); } @Override public void afterChunk() { } @Override public ExitStatus afterStep(StepExecution stepExecution) { return null; } }


Reply With Quote
