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

 更新时间:2020年09月25日 14:24:15   作者:你是格林我是童话  
这篇文章主要介绍了JAVA使用quartz添加定时任务,并依赖注入对象操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

最近在写定时任务,以前没接触过。查了些相关资料说使用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添加定时任务,并依赖注入对象操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 浅析Java中对称与非对称加密算法原理与使用

    浅析Java中对称与非对称加密算法原理与使用

    密码学是研究编制密码和破译密码的技术科学。这篇文章主要为大家介绍了Java中对称与非对称加密算法的原理与使用,感兴趣的小伙伴可以了解一下
    2023-03-03
  • SpringBoot整合分布式锁redisson的示例代码

    SpringBoot整合分布式锁redisson的示例代码

    这篇文章主要介绍了SpringBoot整合分布式锁redisson,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • MyBatis插入时获取自增主键方法

    MyBatis插入时获取自增主键方法

    MyBatis 3.2.6插入时候获取自增主键方法有两种,下面以以MySQL5.5为例通过两种方法给大家介绍mybatis获取自增主键的方法,一起看看吧
    2016-11-11
  • SpringBoot @Value与@ConfigurationProperties二者有哪些区别

    SpringBoot @Value与@ConfigurationProperties二者有哪些区别

    这篇文章主要介绍了SpringBoot @Value与@ConfigurationProperties二者的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • java数据结构基础:栈

    java数据结构基础:栈

    这篇文章主要介绍了Java的数据解构基础,希望对广大的程序爱好者有所帮助,同时祝大家有一个好成绩,需要的朋友可以参考下,希望能给你带来帮助
    2021-07-07
  • 从一道面试题看你对java的理解程度

    从一道面试题看你对java的理解程度

    这篇文章主要给大家介绍了关于如何从一道面试题看你对java的理解程度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧
    2018-09-09
  • 基于Java解决华为机试之字符串加解密 

    基于Java解决华为机试之字符串加解密 

    这篇文章主要介绍了基于Java解决华为机试之字符串加解密,问题描述展开主题即详细代码的分享完成文章内容,具有一的的参考价值,需要的小伙伴可以参考一下。希望对你有所帮助
    2022-02-02
  • Java基础之TreeMap详解

    Java基础之TreeMap详解

    这篇文章主要介绍了Java基础之TreeMap详解,文中有非常详细的代码示例,对正在学习java基础的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • Shiro集成Spring之注解示例详解

    Shiro集成Spring之注解示例详解

    Shiro想必大家都知道了,是目前使用率要比spring security都要多的一个权限框架,下面这篇文章主要给大家介绍了关于Shiro集成Spring之注解的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-09-09
  • Mybatis分页插件PageHelper手写实现示例

    Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08

最新评论