如何基于应用程序参数选择要运行的spring批处理作业-Spring Boot Java Config [英] how to select which spring batch job to run based on application argument - spring boot java config

查看:153
本文介绍了如何基于应用程序参数选择要运行的spring批处理作业-Spring Boot Java Config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一项目中有两个独立的spring batch作业,因为我想使用相同的与基础架构相关的bean.一切都用Java配置.我想知道是否存在适当的方式来独立启动作业,例如基于main方法中的第一个Java应用程序自变量.如果我运行SpringApplication.run,则只有第二个作业被魔术执行. 主要方法如下:

I have two independent spring batch jobs in the same project because I want to use the same infrastructure-related beans. Everything is configured in Java. I would like to know if there's a proper way to start the jobs independent based for example on the first java app argument in the main method for example. If I run SpringApplication.run only the second job gets executed by magic. The main method looks like:

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {                
        SpringApplication app = new SpringApplication(Application.class);
        app.setWebEnvironment(false);
        ApplicationContext ctx= app.run(args);              
    }

}

并按照Spring.io上的"Spring Batch入​​门"教程中的介绍配置这两个作业.这是第一个作业的配置文件,第二个以相同的方式配置.

and the two jobs are configured as presented in the Spring Batch Getting Started tutorial on Spring.io. Here is the configuration file of the first job, the second being configured in the same way.

@Configuration
@EnableBatchProcessing
@Import({StandaloneInfrastructureConfiguration.class, ServicesConfiguration.class})
public class AddPodcastJobConfiguration {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    //reader, writer, processor...

}

要启用模块化,我创建了一个AppConfig类,在其中定义了两个作业的工厂:

To enable modularization I created an AppConfig class, where I define factories for the two jobs:

@Configuration
@EnableBatchProcessing(modular=true)
public class AppConfig {

    @Bean
    public ApplicationContextFactory addNewPodcastJobs(){
        return new GenericApplicationContextFactory(AddPodcastJobConfiguration.class);
    }

    @Bean
    public ApplicationContextFactory newEpisodesNotificationJobs(){
        return new GenericApplicationContextFactory(NotifySubscribersJobConfiguration.class);
    }    

}

P.S.我是Java配置Spring Boot和Spring Batch中的Spring配置新手.

P.S. I am new to Spring configuration in Java configuration Spring Boot and Spring Batch...

推荐答案

要从main方法运行所需的作业,可以从应用程序上下文中加载所需的作业配置bean和JobLauncher,然后运行它:

To run the jobs you like from the main method you can load the the required job configuration bean and the JobLauncher from the application context and then run it:

@ComponentScan
@EnableAutoConfiguration
public class ApplicationWithJobLauncher {

    public static void main(String[] args) throws BeansException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException {

        Log log = LogFactory.getLog(ApplicationWithJobLauncher.class);

        SpringApplication app = new SpringApplication(ApplicationWithJobLauncher.class);
        app.setWebEnvironment(false);
        ConfigurableApplicationContext ctx= app.run(args);
        JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
        JobParameters jobParameters = new JobParametersBuilder()
            .addDate("date", new Date())
            .toJobParameters();  

        if("1".equals(args[0])){
            //addNewPodcastJob
            Job addNewPodcastJob = ctx.getBean("addNewPodcastJob", Job.class);          
            JobExecution jobExecution = jobLauncher.run(addNewPodcastJob, jobParameters);                   
        } else {
            jobLauncher.run(ctx.getBean("newEpisodesNotificationJob",  Job.class), jobParameters);   

        } 

        System.exit(0);
    }
}

引起我很多困惑的是,第二个作业被执行了,即使第一个作业似乎被跑步者捡起了"...问题是我在两个作业的配置文件中都使用了标准方法.命名writer(), reader(), processor() and step(),它使用了第二个作业中的内容,似乎覆盖"了第一个作业中的内容,而没有任何警告... 我虽然使用了@EnableBatchProcessing(modular=true)的应用程序配置类,但我认为它会被Spring Boot神奇地使用:

What was causing my lots of confusion was that the second job were executed, even though the first job seemed to be "picked up" by the runner... Well the problem was that in both job's configuration file I used standard method names writer(), reader(), processor() and step() and it used the ones from the second job that seemed to "overwrite" the ones from the first job without any warnings... I used though an application config class with @EnableBatchProcessing(modular=true), that I thought would be used magically by Spring Boot :

@Configuration
@EnableBatchProcessing(modular=true)
public class AppConfig {

    @Bean
    public ApplicationContextFactory addNewPodcastJobs(){
        return new GenericApplicationContextFactory(AddPodcastJobConfiguration.class);
    }

    @Bean
    public ApplicationContextFactory newEpisodesNotificationJobs(){
        return new GenericApplicationContextFactory(NotifySubscribersJobConfiguration.class);
    }    

}

准备就绪后,我会写一篇有关它的博客文章,但在此之前,该代码可在 https://www.ibm.com上找到. ://github.com/podcastpedia/podcastpedia-batch (工作/正在学习).

I will write a blog post about it when it is ready, but until then the code is available at https://github.com/podcastpedia/podcastpedia-batch (work/learning in progress)..

这篇关于如何基于应用程序参数选择要运行的spring批处理作业-Spring Boot Java Config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆