在Linux系统中有cron定时任务,在spring中也对cron作了实现,它们基本都是相同的,能够把spring的cron实现看作是Linux的cron实现的一个子集。这里主要介绍cron的经常使用的表达式的写法和容易出错的地方,也会给出具体的例子。cron的表达式中主要有6个字段分别表示:秒 分 时 日 月 星期,在Linux中还支持第7个字段表示年份。在cron中还有2个重要的元字符* 和 -,其中*表示匹配任意,在那个位置上就匹配任意位置的时间,例如在分上是*就表示0-59都匹配。-表示区间。例如:JAN-DEC就表示1月至12月。java
##实例讲解 一、 匹配每一天12点怎么办?spring
秒 分 时 日 月 星期测试
0 0 12 * * ?spa
在这里咱们看到一个问号(?),问号是只能是日 和星期使用,代替*号使用,为何有星号了还要弄一个问号呢?你想当咱们一个时间的日期肯定了,那么星期天然也就肯定了,因此不存在匹配任意星期了。同理通常年月星期肯定了日期基本也就肯定了,也就不存在任意的问题了。code
二、每5分钟执行1次component
秒 分 时 日 月 星期xml
1 0/5 * * * ?it
咱们看到有一个/符号,不少资料把它解释为每,好比上面的0/5就表示从0开始,每5分钟执行一次,不过只是不够准确的,不信你能够是一下* 0/5 * * * ?表达式看是否是每5分钟执行一次。其实更加准确的表示是从0开始,分钟的数字mod5==0的时候执行(匹配)。* 0/5 * * * ?因此这个表达式是在分钟数为0,5,15,20,25,30,35,40,45,50,55这些分钟数的这1分钟内会一直执行。(sping的cron中是这样,Linux的没有测)io
三、 每20分钟执行一次ast
秒 分 时 日 月 星期
1 0,20,40 * * * ?
这里主要是介绍一下分号(,),表示多个匹配
四、每周一到周五8点
秒 分 时 日 月 星期
"0 0 8 * * MON-FRI
符号-表示区间。
五、其它 固然还有一下其它的,例如# L,在spring的cron中是不支持的,能够看一下下面注释的代码了解一下意思就能够了。实在想用也能够经过其它表达式实现。
##Spring cron ###任务代码
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component("taskJob") public class AnnotationTaskJob { @Scheduled(cron = "1 0/5 * * * ?") public void everyFiveMin() { System.out.println("每5分钟执行一次任务"); } @Scheduled(cron = "1 0,20,40 * * * ?") public void everyTwentyMin() { System.out.println("每20分钟执行一次任务"); } @Scheduled(cron = "0 * * * * ?") public void everyMin() { System.out.println("每一分钟执行一次任务"); } @Scheduled(cron = "0 0 * * * ?") public void everyHour() { System.out.println("每一小时执行一次任务"); } @Scheduled(cron = "0 0 0 * * ?") public void everyDay() { System.out.println("每一天0点执行一次任务"); } @Scheduled(cron = "0 0 0 1 * ?") public void everyMonth() { System.out.println("每个月1号0点执行一次任务"); } /** * 5月第二个星期天 */ // @Scheduled(cron = "* * * * 5 1#2") // @Scheduled(cron = "* * * * 5 SUN#2") // public void sendEmailWhenMotherDay() // { // System.out.println("母亲节了发个邮件提醒一下本身"); // } /** * 6 月第3个星期天 */ // @Scheduled(cron = "* * * * 6 SUN#3") // public void sendEmailWhenFaherDay() // { // System.out.println("父亲节了发个邮件提醒一下本身"); // } // @Scheduled(cron = "0 0 8 L * ?") // public void theMonthLast() { // System.out.println("每个月最后一天发邮件提醒一下本身总结"); // } @Scheduled(cron = "0 0 8 * * MON-FRI") public void workDay() { System.out.println("周一到周五8点弄个定时开机"); } // @Scheduled(cron = "0 0 0 1 10 ? 2049") // public void china() { // System.out.println("早点起来为祖国庆生!"); // } }
###测试代码
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:conf/spring-task-annotation.xml" }) public class TaskJobAnnotationTest { @Test public void testTaskJob() { Scanner scanner = new Scanner(System.in); scanner.nextLine(); scanner.close(); // System.in.read(); } }
###配置文件
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <context:annotation-config /> <context:component-scan base-package="cn.freemethod.task" /> <task:executor id="executor" pool-size="2" /> <task:scheduler id="scheduler" pool-size="5" /> <task:annotation-driven executor="executor" scheduler="scheduler" /> </beans>