quartz数据库方式与web工程整合
?
quartz數據庫方式與web工程整合
??????這兩天在項目中有一個任務,需要靈活配置調度任務時間,并能自由啟動或停止調度。
????? 有關調度的實現我就想到了quartz這個開源調度組件,自己寫這樣一個類似的東西感覺還有一定難度,其實主要是自己在線程方面的經驗、知識不足,有一種恐懼感,好在有開源的解決方案。
????? 以前在web項目中配置過quartz,比如:每天凌晨幾點定時運行一個程序,這只要在工程中的spring配置文件中配置好spring整合quartz的幾個屬性就好。順便總結一下:
?????
?
Xml代碼?
<bean id="sfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="bjcronTrigger" /></list></property></bean><bean id="bjcronTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="miJobDetail" /></property><property name="cronExpression"><value>0 0/5 1 * * ? *</value></property></bean><bean id="miJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="JobMethodBean" /></property><property name="targetMethod"><value>taskTimePoll</value></property></bean><bean id="JobMethodBean"class="com.yinbo.entrust.service.impl.JobMethodBean"><property name="tasktimepollManager"><ref bean="tasktimepollManager" /></property><property name="workFlowManager"><ref bean="workFlowManager" /></property></bean><!-- 任務定時輪詢 --><bean id="tasktimepollDao"class="com.mycompany.entrust.dao.impl.TasktimepollDaoHibernate"autowire="byName" /><bean id="tasktimepollManager"class="com.mycompany.entrust.service.impl.TasktimepollManagerImpl"><property name="tasktimepollDao" ref="tasktimepollDao" /></bean>??????? 這種配置就是對quartz的一種簡單的使用了,調度任務會在spring啟動的時候加載到內存中,按照bjcronTrigger中定義的crontrigger定義的時間按時觸發調度任務。但是這是quartz使用“內存”方式的一種配置,也比較常見,當然對于不使用spring的項目,也可以單獨整合quartz。方法也比較簡單,可以從quartz的doc中找到配置方式,或者看一下《Quartz Job Scheduling Framework 》(附件中可下載)這本書中的例子。
???????? 但是對于想持久化調度任務的狀態,并且靈活調整調度時間的方式來說,上面的內存方式就不能滿足要求了,正如本文開始我遇到的情況,需要采用數據庫方式集成quartz,這部分集成其實在《Quartz Job Scheduling Framework 》中也有較為詳細的介紹,當然doc文檔中也有,但是缺乏和spring集成的實例,我在這里把我在項目中在spring配置quartz數據庫存儲方式的配置也寫一下:
?
Xml代碼?
<bean id="scheduler"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="schedulerName" value="Mscheduler" /><property name="configLocation"><ref local="configLocationResource" /></property><property name="applicationContextSchedulerContextKey"value="applicationContextKey" /><property name="autoStartup" value="false" /></bean><bean id="configLocationResource"class="org.springframework.core.io.ClassPathResource"><constructor-arg value="quartz.properties"type="java.lang.String"></constructor-arg></bean><bean id="schedulerService"class="cn.mycompany.mdms.scheduler.service.SchedulerServiceImpl"><property name="scheduler"><ref bean="scheduler" /></property></bean><!-- 自動掃描作業服務類 --><bean id="monitorDirService"class="cn.mycompany.mdms.monitordir.MonitorDirService"><property name="adm"><ref bean="IMAdapterManager" /></property></bean>??????? 屬性說明:
??????? dataSource:項目中用到的數據源,里面包含了quartz用到的12張數據庫表;
??????? schedulerName:調度器名,我理解主要在調度集群的時候會有用,如果出現多個調度器實例的時候可以用來進行區分,詳細看一下《Quartz Job Scheduling Framework 》;
????????configLocation:用于指明quartz的配置文件的位置,如果不用spring配置quartz的話,本身quartz是通過一個配置文件進行配置的,默認名稱是quartz.properties,里面配置的參數在quartz的doc文檔中都有介紹,可以調整quartz,我在項目中也用這個文件部分的配置了一些屬性,代碼如下:
?
Java代碼?
#============================================================================ # Configure Main Scheduler Properties #============================================================================#org.quartz.scheduler.instanceName = Mscheduler org.quartz.scheduler.instanceId = AUTO#============================================================================ # Configure ThreadPool #============================================================================org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 3 org.quartz.threadPool.threadPriority = 5#============================================================================ # Configure JobStore #============================================================================#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStoreorg.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX #org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties = true #org.quartz.jobStore.dataSource = myDS org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.isClustered = false org.quartz.jobStore.maxMisfiresToHandleAtATime=1 #============================================================================ # Configure Datasources #============================================================================#org.quartz.dataSource.myDS.driver = com.ibm.db2.jcc.DB2Driver #org.quartz.dataSource.myDS.URL = jdbc:db2://localhost:50000/db #org.quartz.dataSource.myDS.user = db2 #org.quartz.dataSource.myDS.password = db2 #org.quartz.dataSource.myDS.maxConnections = 5#============================================================================ # Configure Plugins #============================================================================#org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin #org.quartz.plugin.jobInitializer.fileNames = jobs.xml #org.quartz.plugin.jobInitializer.overWriteExistingJobs = true #org.quartz.plugin.jobInitializer.failOnFileNotFound = true #org.quartz.plugin.jobInitializer.scanInterval = 10 #org.quartz.plugin.jobInitializer.wrapInUserTransaction = false?比如這里面有關數據源的配置部分我就屏蔽掉了,采用spring注入datasource的方式已經進行了配置;
???? applicationContextSchedulerContextKey:
???? 是org.springframework.scheduling.quartz.SchedulerFactoryBean這個類中把spring上下文以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定義的key得到對應的spring上下文;
????? autoStartup:表示是否調度隨工程啟動自動啟動,如果是false表示不自動啟動,則需要調用scheduler.start()進行啟動。
?
?
轉自http://dayang2001911.javaeye.com/blog/248144
轉載于:https://www.cnblogs.com/coolattt/archive/2010/05/25/1743852.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的quartz数据库方式与web工程整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何消除网站安全的七大风险
- 下一篇: 统计SQL2005中数据库中的每张表的记