spring定时任务执行两次的原因与解决方法
来源:互联网 发布:fedora 24 php 编辑:程序博客网 时间:2024/06/03 07:36
最近遇到一个比较棘手的问题,由于场景需要,每天晚上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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/taskhttp://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;@Componentpublic 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 "%r" %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 "%r" %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 "%r" %s %b" /> <Context docBase="../webapps/emall" path="/" reloadable="true" /> </Host>
- spring定时任务执行两次的原因与解决方法
- spring定时任务执行两次的原因
- spring 定时任务执行两次
- spring 定时任务执行两次
- Spring 被初始化两次(Spring-Task定时任务执行两次)分析和解决方法
- spring quartz 定时任务执行两次的问题
- Spring的定时任务@Scheduled,部署后执行加载两次
- spring+quartz定时任务执行两次
- spring 定时任务执行两次解决办法
- spring 定时任务执行两次解决办法
- spring定时任务quartz执行两次
- spring 定时任务执行两次 解决方案
- Spring 定时任务执行两次 解决办法
- spring 定时任务执行两次解决办法
- spring-task定时任务执行两次解决方案
- 定时任务执行两次
- 解决Spring定时计划任务重复执行两次(实例被构造两次)问题的方法
- 解决Spring定时计划任务重复执行两次(实例被构造两次)问题的方法
- Java多线程相关问题
- python 访问器@property的使用方法
- MCIMX6UL-BB应用程序编译
- codeforces 788C The Great Mixing( BFS / dp+bitset优化 )
- 如何限制电脑PC端访问Web页面,只能手机端才能访问
- spring定时任务执行两次的原因与解决方法
- Hibernate与 MyBatis的比较
- bzoj 3730: 震波 (动态点分治)
- Javascript中的apply与call详解
- c++ 实现Student类
- 代码生成k线数据
- equals
- mybatis教程--映射之一对一查询
- Android开发:shape和selector和layer-list的(详细说明)