spring3.2+quartz2.21,怎么实现动态配置cronExpression,图片中为我目前的spring配置和java工作bean
<!--quartz-2.x的配置--><beanid="myJobTrigger"class="org.springframework.scheduling.quart...
<!-- quartz-2.x的配置 --> <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property name="cronExpression"> <value>0 0 0 * * ?</value> </property> </bean>怎么修改这个bean中的cronExpression表达式呢?
展开
1个回答
展开全部
在代码中改变的方案:
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;
/**
* by sleest 2016/05/16
*/
public class QuartzManager {
/**
* @param triggerName eg. "myJobTrigger"
* @param cronExpression eg. "0 0/5 0 * *" means every 5 min from 0:00 AM and ending at 0:55
* AM,everyday
*/
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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询