简体   繁体   中英

Spring Batch Job Stop Using jobOperator

I have Started my job using jobLauncher.run(processJob,jobParameters); and when i try stop job using another request jobOperator.stop(jobExecution.getId()); then get exeption:

org.springframework.batch.core.launch.JobExecutionNotRunningException: JobExecution must be running so that it can be stopped

Set<JobExecution> jobExecutionsSet= jobExplorer.findRunningJobExecutions("processJob");
        for (JobExecution jobExecution:jobExecutionsSet) {
            System.err.println("job status : "+ jobExecution.getStatus());
            if (jobExecution.getStatus()== BatchStatus.STARTED|| jobExecution.getStatus()== BatchStatus.STARTING || jobExecution.getStatus()== BatchStatus.STOPPING){
                jobOperator.stop(jobExecution.getId());
                System.out.println("###########Stopped#########");
            }
        }

when print job status always get job status: STOPPING but batch job is running

its web app, first upload some CSV file and start some operation using spring batch and during this execution if user need stop then stop request from another controller method come and try to stop running job

Please help me for stop running job

If you stop a job while it is running (typically in a STARTED state), you should not get this exception. If you have this exception, it means you have stopped your job while it is currently stopping (that is what the STOPPING status means).

jobExplorer.findRunningJobExecutions returns only running executions, so if in the next line right after this one you have a job in STOPPING status, this means the status changed right after calling jobExplorer.findRunningJobExecutions . You need to be aware that this is possible and your controller should handle this case.

When you tell spring batch to stop a job it goes into STOPPING mode. What this means is it will attempt to complete the unit of work chunk it is currently processing but then stop working. Likely what's happening is you are working on a long running task that is not finishing a unit of work (is it hung?) so it can't move from STOPPING to STOPPED.

Doing it twice rightly leads to an Exception because your job is already STOPPING by the time you did it the first time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM