我在同一个项目中有两个独立的 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);
}
}
并且这两个作业的配置如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...
}
为了启用模块化,我创建了一个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);
}
}
附:我是Java配置Spring Boot和Spring Batch中的Spring配置新手...
3 回答
只需设置“spring.batch.job.names = myJob”属性即可 . 您可以在启动应用程序时将其设置为SystemProperty(-Dspring.batch.job.names = myjob) . 如果已定义此属性,则spring-batch-starter将仅启动由此属性定义的作业 .
要从main方法运行您喜欢的作业,您可以从应用程序上下文加载所需的作业配置Bean和JobLauncher,然后运行它:
造成我很多困惑的原因是第二份工作被执行了,即使第一份工作似乎是由跑步者"picked up" ...问题是在两个工作的配置文件中我使用标准方法名称
writer(), reader(), processor() and step()
它使用了来自第一份工作的第二份工作中没有任何警告的那些......我使用了@EnableBatchProcessing(modular=true)
的应用程序配置类,我认为它会被Spring Boot神奇地使用:我会在准备好的时候写一篇关于它的博客文章,但在此之前,代码可以在https://github.com/podcastpedia/podcastpedia-batch(工作/学习进行中)中找到 .
有CommandLineJobRunner,也许可以帮助 .
从它的javadoc