Spring配置文件
xmlns加入:
xmlns:task="http://www.springframework.org/schema/task"\
xsi:schemaLocation中加入:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
<task:scheduler id="myScheduler" pool-size="10"/>
<task:annotation-driven scheduler="myScheduler" mode="proxy" />
如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- spring的Scheduler 定时器 需要启动项目后才能运行-->
<task:scheduler id="myScheduler" pool-size="10"/>
<task:annotation-driven scheduler="myScheduler" mode="proxy" />
</beans>
@Component
public class ScheduleTask {
@Scheduled(cron="0 32 15 * * ?") //每天的15点32执行
public void testTask() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(" ************************************************ ");
System.out.println(" 定时任务开始 "+sdf.format(new Date()));
System.out.println(" ************************************************ ");
for(int i = 0; i <= 5; i++) {
System.out.println(i);
}
System.out.println(" ************************************************ ");
System.out.println(" 定时任务结束 "+sdf.format(new Date()));
System.out.println(" ************************************************ ");
}
}
cron表达式
- 第一位,表示秒,取值0-59
- 第二位,表示分,取值0-59
- 第三位,表示时,取值0-23
- 第四位,表示天,取值1-31
- 第五位,表示月,取值1-12
- 第六位,表示星期,取值1-7,星期一,二,···日,1表示星期天,2表示星期一···
- 第七位,表示年,可以留空,取值为1970-2099
cron中还有一些特殊的符号,其含意如下:
- (*)星号:可以理解为每的意思,每秒,每分,每时,每日,每月····
- (?)问号:只能出现在日期和星期这两个位置
- (-)减号:表达一个范围,比如在小时字段中使用,“10-12”,则表示从10点12点,为10点,11点,12点。
- (,)逗号:表达一个列表值,如在星期字段中使用1,2,4,星期日,星期一,星期三
- (/)斜杠:如x/y则x表示开始时间,y表示步长,比如在第一位(秒)0/15就是从0秒开始每15秒,最后就是0,15,30,45。另外*/y,等同于0/y
下面列举几个例子供大家来验证:
0 0 3 * * ? 每天3点执行
0 5 3 * * ? 每天3点5分执行
0 5 3 ? * * 每天3点5分执行,与上面作用相同
0 5/10 3 * * ? 每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行
0 10 3 ? * 1 每周星期天,3点10分 执行,注:1表示星期天
0 10 3 ? * 1#3 每个月的第三个星期,星期天 执行,#号只能出现在星期的位置
15-30/5 * * * * ? 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次
0 0/3 * * * ? 每小时的第0分0秒开始,每三分钟触发一次
0 15 10 ? * MON-FRI 星期一到星期五的10点15分0秒触发任务
0 15 10 L * ? 每个月最后一天的10点15分0秒触发任务
0 15 10 LW * ? 每个月最后一个工作日的10点15分0秒触发任务
0 15 10 ? * 5L 每个月最后一个星期四的10点15分0秒触发任务
0 15 10 ? * 5#3 每个月第三周的星期四的10点15分0秒触发任务