SpringMVC中定時任務配置


 

在項目中使用定時任務是常有的事,比如每天定時進行數據同步或者備份等等。

以前在從事C語言開發的時候,定時任務都是通過寫個shell腳本,然后添加到linux定時任務中進行調度的。

 

現在使用SpringMVC之后,一起都變得簡單了o(∩_∩)o 

 

有兩種配置方式,我都分別講講,但是看了后你肯定只會選擇后面那種,沒錯! 我也是用后面那種方式

 

第一種配置方式:這個比較復雜,配置的地方有點多,稍不留意就不成功,具體看代碼了

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    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.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 每隔一個小時重新獲取一次token -->
    <!-- 1.要調用的工作類 -->
    <bean id="refreshAccessTokenJob" class="com.xiao.weixin.quartz.RefreshAccessToken"/>
    
    <!-- 2.定義調用對象和調用對象的方法 -->
    <bean id="refreshAccessTokenTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 調用的類 -->
        <property name="targetObject">
            <ref bean="refreshAccessTokenJob"/>
        </property>
        <!-- 調用類中的方法 -->
        <property name="targetMethod">
            <value>work</value>
        </property>
    </bean>
    
    <!-- 3.定義觸發時間 -->
    <bean id="refreshAccessToken" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="refreshAccessTokenTask"/>
        </property>
        <!-- cron表達式 -->
        <property name="cronExpression">
            <value>0 0 */2 * * ?</value>
        </property>
    </bean>
    
    
    <!-- 4.總管理類 如果將lazy-init='false'那么容器啟動就會執行調度程序  -->
    <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="refreshAccessToken"/>
            </list>
        </property>
    </bean>
</beans>

 

第二種配置方式:強烈推薦的這種

<?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:tx="http://www.springframework.org/schema/tx" 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/task http://www.springframework.org/schema/task/spring-task.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <task:scheduled-tasks>
        <task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />
    </task:scheduled-tasks>
    
    <bean id="quartzTestBean" class="xiaochangwei.zicp.net.service.QuartzTestServiceImpl"/>
</beans>

這里面很簡單,直接調用service接口實現類中的方法就可以了,

 maven配置中加上

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

 

比如我的實現類是這樣的:

package xiaochangwei.zicp.net.service;

import java.util.Date;

import org.springframework.stereotype.Service;

import xiaochangwei.zicp.net.common.tools.DateUtil;

@Service
public class QuartzTestServiceImpl implements QuartzTestService {

    public void quartzJobTestMethod() {
        System.out.println("定時任務執行:" + DateUtil.getFormatDate(new Date()));
    }

}

每隔五秒打印一個當前時間

執行結果如下:

 

 

定外配置任務多久執行也很簡單:

<task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />

上面這個表示五秒鍾執行一次

 

總共有五個*,從前往后表示秒,分,時。。。。。。。

 

多少秒執行一次說了,說多少分執行一次

cron="* */2 * * * ?"  對么?   

這樣不對哈,要將前面的星改為0 cron="0 */2 * * * ?" 表示兩分執行一次

同理 如果兩小時執行一次,則前面兩個都是0 哦

具體規則請百度cron吧 so easy !


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM