使用spring @scheduled注解能够方便的设定定时任务,可是对于定时参数须要变化的状况就会很不方便,若是要实现更改定时参数,就要中止服务,更改参数,从新部署。
对于这种需求, 能够利用TaskScheduler借口来实现,实现方法有两种html
范例代码以下java
package schedule; import java.util.Date; public class Say implements Runnable { @Override public void run(){ System.out.println("" + new Date() + " hello"); } }
package schedule; import java.util.Date; import java.util.concurrent.ScheduledFuture; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @SpringBootApplication public class ScheduleApplication { @Autowired private ThreadPoolTaskScheduler threadPoolTaskScheduler; @Bean public ThreadPoolTaskScheduler threadPoolTaskScheduler(){ return new ThreadPoolTaskScheduler(); } private ScheduledFuture<?> future; @RequestMapping("/") @ResponseBody public String home(){ return "home"; } ///方法一 @RequestMapping("/startCron") @ResponseBody public String startCron(){ System.out.println("x0"); //threadPoolTaskScheduler.shutdown(); future = threadPoolTaskScheduler.schedule(new Say(), new CronTrigger("*/5 * * * * *")); System.out.println("x1"); return "x"; } @RequestMapping("/stopCron") @ResponseBody public String stopCron(){ System.out.println("stop >>>>>"); if(future != null) { future.cancel(true); } //future = threadPoolTaskScheduler.schedule(new Say(), new CronTrigger("*/5 * * * * *")); System.out.println("stop <<<<<"); return "stop cron"; } @RequestMapping("/startCron10") @ResponseBody public String startCron10(){ System.out.println("x100"); future = threadPoolTaskScheduler.schedule(new Say10(), new CronTrigger("*/12 * * * * *")); System.out.println("x101"); return "x10"; } ///方法二 private String cronStr = "*/5 * * * * *"; @RequestMapping("/startCron1") @ResponseBody public String startCron1(){ System.out.println("startCron1 >>>>"); threadPoolTaskScheduler.schedule(new Say(), new Trigger(){ @Override public Date nextExecutionTime(TriggerContext triggerContext){ return new CronTrigger(cronStr).nextExecutionTime(triggerContext); } }); System.out.println("startCron1 <<<<"); return "*****"; } @PostConstruct public void start(){ startCron1(); } @RequestMapping("/changeCronStr") @ResponseBody public String changeCronStr(){ cronStr = "*/12 * * * * *"; System.out.println("change " + cronStr); return cronStr; } @RequestMapping("/changeCronStr5") @ResponseBody public String changeCronStr5(){ cronStr = "*/5 * * * * *"; System.out.println("change " + cronStr); return cronStr; } public static void main(String[] args) { SpringApplication.run(ScheduleApplication.class, args); } }
说明 threadPoolTaskScheduler.shutdown();会抛出异常
@PostConstruct 在依赖注入完成后,进行调用
使用web
{
sartcron();
}
会产生空指针异常,觉得依赖注入为完成.
@PostConstruct还能够使用BeanPostProcessor的功能来完成.
或使用spring
public class StartupListener implements ApplicationListener<ContextRefreshedEvent>
监听事件.segmentfault
参考
Spring/SpringMVC在启动完成后执行方法
http://blog.csdn.net/renyisheng/article/details/50803875
Spring/SpringMVC在启动完成后执行方法
http://www.cnblogs.com/itjcw/p/5977911.html
Spring @Scheduled定时任务动态修改cron参数
http://blog.csdn.net/xht555/article/details/53121962
Java 定时任务系列(2)-Spring 定时任务的几种实现
http://www.javashuo.com/article/p-kbtmiujb-bo.html
Java timer task schedule
http://stackoverflow.com/questions/4044729/java-timer-task-schedule
[Spring笔记]支持注解的Spring调度器
app
www点
w
2
b
c
点com/article/169011ide