Java Spring @Scheduled任务执行两次 [英] Java Spring @Scheduled tasks executing twice

查看:580
本文介绍了Java Spring @Scheduled任务执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个简单的测试方法,设置为每5秒运行一次,但是看看System.out,你可以看到它似乎在做一些奇怪的事情。

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.

@Scheduled(cron="*/5 * * * * ?")
public void testScheduledMethod() {
     System.out.println(new Date()+" > Running testScheduledMethod...");
}

输出:

Wed Jan 09 16:49:15 GMT 2013 > Running testScheduledMethod...
Wed Jan 09 16:49:15 GMT 2013 > Running testScheduledMethod...
Wed Jan 09 16:49:20 GMT 2013 > Running testScheduledMethod...
Wed Jan 09 16:49:20 GMT 2013 > Running testScheduledMethod...
Wed Jan 09 16:49:25 GMT 2013 > Running testScheduledMethod...
Wed Jan 09 16:49:25 GMT 2013 > Running testScheduledMethod...
Wed Jan 09 16:49:30 GMT 2013 > Running testScheduledMethod...
Wed Jan 09 16:49:30 GMT 2013 > Running testScheduledMethod...

为什么每个运行TWICE (显示)时间?

Why is it running TWICE (appear) each time?

推荐答案

如果查看文档,可以明确指出这种现象。

If you look at the documentation, there is a note that explicitly calls out this phenomenon.

该笔记位于 25.5.1在此链接,并显示:


确保您不是在运行时初始化同一@Scheduled注释类的多个实例,除非您确实要为每个此类实例安排回调。与此相关,请确保不对使用@Scheduled注释的bean类使用@Configurable,并将其注册为带有容器的常规Spring bean:否则将获得双初始化,一次通过容器,一次通过@Configurable方面,每个@Scheduled方法的结果被调用两次。

Make sure that you are not initializing multiple instances of the same @Scheduled annotation class at runtime, unless you do want to schedule callbacks to each such instance. Related to this, make sure that you do not use @Configurable on bean classes which are annotated with @Scheduled and registered as regular Spring beans with the container: You would get double initialization otherwise, once through the container and once through the @Configurable aspect, with the consequence of each @Scheduled method being invoked twice.

我知道这只是建议,但我做了不要以为我们有足够的信息来进一步诊断问题。

I understand that this is merely suggestion at this point, but I do not think we have enough information to diagnose the issue further.

这篇关于Java Spring @Scheduled任务执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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