SSM項目中配置定時任務

首先,springMVC.xml文件中添加

       xmlns:task="http://www.springframework.org/schema/task"

       xsi:加入http://www.springframework.org/schema/task
                   http://www.springframework.org/schema/task/spring-task-3.2.xsd

  

<!-- 配置定時器掃描器-->
<context:component-scan base-package="com.example.timer" />
<!-- 定時任務配置 scheduler 方式 註解 -->
<task:executor id="executor" pool-size="5"/>
<task:scheduler id="scheduler" pool-size="10"/>
<task:annotation-driven executor="executor" scheduler="scheduler"/>

 

     在com.example.timer中創建文件TimerTask.java

package com.example.timer;

import java.io.File;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定時器
 * 
 * @author limstorm
 * @create 2019-05-04 07:55:50
 */
@Component
public class TimerTask {
//	CRON表達式 含義 
//	“0 0 12 * * ?” 每天中午十二點觸發 
//	“0 15 10 ? * *” 每天早上10:15觸發 
//	“0 15 10 * * ?” 每天早上10:15觸發 
//	“0 15 10 * * ? *” 每天早上10:15觸發 
//	“0 15 10 * * ? 2005” 2005年的每天早上10:15觸發 
//	“0 * 14 * * ?” 每天從下午2點開始到2點59分每分鐘一次觸發 
//	“0 0/5 14 * * ?” 每天從下午2點開始到2:55分結束每5分鐘一次觸發 
//	“0 0/5 14,18 * * ?” 每天的下午2點至2:55和6點至6點55分兩個時間段內每5分鐘一次觸發 
//	“0 0-5 14 * * ?” 每天14:00至14:05每分鐘一次觸發 
//	“0 10,44 14 ? 3 WED” 三月的每週三的14:10和14:44觸發 
//	“0 15 10 ? * MON-FRI” 每個週一、週二、週三、週四、週五的10:15觸發

	@Scheduled(cron = "0/600 * * * * ?")//每隔600秒隔行一次 
	public void clearCaptcha()
	{
		System.out.println("開始執行定時任務");
		File file = new File("F:/Tomcat9/apache-tomcat-9.0.2/webapps/rainstorm/captcha/");
		if (file.isDirectory()) {
            String[] children = file.list();
            for (int i=0; i<children.length; i++) {
            	File childFile = new File("F:/Tomcat9/apache-tomcat-9.0.2/webapps/rainstorm/captcha/"+children[i]);
                childFile.delete();
            }
        }
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章