代码之家  ›  专栏  ›  技术社区  ›  Subin Chalil

如何从命令行停止Spring引导批处理作业?

  •  1
  • Subin Chalil  · 技术社区  · 7 年前

    我能够通过命令行使用 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>
    

    到目前为止还不错,但是否有任何选项可以使用命令行停止此批处理?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Ashwini Rao    7 年前

    Spring batch默认支持通过arg停止作业的选项,请查看以下文档:

    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.
    
        2
  •  1
  •   Subin Chalil    7 年前

    二者都 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>