简体   繁体   English

Spring @Scheduled任务运行两次

[英]Spring @Scheduled task runs twice

I am creating an @Scheduled task to run every 5 seconds. 我正在创建一个@Scheduled任务,每5秒运行一次。 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. 我查看了其他问题,并在此处阅读了适用的文档,但是我仍然无法解决问题。

I know that two seperate instances of my @Scheduled class are getting instantiated when I start my tomcat server. 我知道启动我的tomcat服务器时,我的@Scheduled类的两个单独实例被实例化。 I have also figured out when they are instantiated in reference to my log file. 我还想出了何时参考我的日志文件实例化它们。

One associated with this log line : 与该日志行关联的一个:

INFO: Initializing Spring root WebApplicationContext INFO:初始化Spring根WebApplicationContext

and another with this log line: 另一个与此日志行:

INFO: Initializing Spring FrameworkServlet 'servlet' INFO:初始化Spring FrameworkServlet'servlet'

Here is the spring config file. 这是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 />

And my simple java class: 我的简单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();
    }

}
}

Also, I am using Spring 4. 另外,我正在使用Spring 4。

EDIT: Adding web.xml 编辑: 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>

I believe this is caused by same config file being loaded twice in your web.xml 我相信这是由于在您的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: 编辑以解决此问题:

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: 创建另一个文件servlet-servlet.xml(默认情况下,它将由ServletDispatcher配置拾取,因为它通过Servlet名称与该文件匹配)。该文件将包含以下内容:

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

Modify the original file (spring-config.xml): 修改原始文件(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>

Modify your web xml servlet config to following: 将您的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>

I also had this problem. 我也有这个问题。 I am using Spring 4. I have no xml configuration. 我正在使用Spring4。我没有xml配置。 Everything is configured with annotations and Java config. 一切都通过注释和Java配置进行配置。

I have a base configuration and a WebConfiguration. 我有一个基本配置和一个WebConfiguration。 the error was caused by using @ComponentScan in both configurations. 该错误是由于在两种配置中均使用@ComponentScan引起的。 I removed component scan from base configuration. 我从基本配置中删除了组件扫描。

@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();
  }

solution : Quartz + Spring double execution java config 解决方案:Quartz + Spring双重执行java config

getServletConfigClasses()-> return null; getServletConfigClasses()->返回null;

    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 示例代码解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM