1.xml配置文件实现定时任务
第一步:配置环境:在只需要在pom文件中引入spring的依赖就可以
xml文件中需要引入context(自动扫描的)还有task的。
<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
https://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/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!--开启自动化扫描-->
<context:component-scan base-package="com.zks"/>
<task:scheduled-tasks>
<!-- 可以配置多个定时任务-->
<!-- 定时任务1
ref:指代任务类,TaskJob类 cron是一种特殊的表达式
-->
<task:scheduled ref="taskJob" method="job1" cron="0/2 * * * * ?"/>
<task:scheduled ref="taskJob2" method="job2" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
</beans>
第二步:准备javaBean对象
@Component
public class TaskJob {
private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void job1(){
System.out.println("任务1:"+simpleDateFormat.format(new Date()));
}
}
@Component
public class TaskJob2 {
private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void job2(){
System.out.println("任务2:"+simpleDateFormat.format(new Date()));
}
}
第三步测试
public class TaskTest {
public static void main(String[] args) {
//获取上下文环境
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
TaskJob taskJob= (TaskJob) applicationContext.getBean("taskJob");
TaskJob2 taskJob2= (TaskJob2) applicationContext.getBean("taskJob2");
taskJob.job1();
taskJob2.job2();
}
}
注解实现定时任务
第一步:pom配置文件也只需要引入spring的依赖就行
第二步:需要准备javaBean对象,需要在要进行定时任务的方法上面加上 @Scheduled(cron =“0/2 * * * * ?”)注解
@Component
public class TaskJob3 {
private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Scheduled(cron ="0/2 * * * * ?")
public void job3(){
System.out.println("任务3:"+simpleDateFormat.format(new Date()));
}
}
第三步:写xml文件,也需要引入context和task 的环境
<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
https://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/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!--开启自动化扫描-->
<context:component-scan base-package="com.zks"/>
<!-- 只有开启这个定时任务的驱动,才能用注解实现定时任务-->
<task:annotation-driven/>
</beans>
第四步:测试
public class TaskTest {
public static void main(String[] args) {
//获取上下文环境
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring3.xml");
TaskJob3 taskJob3= (TaskJob3) applicationContext.getBean("taskJob3");
taskJob3.job3();
}
}
cron表达式(可以用在线的cron编辑器)
1.秒(0–59)
2.分钟(0–59)
3.小时(0–23)
4.月份中的日期(1–31)
5.月份(1–12 或 JAN–DEC)
6.星期中的日期(1–7 或 SUN–SAT)
7.年份(1970–2099)
“ " —— 字符可以用于所有字段,在"分"字段中设为" ”,表示"每一分钟"的含义。
“?” —— 字符可以用在"日"和"周几"字段,它用来指定"不明确的值"。
这在你需要指定这两个字段中的某一个值而不是另外一个的时候会被用到。在后面的例子中可以看
到其含义。
“-” —— 字符被用来指定一个值的范。
比如在"小时"字段中设为"10-12",表示"10 点到 12 点"。
“,” —— 字符指定数个值。
比如在"周几"字段中设为"MON,WED,FRI",表示"the days Monday, Wednesday, and
Friday"。
“/” —— 字符用来指定一个值的的增加幅度。
比如在"秒"字段中设置为"0/15"表示"第 0, 15, 30,和 45 秒"。
而"5/15"则表示"第 5, 20, 35,和 50"。
在’/‘前加"*“字符相当于指定从 0 秒开始。每个字段都有一系列可以开始或结束的数值。
对于"秒"和"分"字段来说,其数值范围为 0 到 59。
对于"小时"字段来说其为 0 到 23,对于“日”字段来说为 0 到 31。
而对于"月"字段来说为 1 到 12。
“/“字段仅仅只是帮助你在允许的数值范围内从开始"第 n"的值。
“L” —— 字符可用在"日"和"周几"这两个字段。它是"last"的缩写,但是在这两个字段中有不同的含义。
“日"字段中的"L"表示"一个月中的最后一天”,对于一月就是 31 号,对于二月来说就是 28 号
(非闰年)。
“周几"字段中,它简单的表示"7” or “SAT”。
但是如果在"周几"字段中使用时跟在某个数字之后,它表示"该月最后一个星期×”。
比如"6L"表示"该月最后一个周五”。
当使用"L"选项时,指定确定的列表或者范围非常重要,否则你会被结果搞糊涂的。
“W” —— 可用于"日"字段。用来指定历给定日期最近的工作日(周一到周五)。
比如将"日"字段设为"15W”,意为: “离该月 15 号最近的工作日”。
因此如果 15 号为周六,触发器会在 14 号即周五调用。
如果 15 号为周日,触发器会在 16 号也就是周一触发。如果 15 号为周二,那么当天就会触
发。
如果"日"字段设为"1W",而一号是周六,会于下周一即当月的 3 号触发,它不会越过当月的值的范
围边界。
"W"字符只能用于"日"字段的值为单独的一天而不是一系列值的时候。
"L"和"W"可以组合用于“日”字段表示为’LW’,意为"该月最后一个工作日"。
“#” —— 字符可用于"周几"字段。该字符表示"该月第几个周×"。
比如"6#3"表示该月第三个周五( 6 表示周五,而"#3"该月第三个)。
再比如: “2#1” 表示该月第一个周一,而"4#5" 该月第五个周三。
注意如果你指定"#5"该月没有第五个"周×",该月是不会触发的。
“C” —— 字符可用于"日"和"周几"字段,它是"calendar"的缩写。
它表示为基于相关的日历所计算出的值(如果有)。如果没有关联的日历,那它等同于包含全部日
历。
“日"字段值为"5C”,表示"日历中的第一天或者 5 号以后"。
“周几"字段值为"1C”,则表示"日历中的第一天或者周日以后"。
对于"月份"字段和"周几"字段来说合法的字符都不是大小写敏感的。