春季批次 - 从第一步重新运行

时间:2016-04-11 16:32:22

标签: spring spring-batch

有没有人知道是否有办法在Spring批次中重新开始? 我希望它首先从Step1开始,然后是Step2,Step3,然后再回到Step1,Step2,Step3,等等,直到满足条件。我试过谷歌搜索,但没有找到任何具体的例子。

到目前为止

代码:

@Bean
Job job(JobBuilderFactory factory) {
    return factory.get(JOB_NAME)
            .start(stagingStep)
            .next(analyzeStep)
            .next(reportingStep)

            .preventRestart()
            .build();
}

1 个答案:

答案 0 :(得分:1)

我认为这可以通过多种方式完成..

1.接受上述here

的工作
<job id="footballJob">
<step id="playerload"          parent="s1" next="gameLoad"/>
<step id="gameLoad"            parent="s2" next="playerSummarization"/>
<step id="playerSummarization" parent="s3"/>
<listeners>
    <listener ref="sampleListener"/>
</listeners>

.. 并实施你的李斯特..

public interface JobExecutionListener {

void beforeJob(JobExecution jobExecution);

void afterJob(JobExecution jobExecution); // implement and call the job again
}

2.实施您自己的触发/调度程序......

<task:scheduled ref="runScheduler" method="run" trigger="mytrigger" />

<bean id="runScheduler" class="com.spring.scheduler.MyScheduler" >
    <property name="jobLauncher" ref="jobLauncher" />
    <property name="job" ref="helloWorldJob" />
</bean>

...

<task:scheduled-tasks>
<!--task:scheduled ref="runScheduler" method="run" fixed-delay="5000" /> -->

<task:scheduled ref="runScheduler" method="run"  cron="*/5 * * * * *" />
</task:scheduled-tasks>

您可以使用自己的触发器并将参考传递给上面...

<bean id="mytrigger" class="com.spring.scheduler.MyTrigger" />

public class MyScheduler {

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job job;

public void run() {
    try {
        JobParameters param = new JobParametersBuilder().toJobParameters();
        String dateParam = new Date().toString();
        JobExecution execution = jobLauncher.run(job, param);
        System.out.println("Exit Status in scheduler: " + execution.getStatus());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

然后根据需要创建触发器

public class MyTrigger implements Trigger{
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {...return date;}

3.如果只需要再次重新运行一个tasklet,它很容易,只需返回RepeatStatus.CONTINUABLE,此任务一次又一次重新运行......

public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext)throws Exception 
{
   return RepeatStatus.CONTINUABLE;//RepeatStatus.FINISHED;
}

如果您想要一些特定步骤也可以完成(操作步骤1或2并使用特定步骤来构建作业......再次运行之前)