定时任务调度之Sping的@Scheduled注解实例详解

2023-05-04 20:19:50 浏览数 (3)

最近开发了一个简单的用来监控线上各个产品的任务调度执行率的小系统,则考虑在监听系统中使用定时任务来持续监控每个产品系统的定时任务执行率。 理一下: 监听系统构建调度模块–》用来监控–》各个线上产品系统的任务调度模块的执行率(每个产品系统本身又存在自己的任务调度模块)

关于任务调度,最开始想到的就是quartz,想想是不是可以使用spring的注解呢,于是读了相关文档,理了一个入门小样例分享给大家。

Step1:创建maven的web工程,resources文件下放置spring-mvc.xml文件,在web.xml中配置springMVC分发器:

web.xml文件:

代码语言:javascript复制
<!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配置文件中引入命名空间:

代码语言:javascript复制
xmlns:task="http://www.springframework.org/schema/task"

还需要制定xsd的url:

代码语言:javascript复制
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task.xsd

同样的,你需要开启注解扫描:

代码语言:javascript复制
<!--开启task注解扫描 -->
<task:annotation-driven/>

完整的spring-mvc.xml文件:

代码语言:javascript复制
<?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注解的类,需要在扫描包下哦:

代码语言:javascript复制
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());
    }
}

打包,启动项目,即可看到效果。

更多参见:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

0 人点赞