如何在不使用@Scheduled()批注的情况下在春季启动中安排cron作业 [英] How to schedule a cron job in spring boot without using @Scheduled() annotation

查看:198
本文介绍了如何在不使用@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作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆