代码之家  ›  专栏  ›  技术社区  ›  Manas Saxena

获取命令行参数作为@Scheduled spring boot的spring批处理作业参数

  •  1
  • Manas Saxena  · 技术社区  · 7 年前

    @Scheduled

    @EnableScheduling
    @EnableBatchProcessing
    @SpringBootApplication(scanBasePackages = { "com.mypackage" })
    public class MyMain {
    
        @Autowired
        private JobLauncher jobLauncher;
    
        @Autowired
        private Job job;
    
    
        public static void main(String[] args) throws Exception {
    
            SpringApplication.run(MyMain.class, args);
        }
    
    
        @Scheduled(cron = "0 00 05 * * ?")
        private void perform() throws Exception {
            jobLauncher.run(job, new JobParameters());
        }
    }
    

    @预定的 带注释的方法不接受任何参数。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Mahmoud Ben Hassine    7 年前

    你可以注入 ApplicationArguments 并使用它获取应用程序参数:

    @EnableScheduling
    @EnableBatchProcessing
    @SpringBootApplication
    public class MyMain {
    
        @Autowired
        private JobLauncher jobLauncher;
    
        @Autowired
        private Job job;
    
        @Autowired
        private ApplicationArguments applicationArguments;
    
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(MyMain.class, args);
        }
    
        @Scheduled(cron = "0 00 05 * * ?")
        private void perform() throws Exception {
            String[] sourceArgs = applicationArguments.getSourceArgs();
            JobParameters jobParameters; // create job parameters from sourceArgs
            jobLauncher.run(job, jobParameters);
        }
    }
    

    你可以找到更多关于 应用参数 Accessing Application Arguments 章节。

    希望这有帮助。

    推荐文章