JAVA使用quartz添加定時(shí)任務(wù),并依賴(lài)注入對(duì)象操作
最近在寫(xiě)定時(shí)任務(wù),以前沒(méi)接觸過(guò)。查了些相關(guān)資料說(shuō)使用quartz定時(shí)框架。
需要配置文件:config-quartz.xml
相關(guān)配置如下(紅色部分是之后添加的,在后面步驟會(huì)說(shuō)明):
<?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:context="http://www.springframework.org/schema/context" 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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="schedulerName" value="rqmis"></property> <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="configLocation" value="classpath:quartz.properties" /> <property name="autoStartup" value="true"></property> <property name="triggers"> <list> <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="cronExpression" value="0 0 0 * * ?"></property> <property name="jobDetail"> <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.wy.care60.job.HealthPlanJob" /> </bean> </property> </bean> </list> </property> </bean> <!--</property> </bean>--> </beans>
quartz.properties
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName = WrhFrameScheduler org.quartz.scheduler.instanceId = AUTO org.quartz.scheduler.skipUpdateCheck = true #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 12 org.quartz.threadPool.threadPriority = 5 #============================================================================ # Configure JobStore #============================================================================ org.quartz.jobStore.misfireThreshold = 60000 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX #org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate #org.quartz.jobStore.useProperties = false #org.quartz.jobStore.dataSource = myDS #org.quartz.jobStore.tablePrefix = QRTZ_ #org.quartz.jobStore.isClustered = false #============================================================================ # Configure Datasources #============================================================================ #org.quartz.dataSource.myDS.driver = org.postgresql.Driver #org.quartz.dataSource.myDS.URL = jdbc:postgresql://localhost/dev #org.quartz.dataSource.myDS.user = jhouse #org.quartz.dataSource.myDS.password = #org.quartz.dataSource.myDS.maxConnections = 5
最后spring-mvc.xml配置文件中獎(jiǎng)quartz.xml文件引入即可:
<import resource="config-quartz.xml"></import>
然后寫(xiě)測(cè)試類(lèi)開(kāi)始測(cè)試定時(shí)任務(wù):
package com.wy.care60.job; import com.wy.care60.dao.MElementMapper; import com.wy.care60.dao.MInterEnumMapper; import com.wy.care60.dao.MProjectMapper; import com.wy.care60.model.MInterEnum; import com.wy.care60.model.MProject; import org.apache.tools.ant.Project; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * Created by Administrator on 2017/12/20. */ @Component public class HealthPlanJob extends QuartzJobBean { @Autowired MProjectMapper mProjectMapper; @Override public void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println(new Date()); } }
發(fā)現(xiàn)時(shí)間可以打印出來(lái),證明定時(shí)任務(wù)成功開(kāi)啟;但是同時(shí)也發(fā)現(xiàn)了一個(gè)問(wèn)題,就是依賴(lài)注入的 mProjectMapper值為null。
開(kāi)始以為是Spring的原因,導(dǎo)致注解失敗,后來(lái)查了相關(guān)資料發(fā)現(xiàn),不是Spring的原因,而是因?yàn)椋哼@個(gè)Job是由quartz實(shí)例化出來(lái)的,不受Spring的管理,所以就導(dǎo)致注入失敗。解決辦法是自己new一個(gè)類(lèi),讓Spring實(shí)例化這個(gè)類(lèi),代碼如下
import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.scheduling.quartz.AdaptableJobFactory; public class MyJobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { //調(diào)用父類(lèi)的方法 Object jobInstance = super.createJobInstance(bundle); capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
然后把這個(gè)類(lèi)配置到Spring中去,(config-quartz.xml中紅色部分)
<bean id="jobFactory" class="com.wy.care60.job.MyJobFactory"></bean>
然后在把org.springframework.scheduling.quartz.SchedulerFactoryBean的jobFactory設(shè)置成我們自己的。(config-quartz.xml中紅色部分)
<bean name="MyScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 其他屬性省略 --> <property name="jobFactory" ref="jobFactory"></property> </bean>
config-quartz.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" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <bean id="jobFactory" class="com.wy.care60.job.MyJobFactory"></bean> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="schedulerName" value="rqmis"></property> <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="configLocation" value="classpath:quartz.properties" /> <property name="autoStartup" value="true"></property> <property name="jobFactory" ref="jobFactory"></property> <property name="triggers"> <list> <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="cronExpression" value="0 0 0 * * ?"></property> <property name="jobDetail"> <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.wy.care60.job.HealthPlanJob" /> </bean> </property> </bean> </list> </property> </bean> <!--</property> </bean>--> </beans>
到這為止,成功!
以上這篇JAVA使用quartz添加定時(shí)任務(wù),并依賴(lài)注入對(duì)象操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot實(shí)現(xiàn)簡(jiǎn)單的消息對(duì)話的示例代碼
本文主要介紹了springboot實(shí)現(xiàn)簡(jiǎn)單的消息對(duì)話的示例代碼,可以使用WebSocket技術(shù),WebSocket是一種在客戶(hù)端和服務(wù)器之間提供實(shí)時(shí)雙向通信的協(xié)議,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09springboot訪問(wèn)404問(wèn)題的解決辦法
工作中遇到url404問(wèn)題,解決問(wèn)題的進(jìn)程比較崎嶇,寫(xiě)篇文章記錄,下面這篇文章主要給大家介紹了關(guān)于springboot訪問(wèn)404問(wèn)題的解決辦法,文中通過(guò)圖文介紹的非常詳細(xì),要的朋友可以參考下2023-03-03Java多線程模擬銀行系統(tǒng)存錢(qián)問(wèn)題詳解
本文將利用Java多線程模擬一個(gè)簡(jiǎn)單的銀行系統(tǒng),使用兩個(gè)不同的線程向同一個(gè)賬戶(hù)存錢(qián)。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09SpringBoot AOP注解失效問(wèn)題排查與解決(調(diào)用內(nèi)部方法)
這篇文章主要介紹了SpringBoot AOP注解失效問(wèn)題排查與解決(調(diào)用內(nèi)部方法),文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04Hibernate雙向一對(duì)一映射關(guān)系配置代碼實(shí)例
這篇文章主要介紹了Hibernate雙向一對(duì)一映射關(guān)系配置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Spring 實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離的示例
現(xiàn)在大型的電子商務(wù)系統(tǒng),在數(shù)據(jù)庫(kù)層面大都采用讀寫(xiě)分離技術(shù),我們通常的做法就是把查詢(xún)從主庫(kù)中抽取出來(lái),采用多個(gè)從庫(kù),使用負(fù)載均衡,減輕每個(gè)從庫(kù)的查詢(xún)壓力。2017-01-01mybatis 多表關(guān)聯(lián)mapper文件寫(xiě)法操作
這篇文章主要介紹了mybatis 多表關(guān)聯(lián)mapper文件寫(xiě)法操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12解決mysql字符串類(lèi)型的數(shù)字排序出錯(cuò):cast(year as signed)
這篇文章主要介紹了解決mysql字符串類(lèi)型的數(shù)字排序出錯(cuò)問(wèn)題 :cast(year as signed),如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08