Announcement Announcement Module
No announcement yet.
recursive Job Execution Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2

    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.

    <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:chunk reader="itemReader" writer="itemWriter" commit-interval="1" />
    <batch:listener ref="listener"></batch:listener>
    <batch:decision id="decision" decider="decider">
    <batch:next on="CONTINUE" to="myStep" />
    <batch:end on="COMPLETED"/>

    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 {

    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
    if(stepExecution.getReadCount() == 0) {
    return FlowExecutionStatus.COMPLETED;
    } else {
    return new FlowExecutionStatus("CONTINUE");


    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";

    public void beforeStep(StepExecution stepExecution) {
    ExecutionContext jobExecCtx = stepExecution.getJobExecution().getExecutionContex t();
    if(!jobExecCtx.containsKey(DEPTH)) {
    jobExecCtx.putLong(DEPTH, Long.valueOf(1l));

    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;