最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年09月25日 14:24:15   作者:你是格林我是童話  
這篇文章主要介紹了JAVA使用quartz添加定時(shí)任務(wù),并依賴注入對(duì)象操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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

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

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

<?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>

然后寫測(cè)試類開始測(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í)間可以打印出來,證明定時(shí)任務(wù)成功開啟;但是同時(shí)也發(fā)現(xiàn)了一個(gè)問題,就是依賴注入的 mProjectMapper值為null。

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

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;
 } 
}

然后把這個(gè)類配置到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ù),并依賴注入對(duì)象操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Spring Boot 開發(fā)REST接口最佳實(shí)踐

    淺談Spring Boot 開發(fā)REST接口最佳實(shí)踐

    這篇文章主要介紹了淺談Spring Boot 開發(fā)REST接口最佳實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • java實(shí)現(xiàn)隨機(jī)輸出300題四則運(yùn)算

    java實(shí)現(xiàn)隨機(jī)輸出300題四則運(yùn)算

    本文主要介紹了java實(shí)現(xiàn)隨機(jī)輸出300題四則運(yùn)算實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • 深度解析Spring AI請(qǐng)求與響應(yīng)機(jī)制的核心邏輯

    深度解析Spring AI請(qǐng)求與響應(yīng)機(jī)制的核心邏輯

    我們?cè)谇懊娴膬蓚€(gè)章節(jié)中基本上對(duì)Spring Boot 3版本的新變化進(jìn)行了全面的回顧,以確保在接下來研究Spring AI時(shí)能夠避免任何潛在的問題,本文給大家介紹Spring AI請(qǐng)求與響應(yīng)機(jī)制的核心邏輯,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • 詳解java8在Collection中新增加的方法removeIf

    詳解java8在Collection中新增加的方法removeIf

    這篇文章主要介紹了詳解java8在Collection中新增加的方法removeIf的相關(guān)資料,需要的朋友可以參考下
    2018-01-01
  • Spring @Conditional注解從源碼層講解

    Spring @Conditional注解從源碼層講解

    @Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊(cè)bean,這篇文章主要介紹了Spring @Conditional注解示例詳細(xì)講解,需要的朋友可以參考下
    2023-01-01
  • 如何用java編寫微信小程序消息提醒推送

    如何用java編寫微信小程序消息提醒推送

    最近參與開發(fā)的項(xiàng)目有用到微信模板消息推送,在這離記錄一下,下面這篇文章主要給大家介紹了關(guān)于如何用java編寫微信小程序消息提醒推送的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • 關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理

    關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理

    這篇文章主要介紹了關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • spring?boot集成loback日志配置的示例代碼

    spring?boot集成loback日志配置的示例代碼

    這篇文章主要介紹了spring?boot集成loback日志配置的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • 基于spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯

    基于spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯

    這篇文章主要介紹了spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置方法

    SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置方法

    本文詳細(xì)介紹了SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置的方法,包括在application.properties中配置多數(shù)據(jù)源,配置MybatisPlus,創(chuàng)建Mapper接口和使用多數(shù)據(jù)源等步驟,此外,還解釋了每一個(gè)配置項(xiàng)目的含義,以便讀者更好地理解和應(yīng)用
    2024-10-10

最新評(píng)論

乌兰察布市| 延寿县| 峨边| 抚宁县| 清原| 开原市| 博兴县| 府谷县| 衡阳市| 赫章县| 门头沟区| 永丰县| 安丘市| 仙桃市| 泸定县| 洛南县| 工布江达县| 永新县| 江山市| 抚远县| 延寿县| 和平区| 疏勒县| 马关县| 和林格尔县| 忻州市| 澳门| 南乐县| 夏邑县| 宿州市| 繁昌县| 洞头县| 尚义县| 铜川市| 忻城县| 奉化市| 留坝县| 开封县| 吉木乃县| 桂平市| 安新县|