如何在不使用@Scheduled()批注的情况下在春季启动中安排cron作业

编程入门 行业动态 更新时间:2024-10-15 06:13:04
本文介绍了如何在不使用@Scheduled()批注的情况下在春季启动中安排cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Spring Boot中,我可以通过不对方法使用@Scheduled批注来安排Spring作业吗?

In spring boot, can I schedule a spring job by not using @Scheduled annotation to a method?

我正在春季靴中从事春季工作.我想使用cron表达式来计划作业,但不对方法使用@Scheduled(cron = " ")批注.

I am working with spring job in the spring boot. I want to schedule a job by using cron expression, but without using @Scheduled(cron = " ") annotation to the method.

我知道我可以按照以下方法安排工作.

I know that I can schedule a job inside this method as below.

@Scheduled (cron = "0 10 10 10 * ?") public void execute() { / * some job code * / }

但是我希望它是动态的,以便我可以将cron表达式用作用户的输入并安排它.

But I want it to be dynamic so that I can take a cron expression as input from the user and schedule it.

推荐答案

我想出了一个有效的示例,因为我发现您的问题很有趣并且以前对此问题感兴趣.它完全基于源代码,因此我不知道它是否接近遵循最佳实践.尽管如此,您仍可以根据需要进行调整.仅供参考,您不一定需要创建一个新的ScheduledTaskRegistrar对象-我认为由于您的目标是一个动态调度程序,因此您对仅使用覆盖方法定义任务就不会有兴趣.

I came up with a working example since I found your question interesting and have been interested in this problem before. It's based entirely on the source code so I have no idea if it comes close to following best practice. Nonetheless, you may be able to tune it to your needs. FYI, you don't necessarily need to create a new ScheduledTaskRegistrar object - I figured that since your objective is a dynamic scheduler, you wouldn't be interested in defining your tasks purely in the overwritten method.

@SpringBootApplication public class TaskScheduler implements SchedulingConfigurer, CommandLineRunner { public static void main(String[] args){SpringApplication.run(TaskScheduler.class, args);} List<CronTask> cronTasks; @Override public void run(String... args) throws Exception { CronTask task = this.createCronTask(new Runnable() { @Override public void run() { System.out.println(LocalDateTime.now()); } }, "1/10 * * * * *"); ScheduledTaskRegistrar taskRegistrar = new ScheduledTaskRegistrar(); taskRegistrar.addCronTask(task); configureTasks(taskRegistrar); Thread.sleep(51); taskRegistrar.destroy(); taskRegistrar = null; ScheduledTaskRegistrar taskRegistrar2 = new ScheduledTaskRegistrar(); taskRegistrar2.addCronTask(task); configureTasks(taskRegistrar2); } @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // "Calls scheduleTasks() at bean construction time" - docs taskRegistrar.afterPropertiesSet(); } public CronTask createCronTask(Runnable action, String expression) { return new CronTask(action, new CronTrigger(expression)); } }

我有在Azure和其他地方使用cron作业的经验.在Java编程中,为了简单起见,我通常使用固定时间的@Scheduled.希望这对您有用.

I have experience using cron jobs in Azure and other places. Programming in Java, I have typically used @Scheduled with fixed times just for the sake of simplicity. Hope this is useful to you though.

更多推荐

如何在不使用@Scheduled()批注的情况下在春季启动中安排cron作业

本文发布于:2023-11-24 00:14:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   春季   情况下   如何在   Scheduled

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!