本文介绍了改变石英作业计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在考虑使用Quartz安排我的应用程序,但在所有情况下,作业触发器似乎是一次性活动,并且对触发器的更改需要重新启动应用程序-seployed生效。
有没有办法可以让工作触发检查工作时间表的更改,而无需重新部署代码?
谢谢,
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,
推荐答案
- 陷阱某些用户驱动的事件,例如更新文本值,例如a用于安排工作的cron-string
- 找到并取消安排/删除旧作业并触发。
-
再次安排作业,使用新触发器。
- Trap some user-driven event, like updating a text value, for example a cron-string to schedule a job
- Locate and unschedule/delete the old job and trigger.
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);
}
这篇关于改变石英作业计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!