Java Spring @Scheduled tasks executing twice

前端 未结 23 1548
清歌不尽
清歌不尽 2020-11-29 09:07

I have a simple test method here that is set to run every 5 seconds and it does, but looking at the System.out you can see it appears to be doing something odd.



        
相关标签:
23条回答
  • 2020-11-29 09:38

    i am using spring 4.0.3 and i have this problem. i solved it by renaming my beans.

    to:

    <task:annotation-driven executor="taskExecutor"
        scheduler="taskScheduler" />
    <task:executor id="taskExecutor" pool-size="25" />
    <task:scheduler id="taskScheduler" pool-size="25" />
    

    I noticed some INFO logging saying that no bean named taskScheduler found, creating a new instance. So I figured there were two instance of the taskScheduler.

    Let me know if this works for you too :)

    0 讨论(0)
  • 2020-11-29 09:38

    I had the same problem. Spent hours trying to resolve.

    Solution was the application was deploying twice on Tomcat.

    When trying to clean Tomcat it gave an error.

    Checking the server.xml Tomcat file I noticed the same was being deployed twice. There was also an unclosed "host" tag. Not sure which of these fixed it but relieved to have it working properly again.

    0 讨论(0)
  • 2020-11-29 09:40

    I encountered similar problem. It could be because of below reasons.

    1. A bug in spring versions https://jira.spring.io/browse/SPR-10830

    2. Context being loaded twice.

    3. The log4j.xml writing the logs twice. It happened in my case not sure about yours. If you have tried the other options, try this one also.

    0 讨论(0)
  • 2020-11-29 09:42

    According to this post and Spring Jira this is a bug in Spring Framework Scheduler component.

    0 讨论(0)
  • 2020-11-29 09:45

    I had the same problem. I was using annotation based configuration as follows:

    @Configuration
    @EnableScheduling
    public class SchedulerConfig {
    
        @Scheduled(fixedDelay = 60 * 1000)
        public void scheduledJob() { 
            // this method gets called twice... 
        }   
    }
    

    I was also extending AbstractAnnotationConfigDispatcherServletInitializer for initializing the same.

    public class SpringWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { MainConfig.class, SchedulerConfig.class };
    }
    
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] {SpringWebConfig.class};
    }
    
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
    
    @Override
    protected Filter[] getServletFilters() {
        final CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding(CHARACTER_ENCODING);
        encodingFilter.setForceEncoding(true);
        return new Filter[] { encodingFilter };
    }
    

    }

    Removing the SchedulerConfig.class from getRootConfigClasses() method did the trick for me.

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

    I hope this helps.

    0 讨论(0)
  • 2020-11-29 09:45

    In the application.properties, add the following property which tells the Spring Boot Application to not start the Batch Job on Application startup.

    spring.batch.job.enabled=false
    
    0 讨论(0)
提交回复
热议问题