• 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;
        }
    }
    
    

    执行结果

    执行结果

  • 相关阅读:
    VUE初始化
    Flask基础
    算法面试题整理
    python基础数据类型整理
    Cookies 和 Session
    Django 第一天
    初入社会八个月总结
    CSS常用选择器
    分享一点漂亮的扁平化网页
    几个漂亮的网页设计
  • 原文地址:https://www.cnblogs.com/staticking/p/11004853.html
Copyright © 2020-2023  润新知