spring 3 定时任务运行3次

发布于 2024-09-17 22:32:03 字数 565 浏览 5 评论 0原文

我有一个非常简单的方法,计划每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅倚清风 2024-09-24 22:32:03

我也有同样的问题。原因之一是Spring 3.0.0中的一个bug。我升级到 3.0.5,重复次数减少到只有两次。

另一个原因是因为我的具有 @Scheduled 方法的类被实例化两次。发生这种情况是因为上下文配置被加载了两次。在 web.xml 中,我将 ContextLoaderListener 和 DispatcherServlet 指向同一个上下文配置文件:

...
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...

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:

...
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...

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.

那小子欠揍 2024-09-24 22:32:03

您将注释与配置混合在一起,我不认为您需要两者

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.

夜清冷一曲。 2024-09-24 22:32:03

您可能多次加载 applicationContext 吗?

may be you load applicationContext multiple times ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文