package com.mc.bsframe.job;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.TriggerKey;import org.quartz.impl.triggers.SimpleTriggerImpl;import org.springframework.beans.factory.annotation.Autowired;import com.mc.bsframe.service.TestService;import com.mc.bsframe.util.TimeUtils;public class TestJob { @Autowired private TestService testSevice; public void doSomething() { testSevice.sayHi(); System.err.println("****:" + TimeUtils.getCurrentTime()); } private Scheduler scheduler; public Scheduler getScheduler() { return scheduler; } public void setScheduler(Scheduler scheduler) { this.scheduler = scheduler; } public void restJob(long time) { TriggerKey triggerKey=new TriggerKey("testTrigger", Scheduler.DEFAULT_GROUP); SimpleTriggerImpl simpleTrigger=null; try { simpleTrigger= (SimpleTriggerImpl) scheduler.getTrigger(triggerKey); } catch (SchedulerException e) { e.printStackTrace(); } simpleTrigger.setRepeatInterval(time); try { scheduler.rescheduleJob(triggerKey,simpleTrigger); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
代码如上:在任务类中注入Scheduler,然后添加restJob方法。
配置文件如下:
使用方法:
@Autowired @Qualifier(value="testQuartz") private TestJob job; @RequestMapping("/job") public void job(Long time){ job.restJob(time); }