Altering Quartz Job Schedule

萝らか妹 提交于 2019-12-23 01:37:32

问题



I'm looking into scheduling my application with Quartz, but in all cases, the job trigger seems to be a one-time activity, and changes to the trigger need the application to be re-deployed to take effect.
Is there any way I can have the job trigger check for changes to the job schedule without having to redeploy the code?
Thanks,


回答1:


  1. Trap some user-driven event, like updating a text value, for example a cron-string to schedule a job
  2. Locate and unschedule/delete the old job and trigger.
  3. Schedule the job again, using the new trigger.

    public static <T> T scheduleCronJob(Class<T> clazz, String cronString, String uid){
        try{            
            if(cronString == null){
                throw new CronStringConfigurationException();
            }
    
            String jobGroupName = "cronJobsGroup";
            String jobName = "cronJob" + uid;
            String triggerGroupName = "cronTriggers";
            String triggerName = "triggerFor" + uid;
    
            JobDetail jobDetail = new JobDetail(jobName, jobGroupName, clazz);
    
            CronTrigger trigger = new CronTrigger(
                    triggerName, triggerGroupName, 
                    jobName, jobGroupName, 
                    cronString);
    
            JobDataMap jobDataMap = new JobDataMap();
            jobDetail.setJobDataMap(jobDataMap);
    
            getScheduler().scheduleJob(jobDetail, trigger);
        } catch(Exception e){
        // print error message, throw stack trace
        }
        return null;
    }
    
    public static void reloadCronJob(Class clazz, String cronString, String uid) throws SystemException, ParseException, SchedulerException, 
        CronStringConfigurationException, PortalException{
        // locate the job 
        String jobGroupName = "cronJobs";
        String jobName = "jobFor" + uid;
    
        if(cronString == null){
            throw new CronStringConfigurationException();
        }
    
        JobDetail jobDetail = null;
        Class<?> jobClass = null;
    
        // remove the old job/trigger if it exists
        try{
            jobDetail = scheduler.getJobDetail(jobName, jobGroupName);
            if(jobDetail != null){
                jobClass = jobDetail.getJobClass();
            }
            scheduler.deleteJob(jobName, jobGroupName);
        } catch(Exception e){
            e.printStackTrace();
        }
    
        if(jobClass == null){
            jobClass = clazz;
        }
    
        // create a new trigger
        scheduleCronJob(jobClass, expandoColumnName, uid);
    
        System.out.println("(re)scheduled job " + jobName + " using new cron string " + cronString);
    }
    


来源:https://stackoverflow.com/questions/13320419/altering-quartz-job-schedule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!