Spring Boot, Scheduled task, double invocation

做~自己de王妃 提交于 2019-12-08 04:41:17

问题


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?


回答1:


Had the same problem, in my case the reason was in @Scheduled annotation's initialDelay parameter absence - method was called on application start.



来源:https://stackoverflow.com/questions/38163080/spring-boot-scheduled-task-double-invocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!