Rich
Rich

Reputation: 15757

Stopping SpringBatch jobs started from the command line

Spring Batch jobs can be started from the commandline by telling the JVM to run CommandLineJobRunner. According to the JavaDoc, running the same command with the added parameter of -stop will stop the Job:

The arguments to this class can be provided on the command line (separated by spaces), or through stdin (separated by new line). They are as follows:

jobPath jobIdentifier (jobParameters)* The command line options are as follows

  • jobPath: the xml application context containing a Job
  • -restart: (optional) to restart the last failed execution
  • -stop: (optional) to stop a running execution
  • -abandon: (optional) to abandon a stopped execution
  • -next: (optional) to start the next in a sequence according to the JobParametersIncrementer in the Job jobIdentifier: the name of the job or the id of a job execution (for -stop, -abandon or -restart).
  • jobParameters: 0 to many parameters that will be used to launch a job specified in the form of key=value pairs.

However, on the JavaDoc for the main() method the -stop parameter is not specified. Looking through the code on docjar.com I can't see any use of the -stop parameter where I would expect it to be.

I suspect that it is possible to stop a batch that has been started from the command line but only if the batches being run are backed by a non-transient jobRepository? If running a batch on the command line that only stores its data in HSQL (ie in memory) there is no way to stop the job other than CTRL-C etc?

Upvotes: 1

Views: 7632

Answers (2)

Ickster
Ickster

Reputation: 2217

The stop switch will work, but it will only stop the job after the currently executing step completes. It won't kill the job immediately.

Upvotes: 1

Michael Pralow
Michael Pralow

Reputation: 6630

stop command is implemented, see source for CommandLineJobRunner, line 300+

        if (opts.contains("-stop")) {
            List<JobExecution> jobExecutions = getRunningJobExecutions(jobIdentifier);
            if (jobExecutions == null) {
                throw new JobExecutionNotRunningException("No running execution found for job=" + jobIdentifier);
            }
            for (JobExecution jobExecution : jobExecutions) {
                jobExecution.setStatus(BatchStatus.STOPPING);
                jobRepository.update(jobExecution);
            }
            return exitCodeMapper.intValue(ExitStatus.COMPLETED.getExitCode());
        }

Upvotes: 1

Related Questions