zoukankan      html  css  js  c++  java
  • Quartz总结(一):Quartz集成Spring的2个方法

    零、引言

    关于Spring集成Quartz有2种方法:

    1. JobDetailBean.

    2. MethodInvokeJobDetailFactoryBean.

    以下从自身使用和理解以及掌握的知识对其进行阐述。

    需要注意的是,在使用Spring集成Quartz的时候,一定不要忘记引入spring-support这个包:

            <!-- spring-support.jar 这个jar 文件包含支持UI模版(Velocity,FreeMarker,JasperReports),邮件服务,脚本服务(JRuby), 缓存Cache(EHCache),任务计划Scheduling(uartz)方面的类。 
                外部依赖spring-context, (spring-jdbc, Velocity, FreeMarker, JasperReports, BSH, Groovy, JRuby, Quartz, EHCache) -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>4.2.6.RELEASE</version>
            </dependency>

    它协助Spring集成了很多有用的第三方库,包括了邮件服务、定时任务、缓存等。。。

    当然也不要忘记引入Quart的jar包:

            <!-- quartz定时任务 -->
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>2.2.3</version>
            </dependency>

    一、JobDetailBean

    1. 创建一个Job方法,此方法必须继承QuartzJobBean或者实现Job方法。

    public class TestJob extends QuartzJobBean {
        
        @Override
        protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
            System.out.println(TimeUtils.getCurrentTime());
        }
    }

    2. 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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.mc.bsframe.job.TestJob"></property>
            <property name="durability" value="true"></property>
        </bean>
    
        <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
            <property name="jobDetail" ref="jobDetail" />
            <property name="startDelay" value="3000" />
            <property name="repeatInterval" value="2000" />
        </bean>
    
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
        <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <!-- 管理trigger -->
            <property name="triggers">
                <list>
                    <ref bean="simpleTrigger" />
                </list>
            </property>
        </bean>
    </beans>

    二、MethodInvokeJobDetailFactoryBean

    1. 创建一个Job类,此类不需要继承任何类或者实现任何接口:

    package com.mc.bsframe.job;
    
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.TriggerKey;
    import org.quartz.impl.triggers.SimpleTriggerImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.mc.bsframe.service.TestService;
    import com.mc.bsframe.util.TimeUtils;
    
    
    public class TestJob2 {
        
        public void doSomething() {
            System.err.println("****:" + TimeUtils.getCurrentTime());
        }
    
    }

    2. 配置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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- 要执行任务的任务类。 -->
        <bean id="testQuartz" class="com.mc.bsframe.job.TestJob2">
        </bean>
    
        <!-- 将需要执行的定时任务注入JOB中。 -->
        <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="testQuartz"></property>
            <!-- 任务类中需要执行的方法 -->
            <property name="targetMethod" value="doSomething"></property>
            <!-- 上一次未执行完成的,要等待有再执行。 -->
            <property name="concurrent" value="false"></property>
        </bean>
    
        <!-- 基本的定时器,会绑定具体的任务。 -->
        <bean id="testTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
            <property name="jobDetail" ref="testJob"></property>
            <property name="startDelay" value="3000"></property>
            <property name="repeatInterval" value="2000"></property>
        </bean>
    
        <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="testTrigger"></ref>
                </list>
            </property>
        </bean>
    </beans>

    综上:定时任务的基本配置完成。

    三、两种方法的说明

    使用QuartzJobBean,需要继承。而使用MethodInvokeJobDetailFactoryBean则需要指定targetObject(任务实例)和targetMethod(实例中要执行的方法)

    后者优点是无侵入,业务逻辑简单,一目了然,缺点是无法持久化(目前还不太清楚这点!)

    从我使用的经验来说,我更推荐的第二种其中一个很重要的原因就是因为定时任务中注入相关Service的时候,后者可以直接注入,而前者还需要进行Schedular的替换修改。

  • 相关阅读:
    【笔记】二进制文件
    vs2015+64位win10系统ceres-solver编译
    python
    感受函数式编程-scala
    R语言diagram包画订单状态流图
    virtualbox下Centos6.5桥接模式上网配置方法
    配置对IIS上tabular的 HTTP 访问
    centos6.5下逻辑卷操作
    centos6.5下磁盘创建交换分区
    centos6.5下磁盘分区及挂载
  • 原文地址:https://www.cnblogs.com/LiuChunfu/p/5598610.html
Copyright © 2011-2022 走看看