当前位置: 首页 > 知识库问答 >
问题:

如何从命令行停止Spring-boot批处理作业?

申屠飞
2023-03-14

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

共有1个答案

吕成业
2023-03-14

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

https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/launing/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.
 类似资料:
  • 我不知道如何使用调用Spring批处理中定义的作业,文档细节对我来说是不够的。 我遵循了Spring Batch官方指南,使用Java注释(例如)在Spring Batch中编写作业,因为我希望避免使用XML配置文件来描述作业、步骤等。 到目前为止我已经: 配置类(见下文),我使用AnnotaIon将定义、、、和(带有的所有内容放入其中。 具有方法的类,该方法具有并具有注释,以导入处理作业中的数据

  • 问题内容: 运行main方法时,将执行作业。这样我无法弄清楚如何控制作业的执行。例如,您如何安排作业,访问作业执行或设置作业参数的方式。 我试图注册自己的JobLauncher 但是当我尝试在主要方法中使用它时: 当加载上下文时,该作业再次执行,而当我尝试手动运行它时,我得到了。有没有办法防止自动作业执行? 问题答案: 通过设置可以防止作业执行 在application.properties中。或

  • 我按照这个示例使用Boot进行Spring批处理。 运行main方法时,作业将执行。这样我就不知道如何控制作业的执行了。例如如何排定作业、访问作业执行或设置作业参数。 我尝试注册自己的JobLauncher 但当我尝试在主法中使用时: 当加载上下文时,再次执行作业,并且尝试手动运行作业时得到。有没有办法防止自动执行作业?

  • 我最近开始使用java配置方式编写spring批处理程序,并使用spring批处理和starter包。我使用了分区的步骤和任务执行器来完成我的工作,我面临的问题是,一旦作业完成,批处理过程就不会停止,它一直在我的eclipse和Linux盒子中运行。我手动找到并终止作业。你能帮个忙吗。当我在没有分区步骤的情况下以单线程的方式运行作业时,这工作很好。 我的作业配置:

  • 我已经使用开始我的作业,当我尝试使用另一个请求停止作业时,然后获取exeption: JobExecutionNotrunningException:JobExecution必须正在运行,才能停止 当打印作业状态总是获取但批处理作业正在运行时 它的web应用程序,首先上传一些CSV文件,并使用spring batch启动一些操作,在执行过程中,如果用户需要停止,则从另一个控制器方法来停止请求,并试