小记:之前用Tomcat集成Quartz还用最原始的实现,需要在配置文件中配置好Quartz的相关配置以及各个Job的JobDetail和Trigger,然后定义一个Quartz的Servlet让其可以随Tomcat启动来启动调度。这里不做详述,本篇主要记录Spring整合Quartz框架的2种方式:基于配置/基于注解。
本篇主要关注Quartz的相关配置,Spring的相关配置不做赘述。
基于配置的方式
- 编写定时任务的job类
package me.lishuo.job;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* Created by lis on 16/7/6.
*/
@Component
public class TestQuartzJob {
public void run() {
System.out.println("Hello quartz! now ->" + new Date());
}
}
2.配置spring-quartz.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- quartz-2.x的配置 -->
<!-- 定时任务的bean -->
<bean id="testQuartzJob" class="me.lishuo.job.TestQuartzJob" />
<!-- 1) 调度的配置&job的配置 -->
<bean id="testQuartzJob_ct" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail"><ref bean="testQuartzJob_jd" /></property>
<property name="cronExpression" value="*/5 * * * * ?" > </property>
</bean>
<bean id="testQuartzJob_jd"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"> <ref bean="testQuartzJob" /> </property>
<property name="targetMethod" value="run"> </property>
</bean>
<!-- 3) 开启定时任务-->
<bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testQuartzJob_ct"/>
</list>
</property>
</bean>
基于配置的方式,需要在配置文件中详细配置任务类bean、CronTigger以及JobDeail。并且要在JobDeail指定定时任务的targetMethod方法名,即定时任务运行的方法。
基于注解的方式
- 编写定时任务的job类
package me.lishuo.job;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* Created by lis on 16/7/6.
*/
@Component
public class TestQuartzJob {
@Scheduled(cron = "*/1 * * * * ?")
public void run() {
System.out.println("Hello quartz! now ->" + new Date());
}
}
- 注解方式的配置spring-quartz.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task"
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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- task任务扫描注解 -->
<task:annotation-driven/>
<!-- 扫描位置 -->
<context:component-scan base-package="me.lishuo.job"/>
</beans>
如果定时任务有很多的话,采用注解方式就带来便捷了,还需要在定时任务运行的方法中加上@Scheduled注解并指定cron等配置即可。
RUN
配置好之后,可以启动web项目,就可以查看运行结果了,我的测试定时任务1s执行一次,如下:
Tips
以上,定时任务用Quartz实现的话,比较灵活。但如果你的定时任务不复杂的话,也可以使用Linux cronJob,更加简单方便。