java 如何在 Quartz 中重新安排作业执行间隔?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12257990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:12:21  来源:igfitidea点击:

How to reschedule the job execution interval in Quartz?

javaquartz-scheduler

提问by CKing

I am a bit new to Quartz. Is there a way to update the job execution interval for an already submitted Quartz job? Does this interval get updated immediately? Do you have to start the job once again after rescheduling it?

我对 Quartz 有点陌生。有没有办法更新已经提交的 Quartz 作业的作业执行间隔?这个间隔会立即更新吗?重新安排工作后是否必须再次开始工作?

I found the following link but I don't know which libraries is the code referring to since my quartz jars don't contain some of the classes used in the link. Also, where did the triggerKey method come from? Is this some kind of a static import?

我找到了以下链接,但我不知道代码指的是哪个库,因为我的石英罐不包含链接中使用的某些类。另外,triggerKey 方法从何而来?这是某种静态导入吗?

http://www.quartz-scheduler.org/documentation/2.4.0-SNAPSHOT/cookbook/UpdateTrigger.html

http://www.quartz-scheduler.org/documentation/2.4.0-SNAPSHOT/cookbook/UpdateTrigger.html

I want to update the job execution interval to a very large number in one of my JUnit test cases since I don't want the job to interfere with the state of the class under test. Once the test case completes, I want to reset the the job execution interval to the actual value that will be used in production

我想在我的一个 JUnit 测试用例中将作业执行间隔更新为一个非常大的数字,因为我不希望该作业干扰被测类的状态。测试用例完成后,我想将作业执行间隔重置为将在生产中使用的实际值

回答by Kris

You have to reschedule the job by creating a new trigger.

您必须通过创建新触发器来重新安排作业。

public void execute(JobExecutionContext context) throws JobExecutionException {
    Trigger newTrigger = what_ever_you_want;
    Trigger oldTrigger = context.getTrigger();
    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.rescheduleJob(oldTrigger.getKey(), newTrigger);
}

This will replace the same job with a new trigger fire time.

这将用新的触发器触发时间替换相同的作业。

回答by Alex Mi

Perhaps there is a static method triggerKey() somewhere in the Quartz library. However, I managed to reschedule an existing Quartz job (using Quartz 2.3.2) without using this (potential) method, but rather using the TriggerKey class as follows:

也许在 Quartz 库的某个地方有一个静态方法 triggerKey()。但是,我设法重新安排了现有的 Quartz 作业(使用 Quartz 2.3.2)而不使用此(潜在)方法,而是使用 TriggerKey 类,如下所示:

    boolean updateExisting = true; // try it also with false
    int aveId = 1234; // change it as you wish
    java.util.Date closeDate = new java.util.Date(); // change it as you wish

    SchedulerFactory sf = new StdSchedulerFactory("... /quartz_priority.properties");
    Scheduler scheduler = sf.getScheduler();

    TriggerKey triggerKey = new TriggerKey("trigger" + aveId, "group1");

    if (updateExisting) {
        Trigger oldTrigger = scheduler.getTrigger(triggerKey);
        TriggerBuilder oldTriggerBuilder = oldTrigger.getTriggerBuilder();

        Trigger newTrigger = oldTriggerBuilder.startAt(closeDate).build();
        scheduler.rescheduleJob(triggerKey, newTrigger);

    } else {
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(closeDate).build();

        // Define job instance
        JobDetail job1 = JobBuilder.newJob(<YOUR_JOB_CLASS_NAME>.class).withIdentity("job" + aveId, "group1").build();

        JobDataMap map = job1.getJobDataMap();
        map.put(<PARAMETER_NAME>, aveId);

        // Schedule the job with the trigger
        scheduler.scheduleJob(job1, trigger);
    }