在applicationContext.xml中添加:html
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-driven executor="myExecutor" scheduler="myScheduler" /> <task:executor id="myExecutor" pool-size="5" /> <task:scheduler id="myScheduler" pool-size="10" />
java代碼:java
@Component public class CleanExpireTokenTask { private Logger logger = LoggerFactory.getLogger(LogTag.BUSINESS); @Scheduled(cron = "0 * * * * ?") public void startUpdateSaleThread(){ try{ System.out.println("check token expire"); }catch(Exception e){ logger.error("Make salesReport faild",e); } } }
注意:spring
實現類上要加註解@Component安全
定時器的任務方法不能有返回值app