1. 程式人生 > >定時任務排程之Sping的@Scheduled註解例項詳解

定時任務排程之Sping的@Scheduled註解例項詳解

最近開發了一個簡單的用來監控線上各個產品的任務排程執行率的小系統,則考慮在監聽系統中使用定時任務來持續監控每個產品系統的定時任務執行率。
理一下:
監聽系統構建排程模組–》用來監控–》各個線上產品系統的任務排程模組的執行率(每個產品系統本身又存在自己的任務排程模組)

關於任務排程,最開始想到的就是quartz,想想是不是可以使用spring的註解呢,於是讀了相關文件,理了一個入門小樣例分享給大家。

Step1:建立maven的web工程,resources檔案下放置spring-mvc.xml檔案,在web.xml中配置springMVC分發器:

web.xml檔案:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- character encoding --> <filter> <filter-name>characterEncoding</filter-name
>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value
>
</init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring-mvc --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-mvc.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

Step2:使用@Scheduled需要在spring配置檔案中引入名稱空間:

xmlns:task="http://www.springframework.org/schema/task"

還需要制定xsd的url:

http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task.xsd

同樣的,你需要開啟註解掃描:

<!--開啟task註解掃描 -->
<task:annotation-driven/>

完整的spring-mvc.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:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/util 
            http://www.springframework.org/schema/util/spring-util-3.1.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/task 
            http://www.springframework.org/schema/task/spring-task.xsd">

    <util:properties id="valueSettings" location="classpath:globalConfig.properties" />

    <context:component-scan base-package="org.byron4j.task" />
    <!--開啟task註解掃描 -->
    <task:annotation-driven/>
    <!-- 預設的註解對映的支援 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes" value="application/json;charset=UTF-8" />
                <property name="features">
                    <array>
                        <value>WriteMapNullValue</value>
                        <value>WriteNullStringAsEmpty</value>
                    </array>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 靜態資源對映 -->
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/images/**" location="/images/" />
    <!-- 當上面要訪問的靜態資源不包括在上面的配置中時,則根據此配置來訪問 -->
    <mvc:default-servlet-handler />

    <!--對模型檢視名稱的解析,即在模型檢視名稱新增前後綴 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

建立一個@Scheduled註解的類,需要在掃描包下哦:

package org.byron4j.task;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;



/**
 *  @author     Byron.Y.Y
 *  @optDate    2016年10月25日
 *  
 */

@Service
public class TestTrueJob implements BaseJob{

    private final Logger  logger = LoggerFactory.getLogger(McpTrueJob.class);

    @Scheduled(cron="0/3 * * * * ?") //使用cron表示式,每3秒鐘執行一次
    public void checkTaskRules(){
        logger.info("定時任務雛形=======" + new Date());
    }
}

打包,啟動專案,即可看到效果。