Java Spring @Scheduled tasks executing twice

前端 未结 23 1547
清歌不尽
清歌不尽 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:32

    I had this same problem, and I eventually found out that the problem was occurring as a result of the beans being created in the root context as well as the servlet context.

    So, to fix this, you need to separate the creation of the beans into the appropriate contexts.

    This answer explains really well how to that and was what fixed my problem.

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

    For me, after several unsuccessful attempts and configurations, resolution in a very simple:

    Edit the conf/server.xml file:

    <Host appBase="webapps" deployOnStartup="false" autoDeploy="false" name="localhost" unpackWARs="true">
    
    0 讨论(0)
  • I had the same problem today.

    In my project i was using the scheduler with my spring boot application and in my ScheduledTaks class i was using the @Component annotation. But i made a mistake because the @Component represent a bean for my class and in my Application class i have create another bean for this class with the code:

    public class Application extends SpringBootServletInitializer {
    
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(Application.class);
      }
    
      public static void main(String[] args) throws Exception {
          SpringApplication.run(Application.class, args);
      }
    
      @Bean
      public ScheduledTasks getScheduledTasks() {
        return new ScheduledTasks();
      }
    }
    

    I just remove this annotation and the scheduler works percectly.

    Follow the example of my code ScheduledTasks class:

    public class ScheduledTasks {
      private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
      @Scheduled(cron = "00 14 11  * * *")
      public void reportCurrentTime() {
        log.info("The date is: {} " + dateFormat.format(new Date()) );
      }
    }
    

    And the result:

    2016-10-20 11:13:41.298  INFO 6832 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2016-10-20 11:13:41.302  INFO 6832 --- [           main] br.com.Application                       : Started Application in 9.257 seconds (JVM running for 9.676)
    2016-10-20 11:14:00.002  INFO 6832 --- [pool-2-thread-1] br.com.scheduler.ScheduledTasks          : The date is: {} 11:14:00
    
    0 讨论(0)
  • 2020-11-29 09:34

    I did have the same problem, looking in my code and after try all that is here, I did find that I had a SpringApplicationBuilder twice in 2 diferents class

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

    I had a similar problem, I resolved mine doing this:

    package com.sample.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @Configuration
    @EnableScheduling
    public class JobExecutorConfig {
    }
    

    as configuration for spring boot. And I add this as jobclass:

    package com.sample.jobs;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Job {
    
      private final Logger log = LoggerFactory.getLogger(this.getClass());
    
      @Autowired
      MyOtherClass moc;
    
      @Scheduled(fixedRate = 60000) // every 60 seconds
      public void doJob() {
        log.debug("Job running !!!");
        try {
         moc.doSomething()
        } catch (Exception e) {
          log.error(e.getMessage());
        }
        finally {
    
          log.debug("job Done !!!");
        }
    
      }
    
      // examples of other CRON expressions
      // * "0 0 * * * *" = the top of every hour of every day.
      // * "*/10 * * * * *" = every ten seconds.
      // * "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
      // * "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
      // * "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
      // * "0 0 0 25 12 ?" = every Christmas Day at midnight
    }
    
    0 讨论(0)
  • 2020-11-29 09:38

    I was declaring my class as a "Service" and also using the @Scheduled annotation to declare it as a Scheduler. The scheduler invokes it normally, but as we declared the class as a service. It was initiated twice.

    I removed the Service annotation. And declared that class as a bean in the context XML I have maintained. (custom context XML which is declared in web.xml). This fixed the issue for me. Hope it helps someone.

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