Run Spring Batch Job programmatically?

前端 未结 3 1943
礼貌的吻别
礼貌的吻别 2021-02-02 15:50

I have a Spring Batch application, which I start with the CommandLineJobRunner. But now I have to embed this application into our corporate environment. There we ha

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 16:00

    Using a Spring Boot Application if you don't want to use the CommandLineRunner (for some reason or if you need some custom logic), you can always do something like :

    public static void main(String[] args) {
    
        SpringApplication app = new SpringApplication(YourApplication.class);
        app.setWebEnvironment(false);
        ConfigurableApplicationContext ctx = app.run(args);
    
        JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
        Job job = ctx.getBean("your-job-here", Job.class);
        JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
    
    
        JobExecution jobExecution = jobLauncher.run(job, jobParameters);
        BatchStatus batchStatus = jobExecution.getStatus();
    }
    

提交回复
热议问题