Hi all,
I've started to work with spring-batch recently and I found this framework pretty ok.
Now I'm stuck with one problem and I would like to get some help from more experienced users.
Just in short to describe what I'm trying to achieve:
1. Step 1 - I need to read some data from DB, which will fetch records in DB that should later be processed.
2. Step 2 - fetched data (can be multiple records - List<UsersToMove>) must be passed to 2nd step, so step 2 can do some processing of those data. (e.g. Create a mail to admins)
As I've read documentation just to give you the input where I am now:
Job definition
First step item reader and item writer are given respectively:Code:<job id="archiveProfile" job-repository="jobRepository" > <step id="fetchProfiles" next="fetchUserSubscriptions"> <tasklet allow-start-if-complete="true" transaction-manager="liveDBTransactionManager"> <chunk reader="profilesToArchiveItemReader" writer="profilesToArchiveItemWriter" commit-interval="${job.commit.interval}"></chunk> <listeners> <listener ref="promotionListener" /> </listeners> </tasklet> </step> <step id="fetchUserSubscriptions" next="fetchUserSubscriptionBuffers" > <tasklet ref="userSubscriptionsToArchiveTask" allow-start-if-complete="true" transaction-manager="liveDBTransactionManager" /> </step> </job>
Code:@Service("profilesToArchiveItemReader") public class ProfilesToArchiveItemReader implements ItemReader<UserProfileToMove> { private static final Logger log = LoggerFactory.getLogger(ProfilesToArchiveItemReader.class); private JdbcTemplate liveJdbcTemplate; @Autowired public ProfilesToArchiveItemReader(@Qualifier("liveDBJdbcTemplate") JdbcTemplate liveJdbcTemplate) { setLiveJdbcTemplate(liveJdbcTemplate); } @Override public UserProfileToMove read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { log.trace("-+- read() method - ENTER -+-"); Assert.notNull(liveJdbcTemplate); String sql = "SELECT DISTINCT p.id, p.activated, p.bad_mail, p.full_member, u.deleted, u.deleted_date, " + "u.last_login_date, u.blocked FROM profile AS p INNER JOIN USER AS u ON u.profile_id = p.id " + "WHERE (p.activated=FALSE AND u.deleted=TRUE)"; List<UserProfileToMove> result = liveJdbcTemplate.query(sql, ParameterizedBeanPropertyRowMapper.newInstance(UserProfileToMove.class)); log.debug("-+- getAllUserProfilesToMove() method - profilesToMove size: {} -+-", result.size()); log.info("-+- UserProfileToMove itemRead() -+-"); log.trace("-+- read() method - LEAVE -+-"); return result.get(0); } /** * @param liveJdbcTemplate the liveJdbcTemplate to set */ public void setLiveJdbcTemplate(JdbcTemplate liveJdbcTemplate) { this.liveJdbcTemplate = liveJdbcTemplate; }So from logs I am pretty sure that everything is fine.Code:@Service("profilesToArchiveItemWriter") public class SavingItemWriter implements ItemWriter<UserProfileToMove> { private static final Logger log = LoggerFactory.getLogger(SavingItemWriter.class); private StepExecution stepExecution; @Override public void write(List<? extends UserProfileToMove> items) throws Exception { log.trace("-+- write() method - ENTER -+-"); log.info("-+- profilesToArchiveItemWriter write() -+-"); ExecutionContext stepContext = stepExecution.getExecutionContext(); log.info("-+- Items size: {}", items.size()); stepContext.put("profilesToArchive", items); log.trace("-+- write() method - LEAVE -+-"); } @BeforeStep public void saveStepExecution(StepExecution stepExecution) { this.stepExecution = stepExecution; }
But now I want to have 2nd step as POJO (org.springframework.batch.core.step.tasklet.Metho dInvokingTaskletAdapter)
so this bean I've defined as following:
Ok, now the idea is to already fetched Users from step ONE, to pass that listCode:<!-- ### Payment package to be moved ### --> <beans:bean id="userSubscriptionsToArchiveTask" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter" p:targetObject-ref="userSubscriptionToMoveBO" p:targetMethod="userSubscriptionsToMove" />
List<UsersToMove> to step TWO so this POJO business service will do some processing on that list.
I've tried to create method like in documentation
and make it part of Service method, but it never executes, even POJO method <i>userSubscriptionsToMove</i> is called.Code:@BeforeStep public void retrieveInterstepData(StepExecution stepExecution) { JobExecution jobExecution = stepExecution.getJobExecution(); ExecutionContext jobContext = jobExecution.getExecutionContext(); this.someObject = jobContext.get("someKey"); }
Can someone help me or guide me how to accomplish this ?


Reply With Quote
Don't know if this is the best way but it works for me.
