Spring Batch:不重启已经完成的步骤

时间:2015-06-16 01:20:24

标签: spring spring-batch

在Spring Batch中重新启动作业时,它不应重新启动已完成的步骤。但同样的事情并没有发生在我的情况下。

在下面的代码片段中,myTasklet2依赖于myTasklet1。而这两个和myTasklet3并行运行。在第一次运行中,myTasklet1失败,因此myTasklet2也失败了。但myTasklet3成功了。当我再次运行作业时,它应该只运行myTasklet1和myTasklet2。但它也运行myTasklet3。

任何人都可以帮我弄清楚我是否犯了任何错误?

<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 个答案:

没有答案