Spring 批处理 - 从第一步重新运行
Spring batch - Rerun from first step
有谁知道有没有办法在 Spring 批次中重新开始?
我希望它首先从第 1 步开始,然后是第 2 步、第 3 步,然后回到第 1 步、第 2 步、第 3 步,依此类推,直到满足条件。我尝试谷歌搜索,但未能找到任何具体示例。
到目前为止的代码:
@Bean
Job job(JobBuilderFactory factory) {
return factory.get(JOB_NAME)
.start(stagingStep)
.next(analyzeStep)
.next(reportingStep)
.preventRestart()
.build();
}
我认为这可以通过多种方式完成..
1.Intercept 提到的工作 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.Implement你自己的trigger/scheduler...
<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.If只有一个tasklet需要再次重新运行,很简单,只是returnRepeatStatus.CONTINUABLE,这个任务重新运行一次又一次...
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext)throws Exception
{
return RepeatStatus.CONTINUABLE;//RepeatStatus.FINISHED;
}
并且如果您想要一些也可以完成的特定步骤(操作步骤 1 或 2 并使用特定步骤构建作业..再次 运行 之前)
有谁知道有没有办法在 Spring 批次中重新开始? 我希望它首先从第 1 步开始,然后是第 2 步、第 3 步,然后回到第 1 步、第 2 步、第 3 步,依此类推,直到满足条件。我尝试谷歌搜索,但未能找到任何具体示例。
到目前为止的代码:
@Bean
Job job(JobBuilderFactory factory) {
return factory.get(JOB_NAME)
.start(stagingStep)
.next(analyzeStep)
.next(reportingStep)
.preventRestart()
.build();
}
我认为这可以通过多种方式完成..
1.Intercept 提到的工作 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.Implement你自己的trigger/scheduler...
<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.If只有一个tasklet需要再次重新运行,很简单,只是returnRepeatStatus.CONTINUABLE,这个任务重新运行一次又一次...
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext)throws Exception
{
return RepeatStatus.CONTINUABLE;//RepeatStatus.FINISHED;
}
并且如果您想要一些也可以完成的特定步骤(操作步骤 1 或 2 并使用特定步骤构建作业..再次 运行 之前)