1.timer方式
public void timer() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 17); // 控制时
calendar.set(Calendar.MINUTE, 30);
// 控制分
calendar.set(Calendar.SECOND, 0);
// 控制秒
final Date time = calendar.getTime();
// 得出执行任务的时间
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------"+time);
}
}, time, 12);// 这里设定将延时每天固定执行
}
初始化(启动tomcat时任务调度)
public void init(FilterConfig fc) throws ServletException {
timer();
}
配置xml
<filter>
<filter-name>startFilter</filter-name>
<filter-class>com.fh.filter.startFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>startFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.task方式
添加
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
<!-- 任务调度 -->
<task:scheduled-tasks>
<task:scheduled ref="tradeService" method="insertOrUpdateFundList" cron="0 0 0/1 * * ?"/> (0点开始,每一个小时执行一次)
<task:scheduled ref="tradeService" method="uploadCustImageList" cron="0 0 3 * * ?"/> (3点开始,每天一次)
</task:scheduled-tasks>