In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains why the Spring Batch framework job task only runs once. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why the Spring Batch framework job task only runs once.
Catalogue
The Spring Batch job task only runs once.
Cause of occurrence
Solution method
Start, stop, abandon job
1. Start a job
2. Stop a job
3. Give up a job
4. Failed a job
5. End a job
The Spring Batch job task only runs once.
In the process of actually using spring batch, in a scheduled task, there is no problem with the first execution of Job, and then the job task is not executed when it is executed again
Cause of occurrence
In view of this exception, you need to clarify the concept of Job Instance. Job Instance consists of the name of Job and the parameters that execute the job. When Job is executed, it will be considered to be the same Job instance because the parameters are the same. If the Job has been executed, an exception will be reported. The state that determines whether the Job has been executed is saved to the Job Repository.
Solution method
To make the execution parameters different, you can add a timestamp to the parameters
JobParametersBuilder builder = new JobParametersBuilder (); builder.addDate ("date", new Date ()); start, stop, abandon 1, start a job of job
There are at least two requirements for running a batch task: a JobLauncher and a job to run. They all contain the same or different context. For example, starting job from the command line initializes a JVM for each job, so each job will have its own JobLauncher; to start the job from the HttpRequest of the web container, usually just a JobLauncher to start the job,http request asynchronously will call this JobLauncher to start the job they need.
An example of starting job through web:
@ Controllerpublic class JobLauncherController {@ Autowired JobLauncher jobLauncher; @ Autowired Job job; @ RequestMapping ("/ jobLauncher.html") public void handle () throws Exception {jobLauncher.run (job, new JobParameters ());} 2, stop a job
Shutting down does not happen immediately because there is no way to force a task to stop immediately, especially when the task comes to the developer's own code snippet, where the framework is powerless, such as some business logic processing. As soon as control is returned to the framework, it immediately sets the current StepExecution to BachStatus.STOPPED, which means stop, save, and finally do the same for JobExecution before it is finished.
Set executions = jobOperator.getRunningExecutions ("sampleJob"); jobOperator.stop (executions.iterator () .next ())
Or implement it in the configuration file:
It allows the job to stay briefly so that the operator has time to do other operations, the stop element must be configured with the restart attribute, and when the job is restarted, it needs to be triggered manually to execute the step2.
3. Give up a job
The execution of a job will be restarted if it is rebootable after it is executed in the FAILED state. 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 in the middle of a restartable task execution: if a task encounters a step marked ABANDONED after the last execution failure, it will skip that step 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 system error), job will not run either, but JobRepository cannot detect this error because there is no notification of the process before it dies. You have to tell it manually that you know that the task has failed or that you are considering abandoning the task (setting its status to FAILED or ABANDONED)-this is a matter for the business logic layer and cannot make automatic decisions.
Only in non-restartable tasks need to be set to FAILED state, or you know that the data is still valid after restart. There is a series of tools in Spring Batch Admin, JobService, to cancel tasks that are in progress.
4. Failed a job
A failed job can be restarted because its status is FAILED, and if the step2 fails, an EARLY TERMINATION return code is returned and the step3 will not be executed. Otherwise, continue to execute step3
5. End a job
A finished job cannot be restarted because its status is COMPLETED. If the step2 fails, the step3 does not execute, and the job ends with COMPLETED. If step2 succeeds, proceed to step3.
At this point, I believe that everyone has a deeper understanding of why the Spring Batch framework job task only runs once, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.