Spring + Quartz定時任務(wù)
第一種方法, 執(zhí)行任務(wù)的類繼承QuartzJobBean,Spring4.1.0版本后需要quartz2.X版本量蕊, Spring4.1.0之前需要quartz1.X版本骑歹。Spring4.1.0版本后+quartz2.X舉例:
- 執(zhí)行任務(wù)的類:
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MySchedule extends QuartzJobBean {
private int timeout;
private static int INDEX = 0;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "第" + (++INDEX) + "次執(zhí)行MySchedule的定時任務(wù)");
}
public int getTimeout() {return timeout;}
public void setTimeout(int timeout) {this.timeout = timeout;}
}
- applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置JobDetail -->
<bean name="myScheduleJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.quartz.myschedule.MySchedule" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="5" />
</map>
</property>
</bean>
<!-- 配置觸發(fā)器 -->
<!-- SimpleTriggerFactoryBean历葛,每隔多長時間執(zhí)行一次 -->
<bean id="myScheduleJobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="myScheduleJobDetail" />
<!-- 延遲觸發(fā)時間价匠,延遲5秒進行觸發(fā) -->
<property name="startDelay" value="5000" />
<!-- 重復(fù)觸發(fā)的時間間隔当纱,5秒 -->
<property name="repeatInterval" value="5000" />
</bean>
<!-- 配置ScheduleFactoryBean -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myScheduleJobTrigger" />
</list>
</property>
</bean>
</beans>
注意1:配置觸發(fā)器的時候,有兩種方式霞怀。一種是每隔多長時間執(zhí)行一次(SimpleTriggerFactoryBean)惫东。一種是指定時間執(zhí)行(CronTriggerFactoryBean)莉给。當然毙石,第二種也可以通過配置表達式實現(xiàn)每隔多長時間執(zhí)行一次廉沮。如果需要第二種, 只需將上面applicationContext.xml中配置觸發(fā)器的代碼使用以下代碼替換即可
<bean id="myScheduleJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myScheduleJobDetail" />
<!-- 每10秒運行一次 -->
<property name="cronExpression" value="0/10 * * * * ?" />
</bean>
注意2:如果是低版本的spring和quartz徐矩, 將JobDetailFactoryBean改為JobDetailBean滞时。
3.web.xml中配置spring的監(jiān)聽
<!-- 配置Spring核心監(jiān)聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
- 啟動tomcat, 執(zhí)行定時任務(wù)滤灯。
- 可能出現(xiàn)錯誤:
如果使用高版本spring和低版本quartz:報錯坪稽。原因是 JobDetailFactoryBean的創(chuàng)建需要JobDetailImpl, 而低版本quartz1.X包下沒有該實現(xiàn)類鳞骤。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exampleJobDetail' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/quartz/impl/JobDetailImpl
如果使用低版本的spring和高版本的quartz:報錯窒百。原因是需要創(chuàng)建的JobDetailBean繼承了JobDetail, 而JobDetail是一個接口豫尽。
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.JobDetailBean] for bean with name 'exampleJobDetail' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.JobDetailBean has interface org.quartz.JobDetail as super class
第二種方法篙梢,執(zhí)行任務(wù)的類不用繼承其他超級類。這種方式可以實現(xiàn)低版本的spring(4.1.0)以前版本和高版本quartz(2.X)的結(jié)合使用美旧。 如果公司使用spring4.1.0以前版本渤滞,又不想使用quartz1.X版本, 可以通過這種方式榴嗅。但是使用較高版本spring(4.1.0以后)和低版本quartz結(jié)合妄呕, 報錯。
- 執(zhí)行任務(wù)的類
import java.text.SimpleDateFormat;
import java.util.Date;
public class MySchedule {
private static int INDEX = 0;
protected void myScheduleTask() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "第" + (++INDEX) + "次執(zhí)行MySchedule的定時任務(wù)");
}
}
- applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mySchedule" class="com.quartz.myschedule.MySchedule"></bean>
<!-- JobDetail任務(wù)調(diào)度 -->
<bean id="myScheduleDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任務(wù)類 -->
<property name="targetObject" ref="mySchedule" />
<!-- 指定任務(wù)執(zhí)行的方法 -->
<property name="targetMethod" value="myScheduleTask" />
</bean>
<!-- 觸發(fā)器 -->
<bean id="myScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myScheduleDetail" />
<!-- 每10秒運行一次 -->
<property name="cronExpression" value="0/3 * * * * ?" />
</bean>
<!-- schedule工廠 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myScheduleTrigger" />
</list>
</property>
</bean>
</beans>
- web.xml
<!-- 配置Spring核心監(jiān)聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
- 啟動tomcat嗽测, 執(zhí)行定時任務(wù)
Spring的定時任務(wù)
通過xml配置
- 執(zhí)行任務(wù)的類
import java.text.SimpleDateFormat;
import java.util.Date;
public class MySchedule {
private static int INDEX = 0;
public void myScheduleTask() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "第" + (++INDEX) + "次執(zhí)行MySchedule的定時任務(wù)");
}
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.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/tx http://www.springframework.org/schema/tx/spring-tx-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id="mySchedule" class="com.spring.task.MySchedule"></bean>
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="mySchedule" method="myScheduleTask" cron="0/1 * * * * ?" />
</task:scheduled-tasks>
<task:scheduler id="scheduler" pool-size="10"/>
</beans>
- 啟動tomcat
第二種方式绪励,通過注解
- 執(zhí)行任務(wù)的類
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MySchedule {
private static int INDEX = 0;
@Scheduled(cron="0/1 * * * * ?")
public void myScheduleTask() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "第" + (++INDEX) + "次執(zhí)行MySchedule的定時任務(wù)");
}
}
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.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/tx http://www.springframework.org/schema/tx/spring-tx-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:component-scan base-package="com.spring.task"/>
<task:annotation-driven scheduler="scheduler" mode="proxy"/>
<task:scheduler id="scheduler" pool-size="10"/>
</beans>
- 啟動tomcat測試。
jar包及詳細源碼:https://github.com/xueshimeng/schedule