如何从命令行停止 Spring-boot 批处理作业?
How to stop a Spring-boot batch job from command line?
我能够使用 CommandLineRunner
.
通过命令行成功启动 springboot-batch 作业
代码:
@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();
}
}
}
作业已成功启动并且运行 使用此命令
java -jar <jarName> <jobName>
到目前为止一切顺利,但是是否有任何选项可以使用命令行停止此批处理?
Spring 批处理默认支持通过 arg 停止作业的选项,请查看以下文档:
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.
JobExplorer
和 JobOperator
都派上用场了。
终于得到了想要的效果。
@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();
}
}
}
}
要启动作业,使用这样的命令
java -jar <jarName> start <jobName>
并停止
java -jar <jarName> stop <jobName>
我能够使用 CommandLineRunner
.
代码:
@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();
}
}
}
作业已成功启动并且运行 使用此命令
java -jar <jarName> <jobName>
到目前为止一切顺利,但是是否有任何选项可以使用命令行停止此批处理?
Spring 批处理默认支持通过 arg 停止作业的选项,请查看以下文档:
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.
JobExplorer
和 JobOperator
都派上用场了。
终于得到了想要的效果。
@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();
}
}
}
}
要启动作业,使用这样的命令
java -jar <jarName> start <jobName>
并停止
java -jar <jarName> stop <jobName>