spring3.2+quartz2.21,怎么实现动态配置cronExpression,图片中为我目前的spring配置和java工作bean
答案:1 悬赏:20 手机版
解决时间 2021-04-29 07:01
- 提问者网友:不要迷恋哥
- 2021-04-28 21:15
spring3.2+quartz2.21,怎么实现动态配置cronExpression,图片中为我目前的spring配置和java工作bean
最佳答案
- 五星知识达人网友:人间朝暮
- 2021-04-28 22:14
在代码中改变的方案:
import java.text.ParseException;
import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.utils.Key;
public class QuartzManager {
public static void changeCronExpByName(String triggerName, String cronExpression) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
for (TriggerKey triggerKey : scheduler.getTriggerKeys(GroupMatcher
.triggerGroupEquals(Key.DEFAULT_GROUP))) {
if (triggerKey.getName().equals(triggerName)) {
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
CronExpression.validateExpression(cronExpression);
CronTrigger reTrigger = TriggerBuilder.newTrigger().forJob(trigger.getJobKey())
// associated with the same job
.withIdentity(triggerName, Key.DEFAULT_GROUP)
// name can be different from original
.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
scheduler.rescheduleJob(triggerKey, reTrigger);
break;
}
}
} catch (SchedulerException exp) {
// do sth,maybe throw RunEx
exp.printStackTrace();
} catch (ParseException exp) {
// do sth,maybe throw RunEx
exp.printStackTrace();
}
}
}
import java.text.ParseException;
import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.utils.Key;
public class QuartzManager {
public static void changeCronExpByName(String triggerName, String cronExpression) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
for (TriggerKey triggerKey : scheduler.getTriggerKeys(GroupMatcher
.triggerGroupEquals(Key.DEFAULT_GROUP))) {
if (triggerKey.getName().equals(triggerName)) {
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
CronExpression.validateExpression(cronExpression);
CronTrigger reTrigger = TriggerBuilder.newTrigger().forJob(trigger.getJobKey())
// associated with the same job
.withIdentity(triggerName, Key.DEFAULT_GROUP)
// name can be different from original
.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
scheduler.rescheduleJob(triggerKey, reTrigger);
break;
}
}
} catch (SchedulerException exp) {
// do sth,maybe throw RunEx
exp.printStackTrace();
} catch (ParseException exp) {
// do sth,maybe throw RunEx
exp.printStackTrace();
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯