[Spring] - Quartz定時任務

使用Spring 3.2.0 + Quartz 2.2.1 作定時任務方法:html

 

一、Maven的pom.xml:java

<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>com.my.quartz</groupId>
  <artifactId>testquartz1</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>

  <name>testquartz1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>3.2.0.RELEASE</springframework.version>
    <quartz.version>2.2.1</quartz.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>${quartz.version}</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>${quartz.version}</version>
    </dependency>
  </dependencies>
</project>

 

二、AppJob類:spring

package com.my.quartz.testquartz1;

public class AppJob {
    
    public void execute() {
        long ms = System.currentTimeMillis();  
        System.out.println(ms);
    }

}

 

三、Spring 配置文件:apache

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="appJob" class="com.my.quartz.testquartz1.AppJob"/>
    
    <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="appJob"/>
        </property>
        <property name="targetMethod"><!-- 要執行的方法名稱 -->
            <value>execute</value>
        </property>
    </bean>
    
    <!-- ======================== 調度觸發器 ======================== -->
    <bean id="triggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="SpringQtzJobMethod"></property>
        <property name="cronExpression" value="0/1 * * * * ?"></property>
    </bean>
    
    <!-- ======================== 調度工廠 ======================== -->
    <bean id="schedulerBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
        <property name="triggers">
            <list>
                <ref bean="triggerBean"/>
            </list>
        </property>
    </bean>
    
</beans>

 

四、main方法:spring-mvc

package com.my.quartz.testquartz1;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    @SuppressWarnings("resource")
    public static void main( String[] args ) throws InterruptedException, SchedulerException
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        System.out.println("Job-End");
        //Scheduler scheduler = (Scheduler) context.getBean("schedulerBean");
        //scheduler.shutdown();
    }
}

 

運行結果:mvc


 

 

關於cronExpression表達式:app


Cron 表達式包括如下 7 個字段:maven

  • 小時
  • 月內日期
  • 周內日期
  • 年(可選字段)

 

Cron 觸發器利用一系列特殊字符,以下所示:ui

  • 反斜線(/)字符表示增量值。例如,在秒字段中「5/15」表明從第 5 秒開始,每 15 秒一次。
  • 問號(?)字符和字母 L 字符只有在月內日期和周內日期字段中可用。問號表示這個字段不包含具體值。因此,若是指定月內日期,能夠在周內日期字段中插入「?」,表示周內日期值可有可無。字母 L 字符是 last 的縮寫。放在月內日期字段中,表示安排在當月最後一天執行。在周內日期字段中,若是「L」單獨存在,就等於「7」,不然表明當月內周內日期的最後一個實例。因此「0L」表示安排在當月的最後一個星期日執行。
  • 在月內日期字段中的字母(W)字符把執行安排在最靠近指定值的工做日。把「1W」放在月內日期字段中,表示把執行安排在當月的第一個工做日內。
  • 井號(#)字符爲給定月份指定具體的工做日實例。把「MON#2」放在周內日期字段中,表示把任務安排在當月的第二個星期一。
  • 星號(*)字符是通配字符,表示該字段能夠接受任何可能的值。


字段 容許值 容許的特殊字符url

  • 秒 0-59 , - * /
  • 分 0-59 , - * /
  • 小時 0-23 , - * /
  • 日期 1-31 , - * ? / L W C
  • 月份 1-12 或者 JAN-DEC , - * /
  • 星期 1-7 或者 SUN-SAT , - * ? / L C #
  • 年(可選) 留空, 1970-2099 , - * /


表達式例子:

"0 0 12 * * ?" 天天中午12點觸發
"0 15 10 ? * *" 天天上午10:15觸發
"0 15 10 * * ?" 天天上午10:15觸發
"0 15 10 * * ? *" 天天上午10:15觸發
"0 15 10 * * ? 2005" 2005年的天天上午10:15觸發
"0 * 14 * * ?" 在天天下午2點到下午2:59期間的每1分鐘觸發
"0 0/5 14 * * ?" 在天天下午2點到下午2:55期間的每5分鐘觸發
"0 0/5 14,18 * * ?" 在天天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發
"0 0-5 14 * * ?" 在天天下午2點到下午2:05期間的每1分鐘觸發
"0 10,44 14 ? 3 WED" 每一年三月的星期三的下午2:10和2:44觸發
"0 15 10 ? * MON-FRI" 週一至週五的上午10:15觸發
"0 15 10 15 * ?" 每個月15日上午10:15觸發
"0 15 10 L * ?" 每個月最後一日的上午10:15觸發
"0 15 10 ? * 6L" 每個月的最後一個星期五上午10:15觸發
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每個月的最後一個星期五上午10:15觸發
"0 15 10 ? * 6#3" 每個月的第三個星期五上午10:15觸發
天天早上6點

0 6 * * *

每兩個小時

0 */2 * * *
晚上11點到早上8點之間每兩個小時,早上八點

0 23-7/2,8 * * *

每月的4號和每一個禮拜的禮拜一到禮拜三的早上11點

0 11 4 * 1-3
1月1日早上4點

0 4 1 1 *

 

更多知識:
http://www.ibm.com/developerworks/cn/java/j-quartz/index.html
http://www.javaeye.com/topic/117244

相關文章
相關標籤/搜索