1. 程式人生 > >quartz詳解5-quartz與spring結合

quartz詳解5-quartz與spring結合

慕課網_《Java定時任務排程工具詳解之Quartz篇》學習總結

1. spring中quartz依賴

quartz需要事務tx依賴,以及support依賴。
以及本身的一個quartz包。

<!-- ======== quartz 額外依賴 ========== -->
<!--quartz需要tx和support-->
<dependency>
    <groupId
>
org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency
>
<!--quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency>

springmvc結合時,springmvc依賴正常引入。

配置參考
SpringMVC最小依賴+Junit單元測試+log解析

2. quartz配置作業的兩種方式及配置檔案

  • MethodInvokingJobDetailFactoryBean
  • JobDetailFactoryBean(更加靈活)

先看有哪些配置檔案:

web.xml
   |
   |-(載入)spring-context.xml
   |
  (自動尋找資源)log4j的配置檔案(幾種固定名稱)

xml配置中配置jobbean,通常是spring-context.xml中配。這裡單獨提出來。
這裡寫圖片描述
spring-context.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       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.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <context:component-scan base-package="cn.whbing.pro.web"/>
    <mvc:annotation-driven enable-matrix-variables="true" />
    <task:annotation-driven /><!--定時任務註解-->

    <!--spring檔案引入-->
    <import resource="/spring/spring-config-quartz.xml"/>
</beans>

spring-config-quartz.xml中僅僅配置bean

<?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">


</beans>

/ 1 / 新建bean類(job)並允許掃描

package cn.whbing.pro.quartz.bean;
import org.springframework.stereotype.Component;

@Component
public class MyBean {
    public void printMessage(){
        System.out.println("My bean execute!"
                +DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
    }
}

/ 2 / 在 spring-config-quartz.xml 中配置 bean

<!--1.通過方式1建立bean-->
    <bean id="mySimpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean"/>
        <property name="targetMethod" value="printMessage"/>
    </bean>
    <!--2.建立觸發器-->
    <bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="mySimpleJobDetail"/>
        <property name="startDelay" value="1000"/>
        <property name="repeatInterval" value="2000"/>
    </bean>
    <!--3.scheduler -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="mySimpleJobDetail"/>
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="mySimpleTrigger"/>
            </list>
        </property>
    </bean>

/ 3 / 直接在tomcat中執行即可

是web程式,結果會在控制檯中打印出來
結果:

My bean execute!2018-08-03 15:23:52
My bean execute!2018-08-03 15:23:54
My bean execute!2018-08-03 15:23:56

改進:使用JobDetailFactoryBean建立更復雜的jobdetail(可以傳入jobDataMap),並使用CronTrigger觸發器。