Spring @Scheduled 任务运行两次

Spring @Scheduled task runs twice

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

我查看了其他问题,并阅读了适用的文档here,但我一直无法找出问题所在。

我知道当我启动我的 tomcat 服务器时,我的 @Scheduled class 的 两个单独的 实例正在被实例化。我还弄清楚了它们何时根据我的日志文件实例化。

与此日志行关联的一个:

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 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.

编辑:Adding 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>

将您的网站 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>

解决方案:Quartz + Spring 双重执行 java 配置

getServletConfigClasses()->return空;

    public static class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};

    }
}

example code solution

我也遇到了这个问题。 我正在使用 Spring 4。我没有 xml 配置。 一切都配置了注释和 Java 配置。

我有一个基本配置和一个 WebConfiguration。 该错误是由在两种配置中使用@ComponentScan 引起的。 我从基本配置中删除了组件扫描。

@EnableWebMvc
@ComponentScan(basePackages = { "com.myservice" })
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
  @Bean
  public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
  }

  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }
}

@Configuration
@EnableScheduling
//@ComponentScan(basePackages = { "com.myservice" })  
@PropertySource("${myservice.properties.location:classpath:myservice.properties}")
public class BaseConfiguration  {


  @Autowired
  Environment environment;

  @Bean
  public static PropertySourcesPlaceholderConfigurer properties() {
    return new PropertySourcesPlaceholderConfigurer();
  }