欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA使用quartz添加定时任务,并依赖注入对象操作

程序员文章站 2022-04-10 09:10:27
最近在写定时任务,以前没接触过。查了些相关资料说使用quartz定时框架。需要配置文件:config-quartz.xml相关配置如下(红色部分是之后添加的,在后面步骤会说明):

最近在写定时任务,以前没接触过。查了些相关资料说使用quartz定时框架。

需要配置文件: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="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配置文件中奖quartz.xml文件引入即可:

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

然后写测试类开始测试定时任务:

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实例化这个类,代码如下

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中红色部分)

<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添加定时任务,并依赖注入对象操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。