Spring @Scheduled task runs twice

后端 未结 3 1294
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 09:05

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 ques

相关标签:
3条回答
  • 2020-12-11 09:44

    I also had this problem. I am using Spring 4. I have no xml configuration. Everything is configured with annotations and Java config.

    I have a base configuration and a WebConfiguration. the error was caused by using @ComponentScan in both configurations. 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();
      }
    
    0 讨论(0)
  • 2020-12-11 09:49

    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:

    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>
    

    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>
    

    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>
    
    0 讨论(0)
  • 2020-12-11 09:55

    solution : Quartz + Spring double execution java config

    getServletConfigClasses()-> return 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

    0 讨论(0)
提交回复
热议问题