ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Spring @Scheduled任务运行两次

2019-07-25 03:20:32  阅读:204  来源: 互联网

标签:java spring scheduled-tasks spring-mvc tomcat


我正在创建一个@Scheduled任务,每5秒运行一次.由于其他问题一直存在问题,我的任务是运行两次!

我已经查看了其他问题,并阅读了适用的文档here,但我无法弄清楚问题.

我知道当我启动tomcat服务器时,我的@Scheduled类的两个单独实例正在实例化.我还想到了它们在引用我的日志文件时被实例化的时候.

与此日志行相关联的一个:

INFO: Initializing Spring root WebApplicationContext

另一个有这个日志行:

INFO: Initializing Spring FrameworkServlet ‘servlet’

这是spring配置文件.

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<context:component-scan base-package="web.controllers"/>
<context:component-scan base-package="services"/>
<context:component-scan base-package="dao"/>
<context:component-scan base-package="scheduled"/>
<context:property-placeholder location="/WEB-INF/application.properties"/>

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<task:annotation-driven />

而我的简单java类:

package scheduled;

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

@Service
public class Notifier {

@Scheduled(fixedDelay = 5000)
public void notifyUsersOfBidItems() {
    try {
        System.out.println(this);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
}

另外,我使用的是Spring 4.

编辑:添加web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
<display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring_config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring_config.xml</param-value>
</context-param>

<error-page>
    <error-code>404</error-code>
    <location>/error/notFound</location>
</error-page>

<error-page>
    <error-code>403</error-code>
    <location>/error/notFound</location>
</error-page>


<error-page>
    <location>/error/internal</location>
</error-page>

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

解决方法:

我相信这是由在web.xml中加载两次的相同配置文件引起的

<servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring_config.xml</param-value> <!-- FIRST -->
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring_config.xml</param-value> <!-- SECOND -->
</context-param>

编辑
要解决这个问题:

创建另一个文件servlet-servlet.xml(默认情况下,这将由ServletDispatcher配置获取,因为它与servlet名称匹配)
该文件将包含:

<beans>
    <context:component-scan base-package="web.controllers"/>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
</beans>

修改原始文件(spring-config.xml):

<beans>
    <task:annotation-driven />
    <context:component-scan base-package="services"/>
    <context:component-scan base-package="dao"/>
    <context:component-scan base-package="scheduled"/>
    <context:property-placeholder location="/WEB-INF/application.properties"/>
</beans>

将您的Web xml servlet配置修改为以下内容:

<servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

标签:java,spring,scheduled-tasks,spring-mvc,tomcat
来源: https://codeday.me/bug/20190725/1528586.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有