使用Spring的Quartz Scheduler进行任务定时调度

使用Spring的Quartz Scheduler进行任务定时调度,使任务定时启动和触发,对应Spring文档 33.6Using the Quartz Scheduler 部分。

一、准备工作

首先使用maven配置webapp项目,目录结构如下,新建myJob测试类及Sping配置文件。
使用Spring的Quartz Scheduler进行任务定时调度_第1张图片
然后添加依赖,pom.xm代码如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>SpringDemogroupId>
  <artifactId>SpringDemoartifactId>
  <packaging>warpackaging>
  <version>1.0-SNAPSHOTversion>
  <name>SpringDemo Maven Webappname>
  <url>http://maven.apache.orgurl>
  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.2.6.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.2.6.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.2.6.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.2.6.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>4.2.6.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>4.2.6.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.quartz-schedulergroupId>
      <artifactId>quartzartifactId>
      <version>2.2.1version>
    dependency>

  dependencies>
  <build>
    <finalName>SpringDemofinalName>
  build>
project>

MyJob测试类中设置一个data数字,每次调用addData()数字加一,代码如下:

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jutal on 17-2-15.
 * Spring web项目触发器配置实例
 */

public class MyJob {

    private int myData;

    public void addData() {
        myData ++;
        System.out.println("Now my data is: " + myData);
    }

    public int getMyData() {
        return myData;
    }

    public void setMyData(int myData) {
        this.myData = myData;
    }
}

在web.xml对Spring配置文件进行初始化:



<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>

  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext*.xmlparam-value>
  context-param>

  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

二、配置Job

之后要配置Job,即要定时执行的任务。首先在Spring配置文件中配置MyJob的Bean,设置myData初始为0:

<bean id="myJobBean" class="MyJob">
        <property name="myData" value="0"/>
bean>

然后配置MethodInvokingJobDetailFactoryBean,目的是指定要执行的任务对应的类及方法等内容:

<bean id="addDataJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
        <property name="targetObject" ref="myJobBean"/>
        
        <property name="targetMethod" value="addData"/>
        
        <property name="concurrent" value="false"/>
bean>

三、配置Tirgger和Scheduler

在配置完Job后对Job的触发器Trigger和调度器Scheduler进行配置,其中触发器中要对执行的时间进行配置,使用了con表达式,关于此表达式的用法可以在另一篇博客《con表达式用法》中找到。配置文件如下:


    <bean id="addDataTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="addDataJob"/>
        
        <property name="cronExpression" value="0/1 * * * * ?"/>
    bean>

    
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="addDataTrigger"/>
            list>
        property>
    bean>

配置后spring配置文件如下:


<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="myJobBean" class="MyJob">
        <property name="myData" value="0"/>
    bean>

    
    <bean id="addDataJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
        <property name="targetObject" ref="myJobBean"/>
        
        <property name="targetMethod" value="addData"/>
        
        <property name="concurrent" value="false"/>
    bean>

    
    <bean id="addDataTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="addDataJob"/>
        
        <property name="cronExpression" value="0/1 * * * * ?"/>
    bean>

    
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="addDataTrigger"/>
            list>
        property>
    bean>

beans>

四、 测试

将项目放到Tomcat服务器上运行,在控制台中会出现结果,每秒进行一次输出:
使用Spring的Quartz Scheduler进行任务定时调度_第2张图片

你可能感兴趣的:(java,spring)