以前是跟孔浩老师学习的quartz1.8版本的,现在quartz升级到了2.x版本,用以前的xml配置一堆错误
1.pom.xml
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
2.spring-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="processToExecute" class="com.yunchuang.maven.mobile.gzt.quartz.ProcessToExecute" />
<!-- quartz2以上版本都改为FactoryBean了,这个要特别注意,否则会报错,而且spring版本必须3以上 -->
<!-- 方法1:继承QuartzJobBean的方法,调用的是类 -->
<bean name="processToExecuteJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.yunchuang.maven.mobile.gzt.quartz.ProcessToExecuteJob" />
<property name="jobDataAsMap">
<map>
<entry key="processToExecute" value-ref="processToExecute" />
</map>
</property>
<!-- 不加这个会报错SchedulerException: Jobs added with no trigger must be durable. -->
<property name="durability" value="true" />
</bean>
<!-- 方法2(推荐):直接调用类里的方法,可以调用service的方法,我觉得这个更好 -->
<bean id="timedJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="gztService" />
<property name="targetMethod" value="setGztToLocal" />
</bean>
<bean id="simpleTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="timedJob" />
<property name="repeatInterval" value="5000" />
<property name="startDelay" value="2000" />
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="processToExecuteJob" />
<property name="repeatInterval" value="5000" />
<property name="startDelay" value="2000" />
</bean>
<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="timedJob" />
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="processToExecuteJob" />
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- <property name="jobDetails">
<list>
<ref bean="processToExecuteJob" />
<ref bean="timedJob" />
</list>
</property> -->
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
<ref bean="cronTrigger2" />
</list>
</property>
</bean>
</beans>
3.ProcessToExecuteJob
package com.yunchuang.maven.mobile.gzt.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* @author 尹冬飞
* @date 2016年12月14日 上午9:09:00
*/
public class ProcessToExecuteJob extends QuartzJobBean {
private ProcessToExecute processToExecute;
public void setProcessToExecute(ProcessToExecute processToExecute) {
this.processToExecute = processToExecute;
}
@Override
protected void executeInternal(JobExecutionContext jobContext) throws JobExecutionException {
processToExecute.output();
}
}
4.ProcessToExecute
package com.yunchuang.maven.mobile.gzt.quartz;
/**
* @author 尹冬飞
* @date 2016年12月1日 下午5:00:08
*/
public class ProcessToExecute {
public void output() {
System.out.println("important stuff");
}
public void output2() {
System.out.println("important stuff2");
}
}
5.最后贴上我最终使用的xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 方法2(推荐):直接调用类里的方法,可以调用service的方法,我觉得这个更好 -->
<bean id="gztJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="gztService" />
<property name="targetMethod" value="setGztToLocal" />
</bean>
<!-- cron表达式可以用在线表达式生成和解析 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="gztJob" />
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
网友评论