一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - JAVA使用quartz添加定時任務,并依賴注入對象操作

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

2020-09-26 00:42你是格林我是童話 JAVA教程

這篇文章主要介紹了JAVA使用quartz添加定時任務,并依賴注入對象操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#============================================================================
# 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配置文件中獎quartz.xml文件引入即可:

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

然后寫測試類開始測試定時任務:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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());
 
 }
}

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 {
 //調用父類的方法
 Object jobInstance = super.createJobInstance(bundle);
 capableBeanFactory.autowireBean(jobInstance);
 return jobInstance;
 }
}

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

<bean id="jobFactory" class="com.wy.care60.job.MyJobFactory"></bean>

然后在把org.springframework.scheduling.quartz.SchedulerFactoryBean的jobFactory設置成我們自己的。(config-quartz.xml中紅色部分)

?
1
2
3
4
<bean name="MyScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <!-- 其他屬性省略 -->
  <property name="jobFactory" ref="jobFactory"></property>
</bean>

config-quartz.xml完整版如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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添加定時任務,并依賴注入對象操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/Sometimes__/article/details/78864126

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2020中文字幕 | a在线观看欧美在线观看 | 91在线老王精品免费播放 | 毛片网在线观看 | 亚洲天堂网站 | 按摩院已婚妇女中文字幕 | 女人张开腿 让男人桶个爽 免费观看 | 国产一区二区视频免费 | 成人二区| 久草在线精彩免费视频 | 亚洲欧美日韩中文字幕久久 | 性色AV一区二区三区V视界影院 | 男人jj视频 | 国产精品欧美亚洲韩国日本99 | 亚洲国产成人综合 | 王王的视频ivk | 99精品国产成人a∨免费看 | 亚洲一卡2卡4卡5卡6卡残暴在线 | 午夜片无码区在线观看 | 国产va欧美va在线观看 | 久久一er精这里有精品 | 好大好爽好涨太深了小喜 | 久九九精品免费视频 | 亚洲系列在线 | 国产欧美日韩成人 | 日本性漫画 | 肥胖女性大bbbbbb视频女厕 | 色多多影院 | 成年看片免费高清观看 | 欧美日韩亚洲国内综合网香蕉 | 亚洲国产福利精品一区二区 | 亚州性夜夜射在线观看 | 国产探花视频在线观看 | 高h巨肉play 高h短篇辣肉各种姿势bl | 国产精品免费网站 | 国内自拍成人网在线视频 | 国产精品视频视频久久 | 为什么丈夫插我我却喜欢被打着插 | 息与子中文字幕完整在线 | 爆操女友 | 无遮挡h肉动漫在线观看电车 |