1


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

4

1 回答 1

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);
    }
    
于 2013-02-01T19:23:54.290 回答