具有来自数据库的自定义触发时间的调度程序
Scheduler with custom fire time from DB
我需要编写一个调度程序(spring 4 个应用程序)。
我在数据库中有很多触发时间(开始时间)。我可以通过 UI 手动 create/delete 它们。对于每个开始时间,我都需要调用该方法。
我不能使用原生@Scheduled的原因是:
1) 我的任务不是重复的(但这没什么大不了的);
2)我需要手动处理开始时间。它不应该被硬编码或设置在属性中。我需要在不重新部署的情况下随时从 UI 添加新触发器的可能性。
关于 Quartz 触发器,我不确定它是否符合我的问题。如我所见:我需要编写一些服务,每隔几秒就会根据数据库的时间相应地更新所有触发器。
那么,解决我的问题的最佳方法是什么?
可以更新现有的触发器或将其替换为另一个带有 quartz 的触发器。因此,当您的 HMI update/add 触发时,它会在您的数据库中 update/add 并在调度程序中 update/add。
更新现有触发器:
// retrieve the trigger
Trigger oldTrigger = sched.getTrigger(triggerKey("oldTrigger", "group1");
// obtain a builder that would produce the trigger
TriggerBuilder tb = oldTrigger.getTriggerBuilder();
// update the schedule associated with the builder, and build the new trigger
// (other builder methods could be called, to change the trigger in any desired way)
Trigger newTrigger = tb.withSchedule(simpleSchedule()
.withIntervalInSeconds(10)
.withRepeatCount(10)
.build();
//Reschedule
sched.rescheduleJob(oldTrigger.getKey(), newTrigger);
更换触发器:
// Define a new Trigger
Trigger trigger = newTrigger()
.withIdentity("newTrigger", "group1")
.startNow()
.build();
// tell the scheduler to remove the old trigger with the given key, and put the new one in its place
sched.rescheduleJob(triggerKey("oldTrigger", "group1"), trigger);
Those examples are from the quartz documentation.
如果您选择使用 JDBC JobStore,请不要 write/update 直接在石英 table.
中添加任何内容
您不应该访问 JobStore(对于任何类型的 JobStore)您必须仅使用调度程序界面。
我需要编写一个调度程序(spring 4 个应用程序)。
我在数据库中有很多触发时间(开始时间)。我可以通过 UI 手动 create/delete 它们。对于每个开始时间,我都需要调用该方法。
我不能使用原生@Scheduled的原因是:
1) 我的任务不是重复的(但这没什么大不了的);
2)我需要手动处理开始时间。它不应该被硬编码或设置在属性中。我需要在不重新部署的情况下随时从 UI 添加新触发器的可能性。
关于 Quartz 触发器,我不确定它是否符合我的问题。如我所见:我需要编写一些服务,每隔几秒就会根据数据库的时间相应地更新所有触发器。
那么,解决我的问题的最佳方法是什么?
可以更新现有的触发器或将其替换为另一个带有 quartz 的触发器。因此,当您的 HMI update/add 触发时,它会在您的数据库中 update/add 并在调度程序中 update/add。
更新现有触发器:
// retrieve the trigger
Trigger oldTrigger = sched.getTrigger(triggerKey("oldTrigger", "group1");
// obtain a builder that would produce the trigger
TriggerBuilder tb = oldTrigger.getTriggerBuilder();
// update the schedule associated with the builder, and build the new trigger
// (other builder methods could be called, to change the trigger in any desired way)
Trigger newTrigger = tb.withSchedule(simpleSchedule()
.withIntervalInSeconds(10)
.withRepeatCount(10)
.build();
//Reschedule
sched.rescheduleJob(oldTrigger.getKey(), newTrigger);
更换触发器:
// Define a new Trigger
Trigger trigger = newTrigger()
.withIdentity("newTrigger", "group1")
.startNow()
.build();
// tell the scheduler to remove the old trigger with the given key, and put the new one in its place
sched.rescheduleJob(triggerKey("oldTrigger", "group1"), trigger);
Those examples are from the quartz documentation.
如果您选择使用 JDBC JobStore,请不要 write/update 直接在石英 table.
中添加任何内容您不应该访问 JobStore(对于任何类型的 JobStore)您必须仅使用调度程序界面。