SSM项目整合@Scheduled定时任务
在配置文件springmvc.xml加入task的命名空间:
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
启用注解驱动的定时任务:
<task:annotation-driven scheduler="myTask"></task:annotation-driven>
配置定时任务的线程池(推荐):
<task:scheduler id="myTask" pool-size="5"/>
创建定时任务类(Task.java):
package com.kd.nm.controller;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Task {
// 每5秒执行一次(秒、分、时、日、月、星期)
@Scheduled(cron = "0/5 * * * * ? ")
public void method() {
System.out.println("定时任务开始了:" + new Date().toGMTString());
}
}
开始测试:
总结
每天一个提升小技巧!!!