zoukankan      html  css  js  c++  java
  • Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)

      在spring 中的新引入的task 命名空间。可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式。

      

    第一步:

            在Spring的相关配置文件中(applicationContext.xml或者是{project_name}_servelt.xml或者是独立的配置文件如XXX_quartz.xml)中配置并开启Spring Schedule Task.注意其中高亮的部分是必须的。

     1 <?xml version="1.0" encoding="UTF-8"?> 
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     4        xmlns:aop="http://www.springframework.org/schema/aop" 
     5        xmlns:tx="http://www.springframework.org/schema/tx" 
     6        xmlns:context="http://www.springframework.org/schema/context" 
     7        xmlns:mvc="http://www.springframework.org/schema/mvc" 
     8        xmlns:p="http://www.springframework.org/schema/p" 
     9        xmlns:task="http://www.springframework.org/schema/task" 
    10        xsi:schemaLocation=" 
    11        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    12        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
    13        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
    14        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    15        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    16        http://www.springframework.org/schema/task 
    17        http://www.springframework.org/schema/task/spring-task-3.0.xsd 
    18        "> 
    19     <mvc:annotation-driven /> 
    20     <context:component-scan base-package="com.mytools.validator.engine" /> 
    21     
    22     <!-- 启动定时器 --> 
    23     <task:annotation-driven/>      
    24 </beans>

    第二步:

            可以在类中的需要定时执行的方法下指定如下Annotation

    1 @Scheduled(cron="0 33/3 * * * ?") //每小时的33分钟开始执行,每3分钟执行1次 
    2 public void start() throws ServletException { 
    3        validate(); 
    4 }

    备注:其实@Scheduled中可以指定如下3种时间表达式:

    (1)fixedRate:每隔多少毫秒执行一次该方法。如:

    1 @Scheduled(fixedRate=2000)  // 每隔2秒执行一次 
    2 public void scheduleMethod(){   
    3     System.out.println("Hello world...");   
    4 }   

    (2)fixedDelay:当一次方法执行完毕之后,延迟多少毫秒再执行该方法。

    (3)cron:详细配置了该方法在什么时候执行。cron值是一个cron表达式。如:

    1 @Scheduled(cron="0 0 0 * * SAT")   
    2 public voidarchiveOldSpittles() {   
    3    // ...   
    4 }

     到指定时间后,任务总是执行2次的解决方案:

    这是因为我们很容易在一个基于Spring的Web工程中启动2个定时线程:

    第一次:web容器启动的时候,读取applicationContext.xml(或者别的Spring核心配置文件)文件时,会加载一次。

    第二次:Spring本身会加载applicationContext.xml(或者别的Spring核心配置文件)一次。

    解决方案:将你的Task的相关配置独立出来并在web.xml中通过context-param加载。而不是通过spring加载。

    1) 独立出Spring-Task,如新命名一个文件名叫cms_quartz.xml

    2)    在web.xml中去加载该文件:

    1 <context-param> 
    2      <param-name>contextConfigLocation</param-name> 
    3      <param-value>/WEB-INF/cms-servlet.xml, classpath:cms-quartz.xml</param-value> 
    4 </context-param>

    注:出自 http://www.tuicool.com/articles/jmU7bq

  • 相关阅读:
    springmvc log4j 配置
    intellij idea maven springmvc 环境搭建
    spring,property not found on type
    intellij idea maven 工程生成可执行的jar
    device eth0 does not seem to be present, delaying initialization
    macos ssh host配置及免密登陆
    centos7 搭建 docker 环境
    通过rest接口获取自增id (twitter snowflake算法)
    微信小程序开发体验
    gitbook 制作 beego 参考手册
  • 原文地址:https://www.cnblogs.com/kzhan/p/5742523.html
Copyright © 2011-2022 走看看