quartz有兩種注入方式,MethodInvokingJobDetailFactoryBean和JobDetailBean。web
這裏我用的是JobDetailBean。(MethodInvokingJobDetailFactoryBean也試了下,無奈不成功,看網上有人說重寫兩個類文件,試了下也不行,只好用JobDetailBean了)spring
1.下載quartz-1.8.6包,包的的docs文件夾裏有數據庫建表sql,quartz集羣須要將任務信息實例化到數據庫中,而後各個節點從庫中讀取任務信息。sql
2.在src中添加quartz.properties文件。數據庫
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.selectWithLockSQL=SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME \= ?
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = quartzdataSource
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
3.src中添加applicationContext-Quartz.xml文件,內容以下,quartz自帶的鏈接池是DBCP,這個鏈接池問題不少性能也很差,因此改爲了c3p0app
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="syncSpOriinfoService" class="com.jxet.quartz.service.sms.SyncSpOriinfoService" /> <bean id="syncSpOriinfoBean" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.jxet.quartz.client.SyncSpOriinfoServiceClient </value> </property> <!--採用jobDataAsMap方式進行quartzService注入 --> <property name="jobDataAsMap"> <map> <entry key="targetObject" value="syncSpOriinfoService" /> <entry key="targetMethod" value="syncSpOriinfo" /> </map> </property> </bean> <bean id="syncSpOriinfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <ref bean="syncSpOriinfoBean" /> </property> <!-- 程序啓動10秒後運行 --> <property name="startDelay"> <value>10000</value> </property> <!-- 1分鐘啓動一次 --> <property name="repeatInterval"> <value>60000</value> </property> </bean> <bean id="quartzdataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- c3p0鏈接線程池配置文件 -->
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="jdbcUrl" value="jdbc:sqlserver://192.168.32.160:1433;databaseName=quartz" /> <property name="user" value="sa" /> <property name="password" value="123" /> </bean> <bean id="timerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource"> <ref bean="quartzdataSource" /> <!--Spring中對於的數據源--> </property> <property name="configLocation" value="classpath:quartz.properties"/> <property name="triggers"> <list> <ref bean="syncSpOriinfoTrigger" /> </list> </property> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> </bean> </beans>
4.根據 QuartzJobBean 來重寫一個本身的類,而後使用 SPRING 把這個重寫的類注入 appContext 中後,再使用 AOP 技術反射出原有的 quartzJobx( 就是開發人員原來已經作好的用於執行 QUARTZ 的 JOB 的執行類 ) 。ide
public class SyncSpOriinfoServiceClient extends QuartzJobBean { private final Log log = LogFactory.getLog(SyncSpOriinfoServiceClient.class); @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { log.info("execute [" + targetObject + "] at once>>>>>>"); ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"}); Object otargetObject = ctx.getBean(targetObject); Method m = null; try { m = otargetObject.getClass().getMethod(targetMethod, new Class[]{}); m.invoke(otargetObject, new Object[]{}); } catch (SecurityException e) { log.error(e); } catch (NoSuchMethodException e) { log.error(e); } } catch (Exception e) { throw new JobExecutionException(e); } } private String targetObject; private String targetMethod; public void setTargetObject(String targetObject) { this.targetObject = targetObject; } public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; } }
5.寫業務service(syncSpOriinfoService),而後啓動就ok了sqlserver
須要注意的地方是,程序第一次啓動時,會將任務信息實例化到數據庫中,之後修改任務信息必須修改數據庫中的任務,直接修改項目中的信息是沒有用的。看來之後還得整個項目來維護了.性能