Spring Batch: not to restart already completed steps

3.1k Views Asked by At

In Spring Batch when a job is restarted, it should not restart the already completed steps. But same thing is not happening in my case.

In the code snippet below myTasklet2 depends on myTasklet1. And these two and myTasklet3 run in parallel. In the first run myTasklet1 fails and hence myTasklet2 also fails. But myTasklet3 succeeds. When I ran the job again it should have run only myTasklet1 and myTasklet2. But it ran myTasklet3 also.

Can anyone help me to figure out if I am doing any mistake?

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" 
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">


<bean id="myTasklet1" class="com.flow.test.MyTasklet1" />  

<bean id="myTasklet2" class="com.flow.test.MyTasklet2" />  

<bean id="myTasklet3" class="com.flow.test.MyTasklet3" />  

<!-- bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="25" />                  
    <property name="maxPoolSize" value="25" />        
</bean-->

<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>

    <batch:job id="myCustomJob1" restartable="false">  
         <batch:step id="step1" next="step2">  
              <batch:tasklet transaction-manager="transactionManager" ref="myTasklet1" allow-start-if-complete="false">  
              </batch:tasklet>  
         </batch:step>  
         <batch:step id="step2">  
              <batch:tasklet transaction-manager="transactionManager" ref="myTasklet2" allow-start-if-complete="false">  
              </batch:tasklet>  
         </batch:step>  
    </batch:job>    
    <batch:job id="myCustomJob2" restartable="false">  
         <batch:step id="step3">  
              <batch:tasklet transaction-manager="transactionManager" ref="myTasklet3" allow-start-if-complete="false">  
              </batch:tasklet>  
         </batch:step>  
    </batch:job>    

<batch:job id="myCustomJob" job-repository="jobRepository" parent="simpleJob" restartable="false">  
    <batch:split id="preprocessingStep" task-executor="taskExecutor">
        <batch:flow>
            <batch:step id="stepflow1" allow-start-if-complete="false">
                <batch:job ref="myCustomJob1" />
            </batch:step>
        </batch:flow>
        <batch:flow>
            <batch:step id="stepflow2" allow-start-if-complete="false">
                <batch:job ref="myCustomJob2" />
            </batch:step>
        </batch:flow>
    </batch:split>
</batch:job>     

public class MyTasklet1 implements Tasklet {

@Override
public RepeatStatus execute(StepContribution contribution,
        ChunkContext chunkContext) throws Exception {

    String s = "This is an output";

    chunkContext.getStepContext().getStepExecution().getJobExecution()
            .getExecutionContext().put("outputString", s);

    System.out.println("Inside MyTasklet1");

    File f = new File(
            "C:/MyProject/SpringBatchExampleNew/src/main/resources/marker");

    if (f.exists()) {
        System.out.println("File found");
        return RepeatStatus.FINISHED;
    } else {
        System.out.println("File not found");
        throw new Exception("File not found");
    }

}}

public class MyTasklet2 implements Tasklet {

private String output;

public String getOutput() {
    return output;
}

public void setOutput(String output) {
    this.output = output;
}

@Override
public RepeatStatus execute(StepContribution contribution,
        ChunkContext chunkContext) throws Exception {

    setOutput((String) chunkContext.getStepContext().getStepExecution()
            .getJobExecution().getExecutionContext().get("outputString"));

    Thread.sleep(500);
    System.out.println("Inside MyTasklet2");
    System.out.println("Output string is: " + output);

    return RepeatStatus.FINISHED;
}}

public class MyTasklet3 implements Tasklet {

private String output;

public String getOutput() {
    return output;
}

public void setOutput(String output) {
    this.output = output;
}

@Override
public RepeatStatus execute(StepContribution contribution,
        ChunkContext chunkContext) throws Exception {

    setOutput((String) chunkContext.getStepContext().getStepExecution()
            .getJobExecution().getExecutionContext().get("outputString"));

    System.out.println("Inside MyTasklet3");
    System.out.println("Output string is: " + output);

    return RepeatStatus.FINISHED;

}}
0

There are 0 best solutions below