首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取命令行参数作为@Scheduled spring boot的spring批处理作业参数

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

Stack Overflow用户
提问于 2018-10-30 20:56:07
回答 1查看 4.3K关注 0票数 2

下面是我的spring boot主类,其中包含@Scheduled bean

代码语言:javascript
运行
复制
@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相同的带注释的方法不带任何参数。

EN

回答 1

Stack Overflow用户

发布于 2018-10-30 21:43:04

您可以注入ApplicationArguments类型的bean,并使用它来获取应用程序参数:

代码语言:javascript
运行
复制
@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部分中找到有关ApplicationArguments类型的更多详细信息。

希望这能有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53064804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档