========= 本文,只涉及到quartz集群搭建应用 ==========
1,下载quartz集群数据库表压缩包。
下载地址:链接: https://pan.baidu.com/s/1y6rOUfUuXg189Z4Fta3tyg 密码: j29f
解压打开文件夹,选择对应的数据库表sql脚本:
2,选择对应的sql脚本。到数据库里面执行创建表。我这里是mysql数据库。
3,编写job类中可以使用Spring自动注入
package com.cong.quartz;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
/**
* @author
* @description job类支持spring的自动注入
*/
public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware{
private transient AutowireCapableBeanFactory beanFactory;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
beanFactory = applicationContext.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception{
Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
3.1,job类
package com.cong.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.cong.service.GoodsOrderService;
public class GoodsOrderStatusQuartz extends QuartzJobBean{
@Autowired
private GoodsOrderService goodsOrderService;
@Override
protected void executeInternal(JobExecutionContext context)throws JobExecutionException {
try {
goodsOrderService.synOrderStatus();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4,配置quartz.properties文件
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO
#==============================================================
#Configure JobStore
#==============================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#==============================================================
#Configure ThreadPool
#==============================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread =true
5,整合spring与quartz的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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- quartz集群总管理类 开始 -->
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 避免启动的时候,定时器连续执行多次。设置延迟加载10秒,即启动后10秒再执行 -->
<property name="startupDelay" value="10"/>
<!-- 开启修改触发时间同步更新数据库执行时间。如有需求才开启,建议使用默认关闭就好了
<property name="overwriteExistingJobs" value="true" /> -->
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:quartz.properties" />
<!-- 配置job类可以使用@Autowired注入 -->
<property name="jobFactory">
<bean class="com.cong.quartz.AutowiringSpringBeanJobFactory"/>
</property>
<property name="triggers">
<list>
<ref bean="timingGoodsOrderStatusCronTrigger" />
</list>
</property>
</bean>
<!-- quartz集群总管理类 结束-->
<!-- 定时扫描当天晚上1点未支付单 开始 -->
<bean name="timingGoodsOrderStatusJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.cong.quartz.GoodsOrderStatusQuartz" />
<property name="durability" value="true"/>
</bean>
<bean id="timingGoodsOrderStatusCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="timingGoodsOrderStatusJobDetail" />
<property name="cronExpression" value="0 0 1 * * ?" />
</bean>
<!-- 定时扫描当天晚上1点未支付单 结束 -->
</beans>
到此,quartz的集群就搭建完了。是不是很简单呢?
可能遇到的问题:
数据库版本可能与驱动包不匹配。导致quartz抛此异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT' at line 1
解决方案:
把驱动包换成对应的版本。如: