Quartz学习——Spring和Quartz集成详解(三)

Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度。下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quartz学习——2、简单入门示例Demo 
的流程相似,介绍Spring和Quartz集成存储方式使用的是RAM方式和JDBC方式! 
:在企业级开发过程中,正常情况下不会使用RAM方式进行任务的存储,都是使用JDBC方式。方便学习,这两种方式都进行介绍! 
本篇中的代码下载地址:spring_quartz demo

一. Jar包依赖 
使用Maven进行Jar包的管理,使用的jar包如下:

<span style="color:#000000"><code><span style="color:#880000"><!-- quartz 的jar --></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
         <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.quartz-scheduler<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
         <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>quartz<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
         <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>2.2.1<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.quartz-scheduler<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>quartz-jobs<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>2.2.1<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
    <span style="color:#880000"><!-- spring相关jar --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-context<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>4.0.5.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-context-support<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>4.0.5.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-jdbc<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>4.0.5.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-orm<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>4.0.5.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-tx<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>4.0.5.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-aspects<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>3.2.3.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-test<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>4.0.5.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-oxm<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>4.0.5.RELEASE<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>

    <span style="color:#880000"><!-- 日志相关jar包 --></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.slf4j<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>slf4j-api<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>1.7.5<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.slf4j<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>jcl-over-slf4j<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>1.7.5<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span><span style="color:#880000"><!--   Failed to load class "org.slf4j.impl.StaticLoggerBinder". --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>ch.qos.logback<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>logback-classic<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>1.0.13<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span>
    <span style="color:#880000"><!-- MySql的包 --></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>mysql<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>mysql-connector-java<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">version</span>></span>5.1.31<span style="color:#006666"></<span style="color:#4f4f4f">version</span>></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span></code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

二.任务类Job

<span style="color:#000000"><code><span style="color:#000088">package</span> com.dufy.spring.quartz.chapter01.job;

<span style="color:#000088">import</span> java.text.SimpleDateFormat;
<span style="color:#000088">import</span> java.util.Date;

<span style="color:#000088">import</span> org.quartz.Job;
<span style="color:#000088">import</span> org.quartz.JobExecutionContext;
<span style="color:#000088">import</span> org.quartz.JobExecutionException;
<span style="color:#000088">import</span> org.slf4j.Logger;
<span style="color:#000088">import</span> org.slf4j.LoggerFactory;

<span style="color:#880000">/**
 * Spring 整合 Quartz 的  Hello World!
 *<span style="color:#4f4f4f"> @author</span> dufy
 *<span style="color:#4f4f4f"> @date</span> 2017.02.26
 *
 */</span>
<span style="color:#000088">public</span> <span style="color:#000088">class</span> <span style="color:#4f4f4f">HelloWorldJob</span> <span style="color:#000088">implements</span> <span style="color:#4f4f4f">Job</span>{

    <span style="color:#000088">private</span> Logger log = LoggerFactory.getLogger(<span style="color:#000088">this</span>.getClass());

    <span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">execute</span>(JobExecutionContext arg0) <span style="color:#000088">throws</span> JobExecutionException {
        log.info(<span style="color:#009900">"This is a first spring combine quartz !"</span>);
        log.info(<span style="color:#009900">"Welcome to Spring_Quartz World!"</span>+ <span style="color:#000088">new</span> SimpleDateFormat(<span style="color:#009900">"yyyy-MM-dd HH:mm:ss"</span>).format(<span style="color:#000088">new</span> Date()) );
        log.info(<span style="color:#009900">"Let's begin ! \n \n"</span>);
    }

}
</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

测试类:

<span style="color:#000000"><code><span style="color:#000088">package</span> com.dufy.spring.quartz.chapter01.job;

<span style="color:#000088">import</span> org.springframework.context.ApplicationContext;
<span style="color:#000088">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;

<span style="color:#000088">public</span> <span style="color:#000088">class</span> <span style="color:#4f4f4f">HWTest</span> {

    <span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">void</span> <span style="color:#009900">main</span>(String[] args) {
        ApplicationContext ac = <span style="color:#000088">new</span> ClassPathXmlApplicationContext(<span style="color:#009900">"spring_quartz.xml"</span>);


    }
}
</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

三.配置文件 spring_quartz.xml和运行结果

1:RAM存储方式的xml配置文件

(1):配置文件

<span style="color:#000000"><code><span style="color:#006666"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color:#006666"><<span style="color:#4f4f4f">beans</span> <span style="color:#4f4f4f">xmlns</span>=<span style="color:#009900">"http://www.springframework.org/schema/beans"</span>
    <span style="color:#4f4f4f">xmlns:context</span>=<span style="color:#009900">"http://www.springframework.org/schema/context"</span> <span style="color:#4f4f4f">xmlns:p</span>=<span style="color:#009900">"http://www.springframework.org/schema/p"</span>
    <span style="color:#4f4f4f">xmlns:aop</span>=<span style="color:#009900">"http://www.springframework.org/schema/aop"</span> <span style="color:#4f4f4f">xmlns:tx</span>=<span style="color:#009900">"http://www.springframework.org/schema/tx"</span>
    <span style="color:#4f4f4f">xmlns:xsi</span>=<span style="color:#009900">"http://www.w3.org/2001/XMLSchema-instance"</span>
    <span style="color:#4f4f4f">xsi:schemaLocation</span>=<span style="color:#009900">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"</span>></span>

    <span style="color:#880000"><!-- 
        Spring整合Quartz进行配置遵循下面的步骤:
        1:定义工作任务的Job
        2:定义触发器Trigger,并将触发器与工作任务绑定
        3:定义调度器,并将Trigger注册到Scheduler
     --></span>
    <span style="color:#880000"><!-- 1:定义任务的bean ,这里使用JobDetailFactoryBean,也可以使用MethodInvokingJobDetailFactoryBean ,配置类似--></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"hwJob"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.scheduling.quartz.JobDetailFactoryBean"</span>></span>
        <span style="color:#880000"><!-- 指定job的名称 --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"name"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"hw_job"</span>/></span>
        <span style="color:#880000"><!-- 指定job的分组 --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"group"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"hw_group"</span>/></span>
        <span style="color:#880000"><!-- 指定具体的job类 --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"jobClass"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"com.dufy.spring.quartz.chapter01.job.HelloWorldJob"</span>/></span>
        <span style="color:#880000"><!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中会删除该任务  --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"durability"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"true"</span>/></span>
        <span style="color:#880000"><!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"applicationContextJobDataKey"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"applicationContext"</span>/></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
    <span style="color:#880000"><!-- 2.1:定义触发器的bean,定义一个Simple的Trigger,一个触发器只能和一个任务进行绑定 --></span>
    <span style="color:#880000"><!-- <bean name="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        指定Trigger的名称
        <property name="name" value="hw_trigger"/>
        指定Trigger的名称
        <property name="group" value="hw_trigger_group"/>
        指定Tirgger绑定的Job
        <property name="jobDetail" ref="hwJob"/>
        指定Trigger的延迟时间 1s后运行
        <property name="startDelay" value="1000"/>
        指定Trigger的重复间隔  5s
        <property name="repeatInterval" value="5000"/>
        指定Trigger的重复次数
        <property name="repeatCount" value="5"/>
    </bean> --></span>

    <span style="color:#880000"><!-- 2.2:定义触发器的bean,定义一个Cron的Trigger,一个触发器只能和一个任务进行绑定 --></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"cronTrigger"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.scheduling.quartz.CronTriggerFactoryBean"</span>></span>
        <span style="color:#880000"><!-- 指定Trigger的名称 --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"name"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"hw_trigger"</span>/></span>
        <span style="color:#880000"><!-- 指定Trigger的名称 --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"group"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"hw_trigger_group"</span>/></span>
        <span style="color:#880000"><!-- 指定Tirgger绑定的Job --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"jobDetail"</span> <span style="color:#4f4f4f">ref</span>=<span style="color:#009900">"hwJob"</span>/></span>
        <span style="color:#880000"><!-- 指定Cron 的表达式 ,当前是每隔1s运行一次 --></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"cronExpression"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"0/1 * * * * ?"</span> /></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>


    <span style="color:#880000"><!-- 3.定义调度器,并将Trigger注册到调度器中 --></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"scheduler"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.scheduling.quartz.SchedulerFactoryBean"</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"triggers"</span>></span>
            <span style="color:#006666"><<span style="color:#4f4f4f">list</span>></span>
                <span style="color:#880000"><!-- <ref bean="simpleTrigger"/> --></span>
                <span style="color:#006666"><<span style="color:#4f4f4f">ref</span> <span style="color:#4f4f4f">bean</span>=<span style="color:#009900">"cronTrigger"</span>/></span>
            <span style="color:#006666"></<span style="color:#4f4f4f">list</span>></span>
        <span style="color:#006666"></<span style="color:#4f4f4f">property</span>></span>
        <span style="color:#880000"><!-- <property name="autoStartup" value="true" /> --></span>
    <span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>

<span style="color:#006666"></<span style="color:#4f4f4f">beans</span>></span></code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

(2):运行结果

下图为Cron的Trigger运行结果,Simple的Trigger运行结果,可以自行进行查看: 
这里写图片描述

2:JDBC存储方式的xml配置文件

(1):配置文件

<span style="color:#000000"><code><span style="color:#006666"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color:#006666"><<span style="color:#4f4f4f">beans</span> <span style="color:#4f4f4f">xmlns</span>=<span style="color:#009900">"http://www.springframework.org/schema/beans"</span>
    <span style="color:#4f4f4f">xmlns:context</span>=<span style="color:#009900">"http://www.springframework.org/schema/context"</span> <span style="color:#4f4f4f">xmlns:p</span>=<span style="color:#009900">"http://www.springframework.org/schema/p"</span>
    <span style="color:#4f4f4f">xmlns:aop</span>=<span style="color:#009900">"http://www.springframework.org/schema/aop"</span> <span style="color:#4f4f4f">xmlns:tx</span>=<span style="color:#009900">"http://www.springframework.org/schema/tx"</span>
    <span style="color:#4f4f4f">xmlns:xsi</span>=<span style="color:#009900">"http://www.w3.org/2001/XMLSchema-instance"</span>
    <span style="color:#4f4f4f">xsi:schemaLocation</span>=<span style="color:#009900">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"</span>></span>


    <span style="color:#880000"><!-- =========JDBC版=========== --></span>
    <span style="color:#880000"><!-- 
        持久化数据配置,需要添加quartz.properties
     --></span>
    <span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"scheduler"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.scheduling.quartz.SchedulerFactoryBean"</span>></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"applicationContextSchedulerContextKey"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"applicationContextKey"</span>/></span>
        <span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"configLocation"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"classpath:quartz.properties"</span>/></span>   
    <span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>

<span style="color:#006666"></<span style="color:#4f4f4f">beans</span>></span></code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

(2):quartz.properties

<span style="color:#000000"><code><span style="color:#009900"># Default Properties file for use by StdSchedulerFactory</span>
<span style="color:#009900"># to create a Quartz Scheduler Instance, if a different</span>
<span style="color:#009900"># properties file is not explicitly specified.</span>
<span style="color:#009900">#</span>

<span style="color:#009900">#============================================================================</span>
<span style="color:#009900"># Configure Main Scheduler Properties</span>
<span style="color:#009900">#============================================================================</span>
org.quartz.scheduler.instanceName: dufy_test
org<span style="color:#009900">.quartz</span><span style="color:#009900">.scheduler</span><span style="color:#009900">.instanceId</span> = AUTO

org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
<span style="color:#009900">#============================================================================</span>
<span style="color:#009900"># Configure ThreadPool</span>
<span style="color:#009900">#============================================================================</span>
org.quartz.threadPool.class: org<span style="color:#009900">.quartz</span><span style="color:#009900">.simpl</span><span style="color:#009900">.SimpleThreadPool</span>
org.quartz.threadPool.threadCount: <span style="color:#006666">2</span>
org.quartz.threadPool.threadPriority: <span style="color:#006666">5</span>
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: <span style="color:#006666">60000</span>
<span style="color:#009900">#============================================================================</span>
<span style="color:#009900"># Configure JobStore</span>
<span style="color:#009900">#============================================================================</span>

<span style="color:#009900">#default config</span>
<span style="color:#009900">#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore</span>
<span style="color:#009900">#持久化配置</span>
org.quartz.jobStore.class:org<span style="color:#009900">.quartz</span><span style="color:#009900">.impl</span><span style="color:#009900">.jdbcjobstore</span><span style="color:#009900">.JobStoreTX</span>
org.quartz.jobStore.driverDelegateClass:org<span style="color:#009900">.quartz</span><span style="color:#009900">.impl</span><span style="color:#009900">.jdbcjobstore</span><span style="color:#009900">.StdJDBCDelegate</span>
org.quartz.jobStore.useProperties:true

<span style="color:#009900">#============================================================================</span>
<span style="color:#009900">#havent cluster spring</span>
<span style="color:#009900">#============================================================================</span>
org<span style="color:#009900">.quartz</span><span style="color:#009900">.jobStore</span><span style="color:#009900">.isClustered</span> = false  

<span style="color:#009900">#数据库表前缀</span>
org.quartz.jobStore.tablePrefix:qrtz_
org.quartz.jobStore.dataSource:qzDS

<span style="color:#009900">#============================================================================</span>
<span style="color:#009900"># Configure Datasources</span>
<span style="color:#009900">#============================================================================</span>
<span style="color:#009900">#JDBC驱动</span>
org.quartz.dataSource.qzDS.driver:<span style="color:#000088">com</span><span style="color:#009900">.mysql</span><span style="color:#009900">.jdbc</span><span style="color:#009900">.Driver</span>
org.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:<span style="color:#006666">3306</span>/quartz_test
org.quartz.dataSource.qzDS.user:root
org.quartz.dataSource.qzDS.password:root
org.quartz.dataSource.qzDS.maxConnection:<span style="color:#006666">10</span></code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

(3):HelloWorldJob.java没有改动,HWTest.java修改如下

<span style="color:#000000"><code>package <span style="color:#000088">com</span><span style="color:#009900">.dufy</span><span style="color:#009900">.spring</span><span style="color:#009900">.quartz</span><span style="color:#009900">.chapter</span>01<span style="color:#009900">.job</span><span style="color:#880000">;</span>

import org<span style="color:#009900">.quartz</span><span style="color:#009900">.CronScheduleBuilder</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.JobBuilder</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.JobDetail</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.Scheduler</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.SchedulerException</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.SimpleScheduleBuilder</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.Trigger</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.TriggerBuilder</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.impl</span><span style="color:#009900">.StdScheduler</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.quartz</span><span style="color:#009900">.impl</span><span style="color:#009900">.StdSchedulerFactory</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.springframework</span><span style="color:#009900">.context</span><span style="color:#009900">.ApplicationContext</span><span style="color:#880000">;</span>
import org<span style="color:#009900">.springframework</span><span style="color:#009900">.context</span><span style="color:#009900">.support</span><span style="color:#009900">.ClassPathXmlApplicationContext</span><span style="color:#880000">;</span>

public class HWTest {

     private static Scheduler scheduler<span style="color:#880000">;</span>

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(<span style="color:#009900">"spring_quartz.xml"</span>)<span style="color:#880000">;</span>

         scheduler = (StdScheduler)ac<span style="color:#009900">.getBean</span>(<span style="color:#009900">"scheduler"</span>)<span style="color:#880000">;</span>

         startSchedule()<span style="color:#880000">;</span>
    }

    public static void startSchedule() {
        try {
            // <span style="color:#006666">1</span>、创建一个JobDetail实例,指定Quartz
            JobDetail jobDetail = JobBuilder<span style="color:#009900">.newJob</span>(HelloWorldJob<span style="color:#009900">.class</span>)
            // 任务执行类
                    <span style="color:#009900">.withIdentity</span>(<span style="color:#009900">"job1_1"</span>, <span style="color:#009900">"jGroup1"</span>)
                    // 任务名,任务组
                    <span style="color:#009900">.build</span>()<span style="color:#880000">;</span>

            // 触发器类型
            //SimpleScheduleBuilder builder = SimpleScheduleBuilder
                    // 设置执行次数
                    //<span style="color:#009900">.repeatSecondlyForTotalCount</span>(<span style="color:#006666">5</span>)<span style="color:#880000">;</span>

            CronScheduleBuilder builder = CronScheduleBuilder<span style="color:#009900">.cronSchedule</span>(<span style="color:#009900">"0/2 * * * * ?"</span>)<span style="color:#880000">;</span>
            // <span style="color:#006666">2</span>、创建Trigger
            Trigger trigger = TriggerBuilder<span style="color:#009900">.newTrigger</span>()
                    <span style="color:#009900">.withIdentity</span>(<span style="color:#009900">"trigger1_1"</span>, <span style="color:#009900">"tGroup1"</span>)<span style="color:#009900">.startNow</span>()
                    <span style="color:#009900">.withSchedule</span>(builder)
                    <span style="color:#009900">.build</span>()<span style="color:#880000">;</span>

            // <span style="color:#006666">3</span>、创建Scheduler
            scheduler = StdSchedulerFactory<span style="color:#009900">.getDefaultScheduler</span>()<span style="color:#880000">;</span>
            scheduler<span style="color:#009900">.start</span>()<span style="color:#880000">;</span>
            // <span style="color:#006666">4</span>、调度执行
            scheduler<span style="color:#009900">.scheduleJob</span>(jobDetail, trigger)<span style="color:#880000">;</span>

        } catch (SchedulerException e) {
            e<span style="color:#009900">.printStackTrace</span>()<span style="color:#880000">;</span>
        }
    }

}
</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

使用的Cron类型的触发进行存储,运行期间可以在数据库查看到指定的任务已经保存到数据库,并看到控制台打印的信息!

(4):输出的结果

这里写图片描述


Quartz专栏系列

1.Quartz学习——Quartz大致介绍(一)

2.Quartz学习——Quartz简单入门Demo(二)

3.Quartz学习——Spring和Quartz集成详解(三)

4.Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)

5.Quartz源码——JobStore保存JonDetail和Trigger源码分析(一)

6.Quartz源码——scheduler.start()启动源码分析(二)

7.Quartz源码——QuartzSchedulerThread.run() 源码分析(三)

8.Quartz源码——Quartz调度器的Misfire处理规则(四)

 

原文地址:https://blog.csdn.net/u010648555/article/details/54891264

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值