一、spring的配置文件spring
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <!-- 注解类定时器须要配置--> <task:scheduler id="jobScheduler" pool-size="10"/> <task:scheduled-tasks scheduler="jobScheduler"> <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" /> <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/> </task:scheduled-tasks> <!-- 自动扫描的包名 --> <context:component-scan base-package="com.spring.task" /> </beans>
二、基于xml的定时器任务spring-mvc
package com.spring.task; /** * 基于xml的定时器 * @author hj */ public class MyTaskXml { public void show(){ System.out.println("XMl:is show run"); } public void print(){ System.out.println("XMl:print run"); } }
三、基于注解的定时器任务mvc
package com.spring.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * 基于注解的定时器 * @author hj */ @Component public class MyTaskAnnotation { /** * 定时计算。天天凌晨 01:00 执行一次 */ @Scheduled(cron = "0 0 1 * * *") public void show(){ System.out.println("Annotation:is show run"); } /** * 心跳更新。启动时执行一次,以后每隔2秒执行一次 */ @Scheduled(fixedRate = 1000*2) public void print(){ System.out.println("Annotation:print run"); } }
四、测试测试
package com.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml"); } }
说明:spa
1)Cron表达式的格式:秒 分 时 日 月 周 年(可选)code
字段名 容许的值 容许的特殊字符 component
秒 0-59 , - * / xml
分 0-59 , - * / io
小时 0-23 , - * / class
日 1-31 , - * ? / L W C
月 1-12 or JAN-DEC , - * /
周几 1-7 or SUN-SAT , - * ? / L C #
年 (可选字段) empty, 1970-2099 , - * /
“?”字符:表示不肯定的值
“,”字符:指定数个值
“-”字符:指定一个值的范围
“/”字符:指定一个值的增长幅度。n/m表示从n开始,每次增长m
“L”字符:用在日表示一个月中的最后一天,用在周表示该月最后一个星期X
“W”字符:指定离给定日期最近的工做日(周一到周五)
“#”字符:表示该月第几个周X。6#3表示该月第3个周五
2)Cron表达式范例:
每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
天天隔一小时 : [ 0 * */1 * * ?]
天天23点执行一次:0 0 23 * * ?
天天凌晨1点执行一次:0 0 1 * * ?
每个月1号凌晨1点执行一次:0 0 1 1 * ?
每个月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
天天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?