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

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

直接上代码:

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

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


	<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
	<context:component-scan base-package="cc.wangbang.mall.controller"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Component" />
	</context:component-scan>
	<aop:aspectj-autoproxy />
	<annotation-driven />
	<!-- task任务扫描注解 -->
	<task:annotation-driven/>

</beans:beans>

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

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cc.wangbang.mall.controller.ParentController;

@Component
public class TestTask extends ParentController {
	/**
	 * 每天23点执行一次
	 */
    @Scheduled(cron="0 0 23 * * ?") //每天23点
    public void taskCycle(){
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		System.out.println("当前时间:"+sdf.format(new Date()));
		//TODO 以下是定时任务执行计划
		//执行打款、核销等业务代码
		
    }
}

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

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

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

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

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
   <Context docBase="emall" path="/" reloadable="true" />
   </Host>

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

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

方法一:

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

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

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
   <Context docBase="/usr/local/src/tomcat-emall/webapps/emall" path="/" reloadable="true" />
   </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">

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
   <Context docBase="../webapps/emall" path="/" reloadable="true" />
   </Host>

 

转载于:https://my.oschina.net/yaobg/blog/883846

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值