获取命令行参数作为 @Scheduled spring boot 的 spring 批处理作业参数 [英] Get command line parameter as spring batch job parameters for @Scheduled spring boot
本文介绍了获取命令行参数作为 @Scheduled spring boot 的 spring 批处理作业参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的 spring boot 主类,我有 @Scheduled
beans
Below is my spring boot main class , where I have @Scheduled
beans
@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());
}
}
我将从命令行接收参数,我需要将其作为作业参数.我怎样才能实现与 @Scheduled
注释的方法一样不接受任何参数.
I will be receiving parameters from command line which I need to put as job parameters . How can I achieve the same as @Scheduled
annotated methods does not take in any parameter.
推荐答案
您可以注入 ApplicationArguments
类型的 bean 并使用它来获取应用程序参数:
You can inject a bean of type ApplicationArguments
and use it to get application arguments:
@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);
}
}
您可以在 访问应用程序参数 部分.
You can find more details about the ApplicationArguments
type in the Accessing Application Arguments section.
希望这会有所帮助.
这篇关于获取命令行参数作为 @Scheduled spring boot 的 spring 批处理作业参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文