参考博客:https://blog.csdn.net/fhjdzkp/article/details/78854076在ssm中测试定时任务
1.在resources目录下添加:spring-task.xml:
<?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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!--配置包扫描-->
<context:component-scan base-package="test" />
<!-- 定时任务配置 scheduler 方式 注解 -->
<task:executor id="executor" pool-size="5"/>
<task:scheduler id="scheduler" pool-size="10"/>
<task:annotation-driven executor="executor" scheduler="scheduler"/>
</beans>
2.在web.xml中引入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-mybatis.xml
classpath:spring-task.xml
</param-value>
</context-param>
初始化spring-task.xml配置文件。
3.创建test类:
package test;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TimerTask {
@Scheduled(cron = "0/60 * * * * ?")
public void timeTest() {
System.out.println("定时任务开始执行");
}
}
通过注解方式引入cron表达式:
"0 0 08 * * ?" 每天上午8点触发"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 Cron表达式包括7个字段:秒0-59,分0-59,小时0-23,日期1-31,月1-12(JAN-DEC),周1-7(SUN-SAT),年(可选字段)留空或者1970-2099
斜线(/)字符表示增量值。例如,在秒字段中"5/15"代表从第5秒开始,每15秒一次。
问号(?)字符和字母L字符只有在月内日期和周内日期字段中可用。问号表示这个字段不包含具体值。Spring集成Quartz后,它自己加的一个约束,那就是:日期(1-31)和星期(SUN-SAT)两者,必须有一个是问号(?),系统在启动的时候,Spring会检查表达式,如果不符合它的规则,就会抛异常。所以在使用的时候这个地方一定要注意,而这个在Linux上执行Cron是没有这个限制的。
字母L字符是last的缩写。放在月内日期字段中,表示安排在当月最后一天执行。在周内日期字段中,如果"L"单独存在,就等于"7",否则代表当月内周内日期的最后一个实例。所以"0L"表示安排在当月的最后一个星期日执行。
星号(*)字符是通配字符,表示该字段可以接受任何可能的值、表达式例子。
二,使用quartz实现定时任务(调度器 任务 触发器)
在spring applicationContext.xml中配置:
<!-- 配置具体执行定时任务业务的类-->
<bean id="quartzJob" class="com.rd.quartz.QuartzJob">
<property name="incomeService">
<ref bean="incomeService" />
</property>
</bean>
<!-- 配置执行的方法以及执行时间cron-->
<bean id="findSuspension" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="quartzJob" />
</property>
<property name="targetMethod">
<value>findSuspension</value>
</property>
</bean>
<bean id="findSuspensionTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="findSuspension" />
</property>
<property name="cronExpression">
<value>0 23 16 * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="findSuspensionTime"/>
</list>
</property>
</bean>
之后在Quartz类中写执行定时任务的相关业务。
spring中配置四部分:
1,配置定时任务调度器连接具体执行定时任务的类
2,配置定时任务的执行方法,ref指向调度器id
3,通过cron表达式配置方法所执行的时间,指向执行方法id
4,配置定时任务的触发器,指向执行时间id