av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

JAVA使用quartz添加定時任務(wù),并依賴注入對象操作

瀏覽:4日期:2022-08-23 18:38:43

最近在寫定時任務(wù),以前沒接觸過。查了些相關(guān)資料說使用quartz定時框架。

需要配置文件:config-quartz.xml

相關(guān)配置如下(紅色部分是之后添加的,在后面步驟會說明):

<?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 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 = WrhFrameSchedulerorg.quartz.scheduler.instanceId = AUTO org.quartz.scheduler.skipUpdateCheck = true #============================================================================# Configure ThreadPool #============================================================================ org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount = 12org.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配置文件中獎quartz.xml文件引入即可:

<import resource='config-quartz.xml'></import>

然后寫測試類開始測試定時任務(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. */@Componentpublic class HealthPlanJob extends QuartzJobBean { @Autowired MProjectMapper mProjectMapper; @Override public void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println(new Date()); } }

發(fā)現(xiàn)時間可以打印出來,證明定時任務(wù)成功開啟;但是同時也發(fā)現(xiàn)了一個問題,就是依賴注入的 mProjectMapper值為null。

開始以為是Spring的原因,導(dǎo)致注解失敗,后來查了相關(guān)資料發(fā)現(xiàn),不是Spring的原因,而是因為:這個Job是由quartz實例化出來的,不受Spring的管理,所以就導(dǎo)致注入失敗。解決辦法是自己new一個類,讓Spring實例化這個類,代碼如下

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)用父類的方法 Object jobInstance = super.createJobInstance(bundle); capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }

然后把這個類配置到Spring中去,(config-quartz.xml中紅色部分)

<bean 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 class='com.wy.care60.job.MyJobFactory'></bean> <bean 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添加定時任務(wù),并依賴注入對象操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
主站蜘蛛池模板: 国产午夜精品一区二区三区四区 | 在线看91| 成年网站在线观看 | 亚洲一二三区免费 | 欧美精品成人影院 | 一区二区三区日韩精品 | 日韩二区 | 中文在线观看视频 | 91精品国产综合久久久密闭 | 视频二区| 成人国产免费视频 | 日韩av一区二区在线观看 | 久久久国产一区二区三区 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 成人免费观看男女羞羞视频 | 国产国拍亚洲精品av | 中文字幕一区二区三区在线观看 | 视频一区在线 | 成人免费毛片在线观看 | 久久久激情 | 桃色五月 | 久久一区视频 | 久久久精| 国产亚洲一区二区三区在线观看 | 伊人久麻豆社区 | 亚洲免费一区 | 国产亚洲精品久久情网 | 精品一区二区三区日本 | 国产精品资源在线观看 | 成年网站在线观看 | 欧美视频免费在线 | 婷婷久久五月 | 五月天婷婷综合 | 91亚洲精品国偷拍自产在线观看 | 日韩在线播放av | 午夜影院在线观看版 | 亚洲精品一区二三区不卡 | 久久久久国产一区二区三区 | 午夜视频在线免费观看 | 一级片在线观看视频 | 亚洲视频在线观看一区二区三区 |