美文网首页
Spring 解决定时任务执行两次的问题

Spring 解决定时任务执行两次的问题

作者: 迷糊_ | 来源:发表于2017-02-15 16:54 被阅读3074次

    第一种情况可能是spring扫描了两次
    比如spring-config\spring-mvc都扫描了改定时任务,则更改spring-mvc配置的扫描路径过滤定时调度

    <context:component-scan base-package="com.test">
            <context:exclude-filter type="annotation" expression="org.springframework.scheduling.annotation.EnableScheduling"/>
        </context:component-scan>
    

    第二种情况可能是tomcat配置导致容器被初始化两次
    由于web项目直接放在tomcat的webapp下,并且通过根路径访问,配置如下
    <Context path="" docBase="..\.." debug="0" reloadable="true"/>

    查找类似问题处理方式:
    方法1:在conf/Catalina/localhost生成test.xml【推荐】

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
        <Context antiResourceLocking="false" privileged="true" useHttpOnly="true" path="/" docBase="..\.." >
      <!--
        Remove the comment markers from around the Valve below to limit access to
        the manager application to clients connecting from localhost
      -->
      <!--
      <Valve className="org.apache.catalina.valves.RemoteAddrValve"
             allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
      -->
    </Context>
    

    方法2:不配置根路径访问
    方法3:将路径已出webapp目录,在docBase中指定路径(可以是相对于webapp的路径,或者绝对路径)
    方法4:替换ROOT目录

    相关文章

      网友评论

          本文标题:Spring 解决定时任务执行两次的问题

          本文链接:https://www.haomeiwen.com/subject/vzvqwttx.html