要从main方法运行所需的作业,可以从应用程序上下文中加载所需的作业配置bean和JobLauncher,然后运行它:
@ComponentScan@EnableAutoConfigurationpublic 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() andstep(),使用了第二个作业中的那些似乎没有任何警告就“覆盖”了第一个作业中的那些…我使用了带有的应用程序配置类
@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); }}准备就绪时,我将写一篇有关它的博客文章,但是在此之前,可在https://github.com/podcastpedia/podcastpedia-
batch(工作/学习进行中)中找到该代码。



