Subin Chalil
Subin Chalil

Reputation: 3660

How to stop a Spring-boot batch job from command line?

I was able to successfully launch a springboot-batch job through command line using CommandLineRunner.

Code :

@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
    @Autowired
    ApplicationContext context;
    @Override
    public void run(String...args) throws Exception {
        JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        Job myJob = context.getBean(args[0], Job.class);
        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();
        try {
            jobLauncher.run(myJob, jobParameters);
        } catch (JobExecutionAlreadyRunningException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JobRestartException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Job is successfully started and running using this command

java -jar <jarName> <jobName>

So far so good, But is there any option to stop this batch using command line?

Upvotes: 1

Views: 4604

Answers (2)

Subin Chalil
Subin Chalil

Reputation: 3660

Both JobExplorer and JobOperator came in handy for me.

Finally got the desired working.

@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
    @Autowired
    ApplicationContext context;

    @Autowired
    JobExplorer jobExplorer;

    @Autowired
    JobOperator jobOperator;
    @Override
    public void run(String...args) throws Exception {

        if (args!=null&&args.length<2){
            System.out.println("################Jar requires two or more command line args.###########");
            return;
        }
        //Stopping Job
        if(args[0].equals("stop")){

            Set<JobExecution> jobExecutionsSet= jobExplorer.findRunningJobExecutions(args[1]);
            for (JobExecution jobExecution:jobExecutionsSet) {
                System.out.println(jobExecution.getStatus()+"ID :"+jobExecution.getId());
                if (jobExecution.getStatus()== BatchStatus.STARTED|| jobExecution.getStatus()== BatchStatus.STARTING){
                    jobOperator.stop(jobExecution.getId());
                    System.out.println("###########Stopped#########");
                }
            }
            System.out.println("EXITING JOB");
            return;
        }
        else if(args[0].equals("start")){
            JobLauncher jobLauncher = context.getBean(JobLauncher.class);
            Job establishmentJob = context.getBean(args[1], Job.class);
            JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();
            try {
                jobLauncher.run(establishmentJob, jobParameters);
            } catch (JobExecutionAlreadyRunningException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JobRestartException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JobInstanceAlreadyCompleteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JobParametersInvalidException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

To start the job, use the command like this

java -jar <jarName> start <jobName>

And to stop

java -jar <jarName> stop <jobName>

Upvotes: 1

Ashwini Rao
Ashwini Rao

Reputation: 555

Spring batch by default supports option to stop the job via arg, check out the documentation below:

https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/launch/support/CommandLineJobRunner.html

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

jobPath: the xml application context containing a Job

-stop: (optional) to stop a running execution
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.

Upvotes: 2

Related Questions