目录
pom.xml中引入依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
定时任务配置文件
使用xml的方式创建定时器。
(1)spring-mvc.xml配置文件(src/main/resources目录下)
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<import resource="spring-quartz.xml"/>
</beans>
(2)spring-quartz.xml配置文件(src/main/resources目录下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 定义job对象 -->
<bean id="myjob" class="com.example.demo.quartz.TaskTest"></bean>
<!-- 注册jobDetail,通过反射调用自定义任务对象 -->
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 目标对象 -->
<property name="targetObject">
<ref bean="myjob"/>
</property>
<property name="targetMethod">
<value>sendMessage</value>
</property>
</bean>
<!-- CronTriggerFactoryBean:触发器 触发什么任务 什么时间触发 -->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 配置触发哪个任务 -->
<property name="jobDetail">
<ref bean="myJobDetail"/>
</property>
<!-- 什么时间触发任务 cronExpression:cron表达式 每隔2分钟执行一次任务 -->
<property name="cronExpression">
<value>0 0/2 * * * ?</value>
</property>
</bean>
<!-- SchedulerFactoryBean:配置计划调度器容器 -->
<bean lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
</beans>
(3)定时器类
package com.example.demo.quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TaskTest {
public void sendMessage(){
System.out.println("方法执行了:"+new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()));
}
}
(4)测试代码效果
定时任务注解方式创建
使用注解的方式创建定时任务。
(1)注解测试代码
package com.example.demo.quartz;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Configurable
@EnableScheduling
public class TaskOfComponentTest {
@Scheduled(cron = "*/5 * * * * *")
public void sendMessage(){
System.out.println("每隔5秒执行一次");
}
}
(2)测试代码效果