spring 3 定时任务运行3次
我有一个非常简单的方法,计划每 10 秒运行一次,如下所示:
@Component
public class SimpleTask {
@Scheduled(fixedRate=10000)
public void first() {
System.out.println("Simple Task " + new Date());
}
}
配置:
<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />
我的问题是我的方法每 10 秒被调用 3 次。它应该只被调用一次。我做错了什么? 我将 Spring Source ToolSuite 与 SpringSource tc Server 6 一起使用。
I have a very simple method scheduled to run every 10 seconds like this:
@Component
public class SimpleTask {
@Scheduled(fixedRate=10000)
public void first() {
System.out.println("Simple Task " + new Date());
}
}
Config:
<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />
My problem is that my method is being invoked 3 times every 10 seconds. It should be invoked just once. What am I doing wrong?
I use Spring Source ToolSuite with SpringSource tc Server 6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也有同样的问题。原因之一是Spring 3.0.0中的一个bug。我升级到 3.0.5,重复次数减少到只有两次。
另一个原因是因为我的具有 @Scheduled 方法的类被实例化两次。发生这种情况是因为上下文配置被加载了两次。在 web.xml 中,我将 ContextLoaderListener 和 DispatcherServlet 指向同一个上下文配置文件:
WEB-INF/applicationContext.xml 是 ContextLoaderListener 的默认上下文配置。因此,请确保您的 ContextLoaderListener 和 ServletDispatcher 使用不同的上下文文件。我最终创建了一个 /WEB-INF/spring-servlet.xml ,没有任何 bean 定义,并且它工作完美。
I had this same problem. One of the causes is a bug in Spring 3.0.0. I upgraded to 3.0.5 and the repetition went down to only two.
The other cause was because my class that had the @Scheduled method was getting instantiated twice. This happened because the context config was getting loaded twice. In web.xml I was pointing my ContextLoaderListener and DispatcherServlet at the same context config file:
WEB-INF/applicationContext.xml is the default context config for the ContextLoaderListener. So make sure that your ContextLoaderListener and your ServletDispatcher are using different context files. I ended up creating a /WEB-INF/spring-servlet.xml without any bean definitions and it worked flawlessly.
您将注释与配置混合在一起,我不认为您需要两者
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-namespace
来自文档
注释
确保您没有在运行时初始化同一 @Scheduled 注释类的多个实例,除非您确实希望为每个此类实例安排回调。与此相关,请确保不要在使用 @Scheduled 注释并在容器中注册为常规 Spring beans 的 bean 类上使用 @Configurable:否则,您将获得双重初始化,一次通过容器,一次通过 @Configurable 方面,每个 @Scheduled 方法被调用两次的结果。
you are mixing annotations with configuration and I dont believe you need both
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-namespace
From Documentation
Note
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.
您可能多次加载 applicationContext 吗?
may be you load applicationContext multiple times ?