Quartz(08) quartz spring web 项目的整合(方法二)

上一章(Quartz(07) quartz spring web 项目的整合(方法一))中使用了MethodInvokingJobDetailFactoryBean 来配置我们自定义的job,缺点是这样的job不能持久化.本文我们采用JobDetailFactoryBean 来注册job

.源码下载地址

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

     <!-- 配置Quartz的两种方式 -->
     <!-- 1.使用 MethodInvokingJobDetailFactoryBean 来注册jobDetail -->
     <!-- 2.使用JobDetailFactoryBean 来注册jobDetail -->
     <!-- 二者之间的区别.使用方法1,自定义的job类是普通的类不需实现job接口,而且targetMethod必须是无参的方法.使用方便,但是job无法持久化(JobStoreTX) -->
     <!-- 二者之间的区别.使用方法2,自定义的job类需实现job接口 使用复杂些但是job可以持久化(JobStoreTX)-->
     <!-- 本例采用第2种方式 ,quartz06使用使用第一种方式配置.实现job持久化的配置在quartz08中实现-->
    <!-- com.quartz.job.Q1,com.quartz.job.Q2 都实现了Job接口 -->
    <bean id="myJobDetail1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.quartz.job.Q1"></property>
        <property name="durability" value="true"/>
    </bean>
    <bean id="myTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="name" value="my_job1"/>
        <property name="group" value="my_group1"/>
        <property name="jobDetail">
            <ref bean="myJobDetail1"/>
        </property>
        <property name="cronExpression">
            <value>0/5 * * * * ?</value>
        </property>
    </bean>
    <bean id="myJobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.quartz.job.Q2"/>
        <property name="durability" value="true"/>
    </bean>
    <bean id="myTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="name" value="my_job1"/>
        <property name="group" value="my_group2"/>
        <property name="jobDetail">
            <ref bean="myJobDetail2"/>
        </property>
        <property name="cronExpression">
            <value>0/5 * * * * ?</value>
        </property>
    </bean>
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="myTrigger1"/>
                <ref bean="myTrigger2"/>
            </list>
        </property>
    </bean>
</beans>
  • 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

实现了com.quartz.job接口的Q1,Q2

package com.quartz.job;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Q1 implements Serializable, Job {

    private static final long serialVersionUID = 6890216263057956690L;

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        System.out.println("------------------------");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
package com.quartz.job;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class Q2 implements Serializable, Job {

    private static final long serialVersionUID = 5004730246347558783L;

    public void execute(JobExecutionContext arg0) {
        System.out.println("************************");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        System.out.println("************************");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

quarty.properties 的配置如下

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

org.quartz.scheduler.instanceName=DefaultQuartzScheduler
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false

org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=10
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

#jobStoreTX TEST
org.quartz.jobStore.misfireThreshold=60000
# TX method
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#jdbc Delegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#table prefix
org.quartz.jobStore.tablePrefix=qrtz_
#
org.quartz.jobStore.dataSource=myDS

org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver   
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/test
org.quartz.dataSource.myDS.user=root   
org.quartz.dataSource.myDS.password=123456   
org.quartz.dataSource.myDS.maxConnections=10

  • 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

这样就是配置好了,启动web项目,发现数据库中已经对我们的任务进行了持久化. 注意点,1.使用JobDetailFactoryBean 注册的job必须实现了com.quartz.job接口 
下一节:Quartz(09) quartz spring web 项目的整合(终极版)

猜你喜欢

转载自blog.csdn.net/zalan01408980/article/details/80429968