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

相关文章

  • 浅谈Spring Boot 开发REST接口最佳实践

    浅谈Spring Boot 开发REST接口最佳实践

    这篇文章主要介绍了浅谈Spring Boot 开发REST接口最佳实践,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • java实现随机输出300题四则运算

    java实现随机输出300题四则运算

    本文主要介绍了java实现随机输出300题四则运算实例,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • 深度解析Spring AI请求与响应机制的核心逻辑

    深度解析Spring AI请求与响应机制的核心逻辑

    我们在前面的两个章节中基本上对Spring Boot 3版本的新变化进行了全面的回顾,以确保在接下来研究Spring AI时能够避免任何潜在的问题,本文给大家介绍Spring AI请求与响应机制的核心逻辑,感兴趣的朋友跟随小编一起看看吧
    2024-11-11
  • 详解java8在Collection中新增加的方法removeIf

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

    这篇文章主要介绍了详解java8在Collection中新增加的方法removeIf的相关资料,需要的朋友可以参考下
    2018-01-01
  • Spring @Conditional注解从源码层讲解

    Spring @Conditional注解从源码层讲解

    @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean,这篇文章主要介绍了Spring @Conditional注解示例详细讲解,需要的朋友可以参考下
    2023-01-01
  • 如何用java编写微信小程序消息提醒推送

    如何用java编写微信小程序消息提醒推送

    最近参与开发的项目有用到微信模板消息推送,在这离记录一下,下面这篇文章主要给大家介绍了关于如何用java编写微信小程序消息提醒推送的相关资料,需要的朋友可以参考下
    2023-11-11
  • 关于Spring启动流程及Bean生命周期梳理

    关于Spring启动流程及Bean生命周期梳理

    这篇文章主要介绍了关于Spring启动流程及Bean生命周期梳理,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • spring boot集成loback日志配置的示例代码

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

    这篇文章主要介绍了spring boot集成loback日志配置的示例代码,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2024-01-01
  • 基于spring @Cacheable 注解的spel表达式解析执行逻辑

    基于spring @Cacheable 注解的spel表达式解析执行逻辑

    这篇文章主要介绍了spring @Cacheable 注解的spel表达式解析执行逻辑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • SpringCloud集成MybatisPlus实现MySQL多数据源配置方法

    SpringCloud集成MybatisPlus实现MySQL多数据源配置方法

    本文详细介绍了SpringCloud集成MybatisPlus实现MySQL多数据源配置的方法,包括在application.properties中配置多数据源,配置MybatisPlus,创建Mapper接口和使用多数据源等步骤,此外,还解释了每一个配置项目的含义,以便读者更好地理解和应用
    2024-10-10

最新评论