Spring+quartz 實現定時任務

小魏的馬仔發表於2017-04-18
  • 確保jar包已經載入

本文使用maven方式載入相關jar包,pom.xml檔案中配置項如下:

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1</version>
        </dependency>
  • 在web.xml檔案中配置上下文
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-core.xml
        ,
        classpath:spring-hibernate.xml
        ,
        classpath:spring-quartz.xml 
        </param-value>
    </context-param>
  • 在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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <bean id="attRecord" class="com.ytang.docinf.tool.Task" />

   <bean id="jobtaskAttRecord"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 呼叫的類 -->
        <property name="targetObject">
            <ref bean="attRecord" />
        </property>
        <!-- 呼叫類中的方法 -->
        <property name="targetMethod">
            <value>test</value>
        </property>
    </bean>

    <bean id="doTimeAttRecord" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- see the example of method invoking job above -->
        <property name="jobDetail" ref="jobtaskAttRecord" />

        <property name="cronExpression">
            <!--每隔五秒執行一次 --> 
            <value>0/5 * * * * ?</value>

        </property>
    </bean>

    <bean id="startQuartz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="doTimeAttRecord" />
            </list>
        </property>
    </bean>

</beans>
  • 在class類中,實現定時任務方法
package com.ytang.docinf.tool;

/*執行定時任務
 * kevin
 * 2017-04-18*/

public class Task{  

    public void test(){  
        System.out.println("定時任務測試");  

    }  

}

相關文章