Spring整合Quartz (cronTrigger和simpleTrigger實現方法)


Spring整合Quartz (cronTrigger和simpleTrigger實現方法)


之前有記錄過一次springboot整合Quartz的文章,由於偶爾一次自己使用spring需要整合Quartz時有遇到一些異常,導致定時job無法正常執行,所以在此也記錄一下spring整合quartz的兩種實現方式。SpringBoot整合Quartz

應用框架版本

Spring 5.1.4.RELEASE
Quartz 2.3.0
Maven 3.5.0

Demo目錄結構

項目結構

web.xml配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

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

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Spring.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="taskServiceBean" class="com.test.quartz.service.TaskService"></bean>


    <!--************************************************************指定時間間隔定時執行***********************************************************-->


    <!-- 要執行任務的任務類。 -->
    <bean id="timerTaskQuartz" class="com.test.quartz.timer.TimerTask">
        <property name="taskService" ref="taskServiceBean"></property>
    </bean>

    <!-- 將需要執行的定時任務注入JOB中。 -->
    <bean id="timerTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="timerTaskQuartz"></property>
        <!-- 任務類中需要執行的方法 -->
        <property name="targetMethod" value="run"></property>
        <!-- 上一次未執行完成的,要等待有再執行。 -->
        <property name="concurrent" value="true"></property>
    </bean>

    <!-- 基本的定時器,會綁定具體的任務。 -->
    <bean id="timerTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="timerTaskJob"></property>
        <!--延時啟動-->
        <property name="startDelay" value="3000"></property>
        <!--執行頻率-->
        <property name="repeatInterval" value="3000"></property>
    </bean>

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

    <!--************************************************************使用cron表達式在指定時間執行***********************************************************-->

    <bean name="timeTask2" class="com.test.quartz.timer.TimeTask2">
        <property name="taskService" ref="taskServiceBean"></property>
    </bean>
    <bean id="timerTaskJob2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 執行的類 -->
        <property name="targetObject">
            <ref bean="timeTask2" />
        </property>
        <!-- 類中的方法 -->
        <property name="targetMethod">
            <value>run</value>
        </property>
    </bean>
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="timerTaskJob2" />
        </property>
        <!-- 每一秒鍾執行一次 -->
        <property name="cronExpression">
            <value>0/1 * * * * ?</value>
        </property>
    </bean>

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

</beans>

pom.xml依賴

在使用quartz版本時需要注意與spring的版本對應。
spring 3.0版本內置的是Quartz<2.0的版本,所以在使用Spring 3.0版本以上時,需要使用Quartz 2.0以上的版本

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>spring-quartz</groupId>
  <artifactId>com.test.quartz</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>com.test.quartz Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.version>5.1.4.RELEASE</spring.version>
    <quartz.version>2.3.0</quartz.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
      <exclusions>
        <!-- Exclude Commons Logging in favor of SLF4j -->
        <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <type>jar</type>
      <scope>test</scope>
    </dependency>

      <dependency>
          <groupId>org.quartz-scheduler</groupId>
          <artifactId>quartz</artifactId>
          <version>${quartz.version}</version>
      </dependency>
  </dependencies>

  <build>
    <finalName>com.test.quartz</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>


TimerTask.java

package com.test.quartz.timer;

import com.test.quartz.service.TaskService;

/**
 * @author yd
 * @date 2019-06-11 16:08
 * @describe 定時job1
 */
public class TimerTask {

    private TaskService taskService;

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    public void run(){
        String process = taskService.process(this.getClass().getSimpleName());
        System.out.println(process);
    }

}

TimeTask2.java

package com.test.quartz.timer;

import com.test.quartz.service.TaskService;

/**
 * @author yd
 * @date 2019-06-11 16:28
 * @describe 定時job2
 */
public class TimeTask2 {

    private TaskService taskService;

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    public void run(){
        String process = taskService.process(this.getClass().getSimpleName());
        System.out.println(process);
    }
}

service

在對quartz集成的同時,在這里也引入了service層的注入,demo中只是一個簡單的樣例,可以根據自己的實際需求去對service層進行編輯。

TaskService.java

package com.test.quartz.service;

import org.springframework.stereotype.Service;

/**
 * @author yd
 * @date 2019-06-11 16:11
 * @describe 文件描述
 */

@Service
public class TaskService {

    public String process(String taskname){
        return this.getClass().getSimpleName()+"在處理任務:"+taskname;
    }
}

執行結果

執行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM