Spring整合Quartz(Spring:4.2.6.RELEASE+Quartz 2.2.1)

来源:互联网 发布:王家卫我爱你知乎 编辑:程序博客网 时间:2024/06/06 01:02

前言

Quartz作为一种常用的定时任务框架,有一定的使用场景,因此以下介绍Spring 整合 Quartz。因为是一篇介绍使用的博文,所以只介绍测试通过的配置:

配置详解

  1. 项目版本配置:

    //gradleext{springVersion = "4.2.6.RELEASE"}dependencies{//Springcompile "org.springframework:spring-beans:$springVersion"compile "org.springframework:spring-context:$springVersion"compile "org.springframework:spring-core:$springVersion"compile "org.springframework:spring-oxm:$springVersion"compile "org.springframework:spring-web:$springVersion"compile "org.springframework:spring-webmvc:$springVersion"compile "org.springframework:spring-tx:$springVersion"compile "org.springframework:spring-aop:$springVersion"compile "org.springframework:spring-context-support:$springVersion"compile "org.springframework:spring-test:$springVersion"compile "org.springframework:spring-orm:$springVersion"compile "org.springframework:spring-jdbc:$springVersion"// https://mvnrepository.com/artifact/org.apache.archiva.redback.components/spring-quartzcompile "org.apache.archiva.redback.components:spring-quartz:2.1"}
  2. Spring+Quartz配置:

    spring-quartz.xml

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id = "BaseJob" class="com.xxx.quartz.DailyJob"/><bean name="dailyJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    <property name="targetObject">      <ref bean="BaseJob"/>    </property>    <property name="targetMethod">  <!-- 要执行的方法名称 -->      <value>execute</value>    </property></bean><!-- 时间调度规则 --!><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">    <property name="jobDetail" ref="dailyJob" />        <!-- 此处具体规则可参考quartz的官方文档或者其他博客 --!>        <!-- 此处意思是每天每小时每隔一分钟执行一次 --!>    <property name="cronExpression" value="0 0/1 * * * ? *" /></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    <property name="triggers">        <list>            <ref bean="cronTrigger" />        </list>    </property></bean></beans>
    public class DailyJob {@Resourceprivate DrivingRecordService drivingRecordService;private static final Logger log = Logger.getLogger(DailyJob.class);protected void execute() throws JobExecutionException {    log.info("before update");    try{        drivingRecordService.refreshRedisCache();    } catch(Exception e){        e.printStackTrace();    }    log.info("finish update");}}
    <?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="          http://www.springframework.org/schema/beans               http://www.springframework.org/schema/beans/spring-beans.xsd          http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context.xsd          http://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop.xsd          http://www.springframework.org/schema/tx          http://www.springframework.org/schema/tx/spring-tx.xsd">    <context:component-scan base-package="com.xxx.service"/>    <import resource="spring-quartz.xml"/>  </beans>

    web.xml

    <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring/springContext.xml</param-value>    </context-param>    <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>

综述

首先这种配置方式与常规配置方式不同。Job并没有实现Quartz的job接口,也没有继承Spring中的QuartzJobBean类,而是采用了普通的POJO类的形式。在配置中,通过Spring的MethodInvokingJobDetailFactoryBean配合类名和类方法构造出一个Job类,用这个代理的job类配合时间调度器cronTrigger和容器 Scheduler完成任务调度。

​ 这种配置的最大的优点是可以注入Service。而常规配置方法,Job注入Service后空指针异常。这是解决这个问题最简便的方式。

原创粉丝点击