春季3预定任务运行3次 [英] spring 3 scheduled task running 3 times

查看:68
本文介绍了春季3预定任务运行3次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的方法,计划每10秒运行一次,如下所示:

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

配置:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5"  /> 
<task:scheduler id="myScheduler" pool-size="10"  />

我的问题是我的方法每10秒被调用3次.应该只调用一次.我究竟做错了什么? 我在SpringSource tc Server 6中使用Spring Source ToolSuite.

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.

推荐答案

我遇到了同样的问题.原因之一是Spring 3.0.0中的错误.我升级到3.0.5,重复次数减少到只有两个.

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.

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

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是ContextLoaderListener的默认上下文配置.因此,请确保ContextLoaderListener和ServletDispatcher使用的上下文文件不同.我最终创建了一个/WEB-INF/spring-servlet.xml,没有任何bean定义,并且可以完美地工作.

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.

这篇关于春季3预定任务运行3次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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