从控制器运行 spring 批处理作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28566341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:22:35  来源:igfitidea点击:

run spring batch job from the controller

springspring-mvcspring-bootspring-batch

提问by Damian

I am trying to run my batch job from a controller. It will be either fired up by a cron job or by accessing a specific link. I am using Spring Boot, no XML just annotations.

我正在尝试从控制器运行我的批处理作业。它将被 cron 作业或通过访问特定链接启动。我正在使用 Spring Boot,没有 XML 只是注释。

In my current setting I have a service that contains the following beans:

在我当前的设置中,我有一个包含以下 bean 的服务:

@EnableBatchProcessing
@PersistenceContext
public class batchService {

    @Bean
    public ItemReader<Somemodel> reader() {
        ...
    }

    @Bean
    public ItemProcessor<Somemodel, Somemodel> processor() {
        return new SomemodelProcessor();
    }

    @Bean
    public ItemWriter writer() {
        return new CustomItemWriter();
    }

    @Bean
    public Job importUserJob(JobBuilderFactory jobs, Step step1) {
        return jobs.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .flow(step1)
                .end()
                .build();
    }

    @Bean
    public Step step1(StepBuilderFactory stepBuilderFactory,       
            ItemReader<somemodel> reader,
            ItemWriter<somemodel> writer,
            ItemProcessor<somemodel, somemodel> processor) {

        return stepBuilderFactory.get("step1")
                .<somemodel, somemodel> chunk(100)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }   
}   

As soon as I put the @Configurationannotation on top of my batchService class, job will start as soon as I run the application. It finished successfully, everything is fine. Now I am trying to remove @Configuration annotation and run it whenever I want. Is there a way to fire it from the controller?

一旦我将@Configuration注释放在我的 batchService 类的顶部,作业就会在我运行应用程序后立即开始。成功完成,一切正常。现在我正在尝试删除 @Configuration 注释并在需要时运行它。有没有办法从控制器触发它?

Thanks!

谢谢!

回答by Paul Deng

You need to create a application.yml file in the src/main/resources and add following configuration:

您需要在 src/main/resources 中创建一个 application.yml 文件并添加以下配置:

spring.batch.job.enabled: false

With this change, the batch job will not automatically execute with the start of Spring Boot. And batch job will be triggered when specific link.

通过此更改,批处理作业将不会在 Spring Boot 启动时自动执行。特定链接时会触发批处理作业。

Check out my sample code here: https://github.com/pauldeng/aws-elastic-beanstalk-worker-spring-boot-spring-batch-template

在此处查看我的示例代码:https: //github.com/pauldeng/aws-elastic-beanstalk-worker-spring-boot-spring-batch-template

回答by Andy Wilkinson

You can launch a batch job programmatically using JobLauncherwhich can be injected into your controller. See the Spring Batch documentationfor more details, including this example controller:

您可以使用JobLauncher可以将其注入控制器以编程方式启动批处理作业。有关更多详细信息,请参阅Spring Batch 文档,包括此示例控制器:

@Controller
public class JobLauncherController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @RequestMapping("/jobLauncher.html")
    public void handle() throws Exception{
        jobLauncher.run(job, new JobParameters());
    }
}

回答by Michael Minella

Since you're using Spring Boot, you should leave the @Configurationannotation in there and instead configure your application.properties to not launch the jobs on startup. You can read more about the autoconfiguration options for running jobs at startup (or not) in the Spring Boot documentation here: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-execute-spring-batch-jobs-on-startup

由于您使用的是 Spring Boot,您应该将@Configuration注解留在那里,而是将您的 application.properties 配置为在启动时不启动作业。您可以在此处的 Spring Boot 文档中阅读有关在启动时(或不)运行作业的自动配置选项的更多信息:http: //docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto -execute-spring-batch-jobs-on-startup