How to reschedule the job execution interval in Quartz?

后端 未结 2 1799
名媛妹妹
名媛妹妹 2020-12-30 13:54

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

相关标签:
2条回答
  • 2020-12-30 14:23

    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.

    0 讨论(0)
  • 2020-12-30 14:38

    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:

        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);
        }
    
    0 讨论(0)
提交回复
热议问题