如何根据应用程序参数选择要运行的 spring 批处理作业 - spring boot java 配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25122103/
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
how to select which spring batch job to run based on application argument - spring boot java config
提问by amacoder
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.runonly the second job gets executed by magic.
The main method looks like:
我在同一个项目中有两个独立的 spring 批处理作业,因为我想使用相同的与基础设施相关的 bean。一切都在Java中配置。我想知道是否有一种正确的方法可以根据例如 main 方法中的第一个 java app 参数来独立启动作业。如果我SpringApplication.run只运行第二个工作就会被魔法执行。主要方法如下所示:
@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);
}
}
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.
并且这两个作业的配置如 Spring.io 上的 Spring Batch 入门教程中所述。这是第一个作业的配置文件,第二个以相同的方式配置。
@Configuration
@EnableBatchProcessing
@Import({StandaloneInfrastructureConfiguration.class, ServicesConfiguration.class})
public class AddPodcastJobConfiguration {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory stepBuilderFactory;
//reader, writer, processor...
}
To enable modularization I created an AppConfig class, where I define factories for the two jobs:
为了实现模块化,我创建了一个 AppConfig 类,我在其中为两个作业定义了工厂:
@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. I am new to Spring configuration in Java configuration Spring Boot and Spring Batch...
PS我是Java配置Spring Boot和Spring Batch中Spring配置的新手......
回答by Hansjoerg Wingeier
Just set the "spring.batch.job.names=myJob" property. You could set it as SystemProperty when you launch your application (-Dspring.batch.job.names=myjob). If you have defined this property, spring-batch-starter will only launch the jobs, that are defined by this property.
只需设置“spring.batch.job.names=myJob”属性。您可以在启动应用程序时将其设置为 SystemProperty (-Dspring.batch.job.names=myjob)。如果您已定义此属性,则 spring-batch-starter 将仅启动此属性定义的作业。
回答by amacoder
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:
要从 main 方法运行您喜欢的作业,您可以从应用程序上下文加载所需的作业配置 bean 和 JobLauncher,然后运行它:
@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);
}
}
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 :
导致我很困惑的是第二个工作被执行了,即使第一个工作似乎被跑步者“捡起”......问题是在这两个工作的配置文件中我使用了标准方法名称writer(), reader(), processor() and step(),它使用了第二份工作中的那些似乎在没有任何警告的情况下“覆盖”了第一份工作中的那些......我使用了一个带有 的应用程序配置类@EnableBatchProcessing(modular=true),我认为它会被 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);
}
}
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)..
当它准备好时,我会写一篇关于它的博客文章,但在那之前,代码可以在https://github.com/podcastpedia/podcastpedia-batch(工作/学习进行中)获得。
回答by Luca Basso Ricci
There is the CommandLineJobRunnerand maybe can be helpful.
From its javadoc
有CommandLineJobRunner可能会有所帮助。
从它的javadoc
Basic launcher for starting jobs from the command line
用于从命令行启动作业的基本启动器
回答by Abdessamad ABOUKDIR
Spring Batch auto configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context. By default it executes all Jobs in the application context on startup (see JobLauncherCommandLineRunner for details). You can narrow down to a specific job or jobs by specifying spring.batch.job.names (comma separated job name patterns).
通过在上下文中的某处添加 @EnableBatchProcessing(来自 Spring Batch)来启用 Spring Batch 自动配置。默认情况下,它在启动时执行应用程序上下文中的所有作业(有关详细信息,请参阅 JobLauncherCommandLineRunner)。您可以通过指定 spring.batch.job.names(逗号分隔的作业名称模式)来缩小特定作业的范围。
Or disable the auto execution and run the jobs programmatically from the context using a JobLauncher based on the args passed to the main method
或者禁用自动执行并使用基于传递给 main 方法的参数的 JobLauncher 从上下文以编程方式运行作业

