Spring4 Quartz2 动态任务,Spring4整合quartz2.2.3简单动态任务
Posted 蕃薯耀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring4 Quartz2 动态任务,Spring4整合quartz2.2.3简单动态任务相关的知识,希望对你有一定的参考价值。
Spring4 Quartz2 动态任务
Spring4整合quartz2.2.3简单动态任务,
Quartz2.2.3简单动态定时任务二,
SimpleScheduleBuilder简单定时任务
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
此处使用的是Quartz中SimpleScheduleBuilder类,非CronScheduleBuilder,CronScheduleBuilder是Cron表达式的。具体请自行百度。
- /**
- * 新增任务
- * @param scheduleJob
- * @throws Exception
- */
- @Override
- @SuppressWarnings("unchecked")
- public void addJobSimple(ScheduleJob scheduleJob) throws Exception{
- TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- //任务触发
- Trigger trigger = scheduler.getTrigger(triggerKey);
- if (null == trigger) {
- JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(scheduleJob.getClazz()))
- .withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
- jobDetail.getJobDataMap().put("scheduleJob", scheduleJob);
- SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
- if("秒".equals(scheduleJob.getTimeType()) ||
- "second".equalsIgnoreCase(scheduleJob.getTimeType())){
- simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever();
- }else if("分".equals(scheduleJob.getTimeType()) || "分钟".equals(scheduleJob.getTimeType()) ||
- "minute".equalsIgnoreCase(scheduleJob.getTimeType())){
- simpleScheduleBuilder.withIntervalInMinutes(scheduleJob.getTimeValue()).repeatForever();
- }else if("时".equals(scheduleJob.getTimeType()) || "小时".equals(scheduleJob.getTimeType()) ||
- "hour".equalsIgnoreCase(scheduleJob.getTimeType())){
- simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).repeatForever();
- }else if("天".equals(scheduleJob.getTimeType()) ||
- "date".equalsIgnoreCase(scheduleJob.getTimeType())){
- simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).repeatForever();
- }
- ScheduleBuilder<SimpleTrigger> schedBuilder = simpleScheduleBuilder.withMisfireHandlingInstructionNextWithRemainingCount();
- trigger =TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup())
- .withSchedule(schedBuilder).build();
- trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(schedBuilder).build();
- scheduler.scheduleJob(jobDetail, trigger);
- log.info(CC.LOG_PREFIX + "新增简单任务:"+JasonUtils.Object2String(scheduleJob));
- }else {
- this.updateJobCron(scheduleJob);
- }
- }
需要注意是的,当定时任务只执行一次时,就要仔细看代码,如上面的简单定时任务,刚开始是我这样的:
- simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue());
结果只执行一次,而我需要的是不断循环执行,最后改成:
- simpleScheduleBuilder.withIntervalInSeconds(scheduleJob.getTimeValue()).repeatForever();
可以看出,后面多了个:repeatForever(),表示永远重复。
如果是希望执行多少次后不执行,就使用:.withRepeatCount(triggerRepeatCount),如下:
- simpleScheduleBuilder.withIntervalInHours(scheduleJob.getTimeValue()).withRepeatCount(triggerRepeatCount);
(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
以上是关于Spring4 Quartz2 动态任务,Spring4整合quartz2.2.3简单动态任务的主要内容,如果未能解决你的问题,请参考以下文章