spring环境:
JOB类
package com.chenjun.quartz_1.springenv;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;public class SimpleJob extends QuartzJobBean{ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { String message = context.getJobDetail().getJobDataMap().getString("message"); System.out.println(message); }}
JobDetail类
package com.chenjun.quartz_1.springenv;import java.text.ParseException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.quartz.CronExpression;import org.quartz.CronTrigger;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.StatefulJob;import org.quartz.impl.StdScheduler;public class CronJobDetail implements StatefulJob{ /*** 日志 */ private static final Log LOG = LogFactory.getLog(StatefulJob.class); public void execute(JobExecutionContext context) throws JobExecutionException { StdScheduler stdScheduler = (StdScheduler)context.getScheduler(); CronTrigger cronTrigger = (CronTrigger)context.getTrigger(); System.out.println("JOB执行.."+cronTrigger.getCronExpression()); CronTrigger trigger = null; try { trigger = (CronTrigger) stdScheduler.getTrigger("cronJobTrigger", Scheduler.DEFAULT_GROUP); CronTrigger newTriggerIns = new CronTrigger(); newTriggerIns.setJobName(cronTrigger.getJobName()); newTriggerIns.setName("abc"); //模拟从数据库里面查出的时间表达式 newTriggerIns.setCronExpression(new CronExpression("0/5 * * * * ?")); stdScheduler.rescheduleJob("cronJobTrigger", null, newTriggerIns); } catch (SchedulerException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } }}
启动类:
package com.chenjun.quartz_1.springenv;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SimpleSpringQuartz{ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); }}
结果:
搞定了