定时任务解决方案2,Spring提供的ThreadPoolTaskScheduler

 主要是使用org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler

它是由spring提供的定时任务类 ,

特点是可以使用cron表达式,

当然,结合ioc和annotation,还是非常好用的.

 

 

 package cn.test.schedule.spring; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class SpringThreadPoolTaskSchedulerTester implements Runnable{ private ThreadPoolTaskScheduler tpts=null; private String cronExpression="1 * * ? * *"; @PostConstruct private void start() { tpts=new ThreadPoolTaskScheduler(); //必须初始化才能用 tpts.initialize(); CronTrigger ct=new CronTrigger(cronExpression); tpts.schedule(this,ct); } @PreDestroy private void stop() { tpts.shutdown(); } @Override public void run() { System.out.println("yes , a task running based on cron expression!@"+new java.util.Date()); } }

你可能感兴趣的:(Schedule)