我能够使用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>
到目前为止一切顺利,但有没有选择使用命令行停止此批处理?
答案 0 :(得分:2)
默认情况下,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.
答案 1 :(得分:1)
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>