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();
            }
        }
	}
}
发布了21 篇原创文章 · 获赞 2 · 访问量 4804

猜你喜欢

转载自blog.csdn.net/qq_21479345/article/details/89817720