1、Quartz 定时任务功能强大,应用场景也十分广泛。直接上代码
1.0、定义一个要定时执行的方法
package com.fbank.demo.web; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * Quartz定时任务执行的类:被spring管理,注入其他spring对象 */ @Component public class QuartzTest { @Autowired private IRedisService redisService; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定时执行方法 public void execute(){ System.out.println("定时任务执行了:"+sdf.format(new Date())); } }
1.1、maven添加依赖:(spring-context-support-4.3.14.RELEASE.jar)
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.14.RELEASE</version> </dependency>
1.2、在spring 能加载到的地方引入配置文件
1.3、applicationContext-quartz.xml 在 resources 目录下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd" > <!--定时执行类 --> <bean id="quartzTest" class="com.fbank.demo.web.QuartzTest" /> <bean name="quartzTestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzTest" /><!--目标对象--> <property name="targetMethod"> <value>execute</value><!--目标方法--> </property> <property name="concurrent"><!--配置为false不允许任务并发执行--> <value>false</value> </property> </bean> <!--配置定时器--> <bean id="quartzTestTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 触发bean配置 --> <property name="jobDetail" ref="quartzTestJob"/> <!-- 触发时间配置 --> <property name="cronExpression"> <!--cron表达式:每隔五秒钟触发,也可从配置读取--> <value>0/5 * * * * ?</value> <!--<value>${quartzTestTrigger}</value>--> </property> </bean> <!-- 调度任务触发器,启动定时任务--> <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy"> <property name="triggers"> <list> <ref bean="quartzTestTrigger"/> </list> </property> </bean> </beans>
2、项目启动后,被spring 管理的 Quartz 就开始执行了