网站建设如何做,怎样在网站上做销售,台州市环保局网站开发区,廊坊seo排名优化网站1. 前言 上一篇我们学习了Quartz作为定时任务的框架的使用, 这一篇我们来学习Spring全家桶的SpringTask, 对于主张简单易用的Spring家族来说, SpringTask无疑也是一个轻量级的框架,他比Quartz更容易上手.
2. pom.xml依赖 dependencies dependency …1. 前言 上一篇我们学习了Quartz作为定时任务的框架的使用, 这一篇我们来学习Spring全家桶的SpringTask, 对于主张简单易用的Spring家族来说, SpringTask无疑也是一个轻量级的框架,他比Quartz更容易上手.
2. pom.xml依赖 dependencies dependency groupIdorg.springframework/groupId artifactIdspring-context/artifactId version4.3.12.RELEASE/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-context-support/artifactId version4.3.12.RELEASE/version /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version4.12/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-test/artifactId version4.3.12.RELEASE/version /dependency /dependencies 对的,一个spring-context就是他所需要的框架了, 也就是说, 如果你现在开发的项目是用的spring ,无需管依赖,直接看下一步就可以了.
2. 实现代码 spring 的老套路, 一般都可以用两种方式实现: 一是配置方式,二是注解方式
2.1 基于xml配置文件的方式 1 . 写一个运行任务的job类
package com.zgd.spring.task;
import java.util.Date;
/** * xml配置方式的定时任务 */ public class XmlTask { /** * 要执行的任务 */ public void task(){ System.out.println(定时任务执行中: new Date().toLocaleString()); } } 2 .在resource文件夹中, 创建spring 的配置文件: spring.xml , 这里特意要注意beans中的命名空间不能少
beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:taskhttp://www.springframework.org/schema/task xmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
bean idxmlTask classcom.zgd.spring.task.XmlTask/ task:scheduled-tasks !--每5秒钟运行一次-- task:scheduled refxmlTask methodtask cron0/5 * * * * ?/ /task:scheduled-tasks /beans 关于cron的语法: Cron表达式是一个字符串字符串以5或6个空格隔开分为6或7个域每一个域代表一个含义Cron有如下两种语法格式
1Seconds Minutes Hours DayofMonth Month DayofWeek Year 2Seconds Minutes Hours DayofMonth Month DayofWeek
每一个域可出现的字符如下 Seconds:可出现, - * /四个字符有效范围为0-59的整数 Minutes:可出现, - * /四个字符有效范围为0-59的整数 Hours:可出现, - * /四个字符有效范围为0-23的整数 DayofMonth:可出现, - * / ? L W C八个字符有效范围为1-31的整数 Month:可出现, - * /四个字符有效范围为1-12的整数或JAN-DEc DayofWeek:可出现, - * / ? L C #四个字符有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天2表示星期一 依次类推 Year:可出现, - * /四个字符有效范围为1970-2099年 每一个域都使用数字但还可以出现如下特殊字符它们的含义是 (1)*表示匹配该域的任意值假如在Minutes域使用*, 即表示每分钟都会触发事件。 (2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值但实际不会。因为DayofMonth和 DayofWeek会相互影响。例如想在每月的20日触发调度不管20日到底是星期几则只能使用如下写法 13 13 15 20 * ?, 其中最后一位只能用而不能使用*如果使用*表示不管星期几都会触发实际上并不是这样。 (3)-:表示范围例如在Minutes域使用5-20表示从5分到20分钟每分钟触发一次 (4)/表示起始时间开始触发然后每隔固定时间触发一次例如在Minutes域使用5/20,则意味着5分钟触发一次而2545等分别触发一次. (5),:表示列出枚举值值。例如在Minutes域使用5,20则意味着在5和20分每分钟触发一次。 (6)L:表示最后只能出现在DayofWeek和DayofMonth域如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。 (7)W: 表示有效工作日(周一到周五),只能出现在DayofMonth域系统将在离指定日期的最近的有效工作日触发事件。例如在 DayofMonth使用5W如果5日是星期六则将在最近的工作日星期五即4日触发。如果5日是星期天则在6日(周一)触发如果5日在星期一 到星期五中的一天则就在5日触发。另外一点W的最近寻找不会跨过月份 (8)LW:这两个字符可以连用表示在某个月最后一个工作日即最后一个星期五。 (9)#:用于确定每个月第几个星期几只能出现在DayofMonth域。例如在4#2表示某月的第二个星期三。 举例如下: !-- 每半分钟触发任务 -- task:scheduled refapp methodexecute1 cron30 * * * * ?/ !-- 每小时的10分30秒触发任务 -- task:scheduled refapp methodexecute2 cron30 10 * * * ?/ !-- 每天1点10分30秒触发任务 -- task:scheduled refapp methodexecute3 cron30 10 1 * * ?/ !-- 每月20号的1点10分30秒触发任务 -- task:scheduled refapp methodexecute4 cron30 10 1 20 * ?/ !-- 每年10月20号的1点10分30秒触发任务 -- task:scheduled refapp methodexecute5 cron30 10 1 20 10 ?/ !-- 每15秒、30秒、45秒时触发任务 -- task:scheduled refapp methodexecute6 cron15,30,45 * * * * ?/ !-- 15秒到45秒每隔1秒触发任务 -- task:scheduled refapp methodexecute7 cron15-45 * * * * ?/ !-- 每分钟的每15秒时任务任务每隔5秒触发一次 -- task:scheduled refapp methodexecute8 cron15/5 * * * * ?/ !-- 每分钟的15到30秒之间开始触发每隔5秒触发一次 -- task:scheduled refapp methodexecute9 cron15-30/5 * * * * ?/ !-- 每小时的0分0秒开始触发每隔3分钟触发一次 -- task:scheduled refapp methodexecute10 cron0 0/3 * * * ?/ !-- 星期一到星期五的10点15分0秒触发任务 -- task:scheduled refapp methodexecute11 cron0 15 10 ? * MON-FRI/ 然后我们需要一个测试类来测试:
package com.zgd.spring; 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:spring.xml) public class App{ Test public void start(){ System.out.println(启动spring); while (true){ //使用死循环确保程序持续的运行 } }
} 运行结果: 2.2 基于Scheduled注解的方式 这个注解可以传五个参数:
cron指定cron表达式 zone:官方文档解释A time zone for which the cron expression will be resolved。指定cron表达式运行的时区 fixedDelay官方文档解释An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔单位是毫秒。 fixedRate官方文档解释An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔单位是毫秒。 initialDelay:官方文档解释:Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.任务第一次被调用前的延时单位毫秒 再写一个任务类: package com.zgd.spring.task;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
import java.util.Date;
/** * 注解执行的任务 */ Component public class AnnoTask { /** * 使用注解,5秒执行一次 */ Scheduled(cron 0/5 * * * * ?) public void task(){ System.out.println(注解的方式的任务执行了 new Date().toLocaleString()); }
} 2 . 在spring配置文件头中添加命名空间及描述,并开启定时任务注解驱动
beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:taskhttp://www.springframework.org/schema/task xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd bean idxmlTask classcom.zgd.spring.task.XmlTask/ !-- task:scheduled-tasks lt;!ndash;每5秒钟运行一次ndash;gt; task:scheduled refxmlTask methodtask cron0/5 * * * * ?/ /task:scheduled-tasks-- task:annotation-driven / context:component-scan base-packagecom.zgd.spring.task/
/beans 这里我试了一下,怎么都无法执行定时任务, 后面仔细检查,发现是没有加上注解的自动扫描, 平时开发肯定都会加上这个,但是由于是测试demo所以把这个忘记了. context:component-scan base-packagecom.zgd.spring.task/
运行我们的测试类, 结果如下
--------------------- 作者zzzgd_666 来源CSDN 原文https://blog.csdn.net/zzzgd_666/article/details/80723654 版权声明本文为博主原创文章转载请附上博文链接