java - 定制@Scheduled Spring

标签 java spring quartz-scheduler

我有一个方法 doJob() 每 10 秒运行一次,如下所示。 我想在运行时自定义运行周期。我的意思是,在下午 2 点到 3 点之间,doJob() 每 5 秒运行一次。下午 3 点至 4 点之间 doJob() 每 50 秒运行一次。

是否可以在运行时自定义运行周期?

@Scheduled(cron = "0/10 * * * * ?")
    public void doJob() {
}

在本文中http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06

Note that some scheduling requirements are too complicated to express with a single trigger - such as "every 5 minutes between 9:00 am and 10:00 am, and every 20 minutes between 1:00 pm and 10:00 pm". The solution in this scenario is to simply create two triggers, and register both of them to run the same job.

最佳答案

您将无法使用相同的作业,但肯定无法使用两个不同的作业

作业 1(下午 2 点至 3 点之间每 5 秒运行一次)

0/5 * 2-3 * * ?

作业 2(下午 3 点至 4 点之间每 50 秒运行一次)

0/50 * 3-4 * * ?

关于java - 定制@Scheduled Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625349/

相关文章:

java - 数据库连接失败时重试 Quartz Scheduler 启动

java - 本地方法调用中的 EJB 事务

java - mongodb Java 驱动程序 - 具有多个字段的 $group

java - 使用 Spring options 标签显示枚举的 toString 值

java - 一旦作业执行,Quartz Scheduler 会删除该文件吗?

java - 使用假日日历

java - 在spring boot中连接到多个数据库

Java:这足以删除对象吗?

java - 刷新 Grails applicationContext

java - Spring RMI 远程注解配置