springmvc+quartz簡單實現定時調度

1、簡介:Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,它能夠與J2EE與J2SE應用程序相結合也能夠單獨使用。Quartz能夠用來建立簡單或爲運行十個,百個,甚至是好幾萬個Jobs這樣複雜的程序。Jobs能夠作成標準的Java組件或 EJBs。Quartz的最新版本爲Quartz 2.3.0。java

2、由於定時調度,在不少業務上面都會涉及,想要根據本身的規則來生成本身想要的達到的目的。因此利用quartz來時間定時任務的觸發。是很是有必要的。spring

3、springmvc下須要的jar包spring-mvc

   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.2.3</version>
    </dependency>

說明:quartz是基礎包。spring-context-support包是調度設置的依賴包。spring-context能夠不用添加。mvc

4、spring-quartz.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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
       
    <!-- 加入須要執行的類 -->
    <bean id="timingSchedule" class="com.troy.jpa.schedule.TimingSchedule"/>
    <!-- 加入定時執行的方法 -->
    <bean id="timingScheduleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 定時執行的類 -->
        <property name="targetObject" ref="timingSchedule"/>
        <!-- 具體的方法 -->
        <property name="targetMethod" value="execute"/>
    </bean>
    <!-- 調度觸發器,設置本身想要的時間規則 -->
    <bean id="timingScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 加入相關的執行類和方法 -->
        <property name="jobDetail" ref="timingScheduleJobDetail"/>
        <!-- 設置時間規則 (爲了方便測試,設置成一分鐘一次。具體的規則見詳情)-->
        <property name="cronExpression" value="00 * * * * ?"/>    
    </bean>
    <!-- 加入調度工廠 ,設置調度觸發器便可-->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="timingScheduleTrigger"/>
            </list>
        </property>
    </bean>
</beans>

5、須要執行的類和方法spa

package com.troy.jpa.schedule;

import java.util.Date;

public class TimingSchedule {
    
    //定時執行的方法
    public void execute(){
        System.out.println("執行時間"+ new Date());
    }
}

6、執行效果code

執行時間Wed Jul 12 21:01:00 CST 2017
執行時間Wed Jul 12 21:02:00 CST 2017
執行時間Wed Jul 12 21:03:00 CST 2017
執行時間Wed Jul 12 21:04:00 CST 2017

7、時間設置規則xml

1  秒  是  0-59    , - * / 
2  分  是  0-59   , - * / 
3 小時  是  0-23   , - * / 
4  日  是  1-31   , - * ? / L W 
5  月  是  1-12 or JAN-DEC   , - * / 
6  周  是  1-7 or SUN-SAT   , - * ? / L # 
7  年  否  empty 或 1970-2099  , - * / 
相關文章
相關標籤/搜索