1. 程式人生 > >spring mvc整合Quartz框架配置多定時任務

spring mvc整合Quartz框架配置多定時任務

一、增加所依賴的JAR包
    1、增加Spring的Maven依賴

 <!--Quartz的Maven依賴 -->
<dependency>
   <groupId>org.quartz-scheduler</groupId>
   <artifactId>quartz</artifactId>
   <version>1.8.4</version>
</dependency>

二、增加spring-mvc.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:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
    
    <!--配置定時器-->
   <bean id="TimerService" class="com.fh.service.system.appuser.TimerService" /> <!--註冊定時器類-->

   <!--定時器1 begin-->
   <bean id="midDownTaskInfo"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject" ref="TimerService" /><!--ref中是新建的TimeService類-->
      <property name="targetMethod" value="timer1" /><!--value是TimeService類中的具體方法名-->
      <!--指定定時器任務類要執行的方法名稱 這裡是timer1 -->
   </bean>
   <bean id="midDownTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!--配置定時器任務的排程器 -->
      <property name="jobDetail" ref="midDownTaskInfo" />
      <!--<property name=""></property>-->
      <property name="cronExpression" value="0/5 * * * * ?" /> <!--每隔5秒執行 -->
   </bean>

   <!--定時器2  begin-->
   <bean id="midStatusTaskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject" ref="TimerService" />
      <property name="targetMethod" value="timer2" />
   </bean>
   <bean id="midStatusTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property name="jobDetail" ref="midStatusTaskInfo" />
      <property name="cronExpression" value="0/10 * * * * ?" /> <!--每隔10秒執行-->
   </bean>
   <!--註冊監聽器--><!--quartz預設的執行緒數是10個,如果我們要修改這個執行緒數需要做一個配置檔案,在配置檔案內修改執行緒-->
   <bean id="registerQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <!--註冊定時器實體 集合 -->
      <property name="triggers">
         <list>
            <ref bean="midDownTrigger"></ref><!--定時器任務的排程器id名 -->
            <ref bean="midStatusTaskTrigger"></ref>
         </list>
      </property>
      <property name="configLocation" value="classpath:quartz.properties"/> <!--quartz配置檔案的配置 -->
   </bean>
</beans>

三、quartz配置檔案

新建quartz.properties檔案

org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=15//配置執行緒數
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore

四、TimerService.java

package com.fh.service.system.appuser;

import com.fh.util.PageData;
import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by hu 2018/11/30.
 */
public class TimerService {

    public void timer1(){//每五秒執行一次
            Date date=new Date();
            System.out.println(date);
    }

    public void timer2(){//每十秒執行一次
      System.out.println("task2");
    }
}