Chl*_*loe 5 java spring spring-boot
如何从命令行为长期运行的作业运行任务,例如 Spring Boot 中来自 Jenkins 的报告?我正在寻找类似于 Ruby on Rails Rake 任务的东西。Rake 任务在与 Web 服务器相同的应用程序上下文中从命令行执行代码,以便您可以重用代码。
我有一份报告,其中查询运行时间超过 30 秒,并生成了一个我想邮寄的 CSV 文件。我希望它每周使用 cron 或 Jenkins 自动运行。
我破解了一个解决方案。欢迎提出建议。
package com.example.tasks;
@Component
public class WeeklyReport implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
Arrays.asList(args).stream().forEach(a -> System.out.println(a));
if (!(args.length > 0 && args[0].equals("-task report:weekly"))) return;
System.out.println("weekly report");
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
并从“cron”作业运行它
$ mvn spring-boot:run -Drun.arguments="-task report:weekly"
Run Code Online (Sandbox Code Playgroud)
https://www.baeldung.com/spring-boot-command-line-arguments
https://therealdanvega.com/blog/2017/04/07/spring-boot-command-line-runner
| 归档时间: |
|
| 查看次数: |
1750 次 |
| 最近记录: |