简体   繁体   中英

Spring Boot, Scheduled task, double invocation

Got a pretty standard Spring Boot (1.3.5) application.

Enabled scheduling with @EnableScheduling (tried on main application entry point and a @Configuration annotated class.

Created a simple class with a @Scheduled method (simple fixedDelay schedule).

Scheduled task executes twice (always).

From what I have gathered so far, it is probably because two contexts are being loaded, and thusly picking up my beans twice. Ok. So how do I fix/prevent this double execution, since all the config is basically hidden Spring Boot magic?

Framework versions:

  • Spring Boot 1.3.5
  • Spring Cloud Brixton SR1

Main application:

   @SpringBootApplication
    @EnableDiscoveryClient
    @EnableAsync
    @EnableCircuitBreaker
    public class AlertsApplication {

    public static void main(final String[] args) {
        SpringApplication.run(AlertsApplication.class, args);
    }
}

My task class (HookCreateRequest list is pulled in from application.yml - I do not believe that to be relevant currently, but if required, can be provided):

@ConditionalOnProperty(name = "init.runner", havingValue = "InitRunner")
@ConfigurationProperties(prefix = "webhook")
public class InitRunner /*implements CommandLineRunner*/ {

    private final List<HookCreateRequest> receivers = new ArrayList<>();

    @Autowired
    private WebHookService hookService;

    @Scheduled (fixedRate = 300000)
    public void run() throws Exception {

        getReceivers().stream().forEach(item -> {
            log.debug("Request : {}", item);
            hookService.create(item);
        });

    }

    public List<HookCreateRequest> getReceivers() {
        return receivers;
    }

}

There is zero xml configuration. Not sure what else might be relevant?

EDIT 2016/07/04

I have modified to output the scheduled instance when it runs (I suspected that two different instances were being created). However, the logs seem to indicate it is the SAME instance of the task object. logs: 15:01:16.170 DEBUG - scheduled.ScheduleHookRecreation - Schedule task running: scheduled.ScheduleHookRecreation@705a651b ...task stuff happening ...first run completes, then: 15:01:39.050 DEBUG - scheduled.ScheduleHookRecreation - Schedule task running: scheduled.ScheduleHookRecreation@705a651b So it would seem it is the same task instance ( @705a651b ). Now why would in the name of sweet things would it be executed twice?

EDIT 2016/07/05

I added a @PostConstruct method to the class that carries the scheduled method, with just some logging output in. By doing that I could verify that the @PostConstruct method is being called twice - which seems to confirm that the bean is being picked up twice, which which presumably means it is fed to the scheduler twice. So how to prevent this?

有同样的问题,在我的情况下,原因在于@Scheduled annotation的initialDelay参数缺失 - 在应用程序启动时调用了方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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