配置:

Maven: pom.xml

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.2.2</version>
</dependency>

 

Quartz是一個任務進度管理器,以下做簡單的配置

Step 1. 编寫工作(Job class):SpringQuartzJobDemo.java ,需要繼承 spring 中的 QuartzJobBean,因為 QuartzJobBean 類實現了 Job 接口

package com.mis.demo;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class SpringQuartzJobDemo extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Business code ..");

    }

}

 

Step 2. Quartz 定時任務,由 Spring 依賴注入配置方法

<?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:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!-- Spring Quartz -->
    <!--集成方式:MethodInvokingJobDetailFactoryBean,並且任務類別,是需要繼承QuartzJobBean-->
    <!--定義jobDetail-->
    <bean name="schedulingJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.mis.demo.SpringQuartzJobDemo"/>
        <property name="durability" value="true" />        
    </bean>
 
    <!--定義Trigger觸發器: cronExpression 表達式的格式:秒 分 時 日 月 周 年(可選)-->
    <bean id="triggerJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="schedulingJob"/>
        <property name="cronExpression" value="*/5 * * * * ?" />
    </bean>
 
        <!--定義核心調度器-->
    <bean id="scheduleController" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true">
        <property name="schedulerName" value="monitorScheduler"/>
        <property name="triggers">
            <list>            
                 <ref bean="triggerJob"/>
            </list>                
        </property>
    </bean>

    
</beans>

 

Step 3. 完成設定之後,只要啟動Spring並讀取定義檔完成後,排程任務就會進行,

撰寫一個簡單的任務啟動類別:SpringQuartzJobExcute.java

package com.mis.demo;
import org.quartz.SchedulerException;
import org.quartz.impl.StdScheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringQuartzJobExcute {

    private static ApplicationContext APP_CONTEXT;
    
    public static void main(String[] args) throws SchedulerException {
        APP_CONTEXT = new FileSystemXmlApplicationContext("/src/main/resources/spring-demo-cfg.xml");
        
        System.out.println("Start - ScheduleController");
 
        getScheduleController().start();

    }

    /**
     * get StdScheduler
     *
     * @return StdScheduler
     */
    public static StdScheduler getScheduleController() {
        StdScheduler bean = null;
        bean = (StdScheduler) APP_CONTEXT.getBean("scheduleController", StdScheduler.class);
        return bean;
    }

}

 

 

Cron表達式的格式:秒 分 時 日 月 周 年(可選)。

字段名                 允許的值                        允許的特殊字符

秒                         0-59                               , – * /

分                         0-59                               , – * /

小時                      0-23                               , – * /

日                         1-31                               , – * ? / L W C

月                         1-12 or JAN-DEC            , – * /

周幾                      1-7 or SUN-SAT             , – * ? / L C #

年 (可選字段)     empty, 1970-2099             , – * /

 “?”字符:表示不確定的值

 “,”字符:指定數個值

“-”字符:指定一個值的範圍

“/”字符:指定一個值的增加幅度。n/m表示從n開始,每次增加m

“L”字符:用在日表示一個月中的最後一天,用在周表示該月最後一個星期X

“W”字符:指定離给定日期最近的工作日(周一到周五)

“#”字符:表示該月第幾個周X。6#3表示該月第3個周五

 

Cron表達式範例:

每隔5秒執行一次:*/5 * * * * ?

每隔1分鐘執行一次:0 */1 * * * ?

每天23點執行一次:0 0 23 * * ?

每天淩晨1點執行一次:0 0 1 * * ?

每月1號淩晨1點執行一次:0 0 1 1 * ?

每月最後一天23點執行一次:0 0 23 L * ?

每周星期天淩晨1點實行一次:0 0 1 ? * L

在26分、29分、33分執行一次:0 26,29,33 * * * ?

每天的0點、13點、18點、21點都執行一次:0 0 0,13,18,21 * * ?

 

參考資料: http://quartz-scheduler.org/documentation

 

參考:

http://wp.mlab.tw/?p=1701

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

http://quartz-scheduler.org/overview/quick-start

 

 

arrow
arrow
    文章標籤
    Spring Quartz Job schedule
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()