-
Mar 6th, 2013, 06:14 AM
#1
recursive Job Execution
HI All,
i need to configure spring job in a way that, on checking of some condition it will execute its all steps again , basically i need to parse nested XML and load data in DB , but nesting level i am not sure so for now i am loading data of level 1 and then level2 and so on ....for it is hardcoded for 3 level only , but i need to make it dynamic for n -level ,
please suggest
-
Mar 7th, 2013, 10:47 AM
#2
Hi,
you could use the JobExecutionContext to store the depth you are currently in. Use a StepExecutionListener to increase the depth after execution. After the step, use a decider that re-runs the step if some items were read. If no items were read - I guess - you are done.
Spring-config:
<bean id="decider" class="com.test.MyDecider"/>
<bean id="listener" class="com.test.NestedDepthListener"/>
<batch:job id="myJob">
<batch:step id="myStep" next="decision">
<batch:tasklet>
<batch:chunk reader="itemReader" writer="itemWriter" commit-interval="1" />
<batch:listeners>
<batch:listener ref="listener"></batch:listener>
</batch:listeners>
</batch:tasklet>
</batch:step>
<batch:decision id="decision" decider="decider">
<batch:next on="CONTINUE" to="myStep" />
<batch:end on="COMPLETED"/>
</batch:decision>
</batch:job>
Decider:
package com.test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecut ionStatus;
import org.springframework.batch.core.job.flow.JobExecuti onDecider;
public class MyDecider implements JobExecutionDecider {
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
if(stepExecution.getReadCount() == 0) {
return FlowExecutionStatus.COMPLETED;
} else {
return new FlowExecutionStatus("CONTINUE");
}
}
}
Listener:
package com.test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListen er;
import org.springframework.batch.item.ExecutionContext;
public class NestedDepthListener implements StepExecutionListener {
final String DEPTH = "DEPTH";
@Override
public void beforeStep(StepExecution stepExecution) {
//Initialization
ExecutionContext jobExecCtx = stepExecution.getJobExecution().getExecutionContex t();
if(!jobExecCtx.containsKey(DEPTH)) {
jobExecCtx.putLong(DEPTH, Long.valueOf(1l));
}
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
ExecutionContext jobExecCtx = stepExecution.getJobExecution().getExecutionContex t();
long currDepth = jobExecCtx.getLong(DEPTH);
jobExecCtx.putLong(DEPTH, Long.valueOf(currDepth + 1));
return ExitStatus.COMPLETED;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules