spring定时任务执行两次的原因与解决方法

最近遇到一个比较棘手的问题,由于场景需要,每天晚上11点执行一个定时任务,我用的是spring的定时器,具体的定时任务相关配置和代码如下,没啥毛病。。。

直接上代码:

1、项目下的配置文件servlet-context.xml

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"  
  6.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  9.         http://www.springframework.org/schema/beans   
  10.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  11.         http://www.springframework.org/schema/context   
  12.         http://www.springframework.org/schema/context/spring-context.xsd  
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  15.         http://www.springframework.org/schema/aop   
  16.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  17.         http://www.springframework.org/schema/task  
  18.         http://www.springframework.org/schema/task/spring-task-3.2.xsd">  
  19.   
  20.   
  21.     <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->  
  22.     <context:component-scan base-package="cc.wangbang.mall.controller"  
  23.         use-default-filters="false">  
  24.         <context:include-filter type="annotation"  
  25.             expression="org.springframework.stereotype.Controller" />  
  26.         <context:include-filter type="annotation"  
  27.             expression="org.springframework.stereotype.Component" />  
  28.     </context:component-scan>  
  29.     <aop:aspectj-autoproxy />  
  30.     <annotation-driven />  
  31.     <!-- task任务扫描注解 -->  
  32.     <task:annotation-driven/>  
  33.   
  34. </beans:beans>  

2、定时任务执行代码如下

 
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3.   
  4. import org.springframework.scheduling.annotation.Scheduled;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7. import cc.wangbang.mall.controller.ParentController;  
  8.   
  9. @Component  
  10. public class TestTask extends ParentController {  
  11.     /** 
  12.      * 每天23点执行一次 
  13.      */  
  14.     @Scheduled(cron="0 0 23 * * ?") //每天23点  
  15.     public void taskCycle(){  
  16.         SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");  
  17.         System.out.println("当前时间:"+sdf.format(new Date()));  
  18.         //TODO 以下是定时任务执行计划  
  19.         //执行打款、核销等业务代码  
  20.           
  21.     }  
  22. }  

可以肯定的说,以上配置和代码写的都没啥毛病,因为在eclipse本地调试是OK的,定时任务也按照逻辑来每天23点执行一次操作。。。。但是,将代码部署到服务器(linux + tomcat8)上,却诡异的发现定时任务执行了两次操作,这是什么原因造成的?于是去伟大的互联网上寻找真相,大部分网友都说是定时任务实例化了两次,也给出了五花八门的说法,大部分都说配置文件出的问题,矛头都指向servlet-context.xml和web.xml,本人也相信网友说法,对配置文件做了各种修改和优化,最终还是无果。。。直到最近,我发现自己是不是蒙圈了,为啥本地定时任务好好的,放到服务器上就出问题?当时我就想到应该是服务器出问题了,准确的说应该是tomcat配置出问题了,于是我采取了linux上调试的做法,最终找到问题的根源。。。我仔细观察项目的log日志,发现tomcat在启动完成后隔了10秒左右又自行启动了一次,难怪定时任务实例化两次,罪魁祸首是tomcat下的server.xml文件。

有问题的server.xml写法如下:

在server.xml下面找到Host这行代码

  1. <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">  
  2.   
  3.  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
  4.             prefix="localhost_access_log" suffix=".txt"  
  5.             pattern="%h %l %u %t "%r" %s %b" />  
  6. <Context docBase="emall" path="/" reloadable="true" />  
  7. </Host>  

这几行代码问题是什么?是appBase="webroot"和docBase="emall",原因是tomcat加载完appBase="webapps"之后又去加载docBase,因此造成加载两次项目的问题。

找到问题的根源,下面讲解决办法:

方法一:

将 appBase="webapps"改成appBase="webroot",将docBase="emall" 改成项目的绝对路径docBase="/usr/local/src/tomcat-emall/webapps/emall" ,重启tomcat,问题解决!!

  1. <Host name="localhost"  appBase="" unpackWARs="true" autoDeploy="true">  
  2.   
  3.  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
  4.             prefix="localhost_access_log" suffix=".txt"  
  5.             pattern="%h %l %u %t "%r" %s %b" />  
  6. <Context docBase="/usr/local/src/tomcat-emall/webapps/emall" path="/" reloadable="true" />  
  7. </Host>  

方法二:

在/usr/local/src/tomcat-emall路径下新建文件夹webroot,将appBase="webapps"改成appBase="webroot",将docBase="emall" 改成项目的相对路径docBase="../webapps/emall" ,重启tomcat,问题解决!!

<Host name="localhost"  appBase="webroot" unpackWARs="true" autoDeploy="true">  

    1.   
    2.  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
    3.             prefix="localhost_access_log" suffix=".txt"  
    4.             pattern="%h %l %u %t "%r" %s %b" />  
    5. <Context docBase="../webapps/emall" path="/" reloadable="true" />  
    6. </Host>  
原文地址:https://www.cnblogs.com/youqc/p/8420788.html