Spring + Quartz定时任务
第一种方法, 执行任务的类继承QuartzJobBean,Spring4.1.0版本后需要quartz2.X版本, Spring4.1.0之前需要quartz1.X版本。Spring4.1.0版本后+quartz2.X举例:
- 执行任务的类:
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) + "次执行MySchedule的定时任务");
}
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>
<!-- 配置触发器 -->
<!-- SimpleTriggerFactoryBean,每隔多长时间执行一次 -->
<bean id="myScheduleJobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="myScheduleJobDetail" />
<!-- 延迟触发时间,延迟5秒进行触发 -->
<property name="startDelay" value="5000" />
<!-- 重复触发的时间间隔,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:配置触发器的时候,有两种方式。一种是每隔多长时间执行一次(SimpleTriggerFactoryBean)。一种是指定时间执行(CronTriggerFactoryBean)。当然,第二种也可以通过配置表达式实现每隔多长时间执行一次。如果需要第二种, 只需将上面applicationContext.xml中配置触发器的代码使用以下代码替换即可
<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的监听
<!-- 配置Spring核心监听器 -->
<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, 执行定时任务。
- 可能出现错误:
如果使用高版本spring和低版本quartz:报错。原因是 JobDetailFactoryBean的创建需要JobDetailImpl, 而低版本quartz1.X包下没有该实现类。
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:报错。原因是需要创建的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
第二种方法,执行任务的类不用继承其他超级类。这种方式可以实现低版本的spring(4.1.0)以前版本和高版本quartz(2.X)的结合使用。 如果公司使用spring4.1.0以前版本,又不想使用quartz1.X版本, 可以通过这种方式。但是使用较高版本spring(4.1.0以后)和低版本quartz结合, 报错。
- 执行任务的类
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) + "次执行MySchedule的定时任务");
}
}
- 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任务调度 -->
<bean id="myScheduleDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任务类 -->
<property name="targetObject" ref="mySchedule" />
<!-- 指定任务执行的方法 -->
<property name="targetMethod" value="myScheduleTask" />
</bean>
<!-- 触发器 -->
<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核心监听器 -->
<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, 执行定时任务
Spring的定时任务
通过xml配置
- 执行任务的类
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) + "次执行MySchedule的定时任务");
}
- 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
第二种方式,通过注解
- 执行任务的类
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) + "次执行MySchedule的定时任务");
}
}
- 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