1

有一个非常标准的 Spring Boot (1.3.5) 应用程序。

启用调度@EnableScheduling(在主应用程序入口点和带@Configuration注释的类上尝试过。

使用方法创建了一个简单的类@Scheduled(简单的 fixedDelay 计划)。

计划任务执行两次(总是)。

从我到目前为止收集的信息来看,这可能是因为正在加载两个上下文,因此两次拿起我的豆子。好的。那么我该如何修复/防止这种双重执行,因为所有配置基本上都是隐藏的 Spring Boot 魔法?

框架版本:

  • 春季启动 1.3.5
  • Spring Cloud Brixton SR1

主要应用:

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

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

我的任务类(从 HookCreateRequest 列表中提取application.yml- 我认为目前不相关,但如果需要,可以提供):

@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;
    }

}

xml 配置为零。不确定还有什么相关的?

编辑 2016/07/04

我已经修改为在运行时输出计划的实例(我怀疑正在创建两个不同的实例)。但是,日志似乎表明它是任务对象的 SAME 实例。日志: 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 所以看起来它是同一个任务实例(@705a651b)。现在为什么会以甜蜜的名义执行两次?

编辑 2016/07/05

我向@PostConstruct带有预定方法的类添加了一个方法,只输入了一些日志输出。通过这样做,我可以验证该@PostConstruct方法是否被调用了两次——这似乎证实了 bean 被拾取了两次,这大概是意味着它被馈送到调度程序两次。那么如何预防呢?

4

1 回答 1

0

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

于 2016-07-02T19:39:17.013 回答