Spring @Scheduled任务运行两次 [英] Spring @Scheduled task runs twice

查看:126
本文介绍了Spring @Scheduled任务运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 @Scheduled 任务,每5秒运行一次。正如其他问题一样,我的任务是运行两次!

I am creating an @Scheduled task to run every 5 seconds. As has been a problem in other questions, my task is running twice!

我已经查看了其他问题,并阅读了适用的文档这里,但我还没有去过能够找出问题。

I have looked at other questions, and read the applicable documentation here, but I have not been able to figure out the problem.

我知道我的的两个单独的个实例@Scheduled 类正在实例化。我还想到了它们在引用我的日志文件时被实例化的时候。

I know that two seperate instances of my @Scheduled class are getting instantiated when I start my tomcat server. I have also figured out when they are instantiated in reference to my log file.

与此日志行关联的一个:

One associated with this log line :


INFO:初始化Spring root WebApplicationContext

INFO: Initializing Spring root WebApplicationContext

和另一个有这个日志行:

and another with this log line:


INFO:初始化Spring FrameworkServlet'servlet'

INFO: Initializing Spring FrameworkServlet 'servlet'

这是spring配置文件。

Here is the spring config file.

<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类:

And my simple java class:

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.

Also, I am using 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中两次

I believe this is caused by same config file being loaded twice in your 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>

编辑
修复它:

EDIT To fix it:

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

Create another file servlet-servlet.xml (this will be picked up by the ServletDispatcher config by default since it matches the file by servlet name) The file will contain this:

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

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

Modify the original file (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配置修改为以下内容:

Modify your web xml servlet config to following:

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

这篇关于Spring @Scheduled任务运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆