LG87
LG87

Reputation: 715

Spring batch - Rerun from first step

Does anyone know if there is a way to start over in Spring batch? I want it to first start at Step1, then Step2, Step3, then back to Step1, Step2, Step3, and so forth until a condition is met. I tried googling, but failed to fin any concrete examples.

Code so far:

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

            .preventRestart()
            .build();
}

Upvotes: 0

Views: 2037

Answers (1)

surya
surya

Reputation: 2749

I think this can be done in multiple ways..

1.Intercept the job as mentioned 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>

.. and implement your lister..

public interface JobExecutionListener {

void beforeJob(JobExecution jobExecution);

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

2.Implement your own 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>

You can use your own trigger and pass reference to above...

<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();
    }
}

and then if needed you can create a trigger

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

3.If only one tasklet needs to be rerun again again, its easy , just return RepeatStatus.CONTINUABLE, this task rerun again and again...

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

And if you want some specific step that can also be done (manipulate step 1 or 2 and use specific steps to build a job..before running again)

Upvotes: 1

Related Questions