Spring Boot,Scheduled任务,双重调用 [英] Spring Boot, Scheduled task, double invocation

查看:227
本文介绍了Spring Boot,Scheduled任务,双重调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Got a pretty standard Spring Boot (1.3.5) application.

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

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

创建一个带有<$ c的简单类$ c> @Scheduled 方法(简单的fixedDelay计划)。

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

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

Scheduled task executes twice (always).

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

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?

框架版本:


  • 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 - 我认为目前不相关,但如果需要,可以提供):

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

}

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

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

编辑2016/07/04

我已修改为在运行时输出计划实例(我怀疑正在创建两个不同的实例)。但是,日志似乎表明它是任务对象的SAME实例。
日志:

15:01:16.170 DEBUG - scheduled.ScheduleHookRecreation - 计划任务运行:scheduled.ScheduleHookRecreation@705a651b
...任务发生的事情
...第一次运行完成,然后:
15:01:39.050 DEBUG - scheduled.ScheduleHookRecreation - 计划任务运行:scheduled.ScheduleHookRecreation@705a651b

所以看起来它是相同的任务实例( @ 705a651b )。现在为什么以甜蜜的名义会被执行两次?

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?

编辑2016/07/05

我在带有调度方法的类中添加了一个 @PostConstruct 方法,只有一些日志输出。通过这样做,我可以验证 @PostConstruct 方法被调用两次 - 这似乎确认了bean被拾取两次,这可能意味着它被送到调度程序两次。那么如何防止这种情况?

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?

推荐答案

出现同样的问题,在我的情况下原因是在 @计划注释的 initialDelay 参数缺席 - 在应用程序启动时调用方法。

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

这篇关于Spring Boot,Scheduled任务,双重调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆