改变石英作业计划

时间:2012-11-10 08:13:37

标签: java quartz-scheduler


我正在考虑使用Quartz安排我的应用程序,但在所有情况下,作业触发器似乎是一次性活动,并且对触发器的更改需要重新部署应用程序才能生效。
有没有办法让工作触发器检查工作计划的更改,而无需重新部署代码? 谢谢,

1 个答案:

答案 0 :(得分:0)

  1. 捕获一些用户驱动的事件,例如更新文本值,例如用于安排作业的cron字符串
  2. 找到并取消预定/删除旧作业并触发。
  3. 使用新触发器再次安排作业。

    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);
    }