Solve the problem that Spring Batch framework job task only runs once

  • 2021-11-10 09:38:23
  • OfStack

Directory Spring Batch job task only runs once. Cause solution job start, stop, abandon 1, start 1 job2, stop 1 job3, abandon 1 job4, fail 1 job5, end 1 job

Spring Batch job task only runs once

In the process of actually using spring batch for one time, there is no problem in executing Job for the first time, and then job task will not be executed when executing again;

Causes of occurrence

In view of this exception, it is necessary to clarify the concept of Job Instance. Job Instance is composed of the name of Job and the parameters for executing job. When Job is executed, it will be considered as the same instance of Job because of the same parameters. If the Job has been executed, an exception will be reported. The state of judging whether Job has been executed is stored in Job Repository.

Solution

To make the execution parameters different, you can add 1 timestamp to the parameters


 JobParametersBuilder builder = new JobParametersBuilder();      
                       builder.addDate("date", new Date());

Start, stop and abandon of job

1. Start an job

There are at least two requirements for running a batch task: an JobLauncher and an job to run. They all contain the same or different context. For example, starting job from the command line initializes one JVM for every job, so each job will have its own JobLauncher;; Starting an job from an HttpRequest of the web container is generally just an JobLauncher to start an job asynchronously, and http requests call this JobLauncher to start the job they need.

Example of starting job through web:


@Controller
public class JobLauncherController {
    @Autowired
    JobLauncher jobLauncher;
    @Autowired
    Job job;
    @RequestMapping("/jobLauncher.html")
    public void handle() throws Exception{
      jobLauncher.run(job, new JobParameters());
    }
}

2. Stop 1 job

Shutdown does not happen immediately, because there is no way to force a task to stop immediately, especially when the task goes to the developer's own code segment, and the framework is powerless at this moment, such as a business logic processing. The 1-denier control is returned to the framework, which immediately sets the current StepExecution to BachStatus. STOPPED, meaning stop, then save, and finally does the same on JobExecution before completion.


Set<Long> executions = jobOperator.getRunningExecutions("sampleJob");
jobOperator.stop(executions.iterator().next());

Or in a configuration file:

It allows the job to stay briefly so that the operator has time for other operations. The stop element must be configured with the restart attribute. When the job is restarted, it needs to be triggered manually to execute step2.


<step id="step1" parent="s1">
    <stop on="COMPLETED" restart="step2"/>
</step>
<step id="step2" parent="s2"/>

3. Abandon 1 job

1 Execution of an job When executed to the FAILED state, if it is rebootable, it will be restarted. If the execution state of the task is ABANDONED, the framework will not restart it. The ABANDONED state also applies to execution steps, so that they can be skipped, even during a rebootable task execution: if a step marked as ABANDONED after the last execution failure is encountered during task execution, it will be skipped directly to the next step (this is determined by the task flow definition and the exit code of the execution step).

If the current system process dies ("kill-9" or a system error), job will not run, but JobRepository cannot detect this error because the process was not notified before it died. You must tell it manually whether you know the task has failed or consider abandoning it (set its status to FAILED or ABANDONED)-this is a business logic layer matter, and there is no automatic decision.

FAILED status is required only for tasks that cannot be restarted, or you know that the data is still valid after restarting. Spring Batch Admin has a family of tools, JobService, to cancel a task in progress.

4. Failed 1 job

A failed job can be restarted because its status is FAILED. If step2 fails, a return code of EARLY TERMINATION is returned, and step3 will not execute. Otherwise, continue to execute step3


<step id="step1" parent="s1" next="step2">
<step id="step2" parent="s2">
    <fail on="FAILED" exit-code="EARLY TERMINATION"/>
    <next on="*" to="step3"/>
</step>
<step id="step3" parent="s3">

5. End 1 job

An job that has ended cannot be restarted because its status is COMPLETED. If step2 fails, step3 does not execute, and the job is COMPLETED, ending. If step2 succeeds, proceed to step3.


<step id="step1" parent="s1" next="step2">
<step id="step2" parent="s2">
    <end on="FAILED"/>
    <next on="*" to="step3"/>
</step>
<step id="step3" parent="s3">

Related articles: