Spring @Scheduled 使用注释时执行任务两次
我已经使用 Spring @Scheduled 注释创建了任务,但由于某种原因它执行任务两次。我的Spring框架版本是3.0.2。
@Service
public class ReportService {
@Scheduled(fixedDelay=1000 * 60 * 60* 24)
@Transactional
public void dailyReportTask()
{
... code here ...
}
}
这是我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:scheduler id="taskScheduler" />
<task:executor id="taskExecutor" pool-size="1" />
<task:annotation-driven executor="taskExecutor"
scheduler="taskScheduler" />
</beans>
I have made task using Spring @Scheduled annotation, but for some reason it is executing task twice. My Spring Framework version is 3.0.2.
@Service
public class ReportService {
@Scheduled(fixedDelay=1000 * 60 * 60* 24)
@Transactional
public void dailyReportTask()
{
... code here ...
}
}
Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:scheduler id="taskScheduler" />
<task:executor id="taskExecutor" pool-size="1" />
<task:annotation-driven executor="taskExecutor"
scheduler="taskScheduler" />
</beans>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是因为上下文侦听器而发生的,
只需从
web.xml
中删除它就应该可以工作。
it is happening because of context listener
Just remove
from
web.xml
it should work.我也遇到了同样的问题,最终发现问题是由于在
root context
以及servlet context
中创建 bean 造成的。因此,要解决此问题,您需要将 bean 的创建分离到适当的上下文中。
这个答案很好地解释了如何做到这一点,并且解决了我的问题。
I had this same problem, and I eventually found out that the problem was occurring as a result of the beans being created in the
root context
as well as theservlet context
.So, to fix this, you need to separate the creation of the beans into the appropriate contexts.
This answer explains really well how to that and was what fixed my problem.
根据这篇文章: http://www.vodori.com/blog/spring3scheduler.html
报告了另一个影响版本的错误:3.0.2
https://jira.springsource.org/browse/SPR-7216
应该修复版本:3.0.3。
According to this post: http://www.vodori.com/blog/spring3scheduler.html
There has been another bug reported which affects Version/s: 3.0.2
https://jira.springsource.org/browse/SPR-7216
Which should be fixed in Version/s: 3.0.3.
我最近刚刚遇到这个问题,这是由于我的应用程序通过eclipse在Tomcat中部署了两次而引起的。问题是我已经在 Eclipse 中重命名了我的应用程序,但“org.eclipse.wst.common.component”.settings 文件中指定的“wb-module deploy-name”仍然具有旧名称。
在 tomcat 管理器中,我可以看到有 2 个应用程序以不同的名称运行。
I just had this problem recently and it was caused by my app being deployed twice in Tomcat by eclipse. The problem was that I had renamed my application in eclipse but the "wb-module deploy-name" specified in the "org.eclipse.wst.common.component" .settings file still had the old name.
In the tomcat manager, I could see that I had 2 apps running with different names.
你实际上在哪里运行它?你的电脑?单服务器? 2 个负载平衡的应用程序服务器?
它可能在 (a) 你的电脑和 (b) 你的服务器上运行,所以它看起来就像运行了两次,如果你明白我的意思:它正确运行一次,只是在两个不同的位置。
Where are you actually running it? Your PC? Single server? 2 x load-balanced app servers?
Could be it's running on (a) your PC and (b) your server, so it just looks like it's running twice, if you see what I mean: it's correctly running once, just on two distinct locations.
检查配置文件中是否有任何手动调度程序配置(通过 Java/XML)。我遇到了同样的问题,我发现我的配置加载了我的调度程序类两次:
在Java中:
在XML applicationContext.xml中:
在我的调度程序类中,我有@Component注释,它被组件扫描捕获并加载了一个第二次导致 @scheduler 方法被执行两次。
我删除了 Java 配置,现在运行良好!
Check if you have any manual scheduler config in your configuration files (through Java/XML). I'ved the same problem, and I discover that my config was loading my scheduler class twice:
In Java:
In XML applicationContext.xml:
And in my scheduler class, I had @Component annotation thats was catch by the component scan and loaded a second time causing the @scheduler methods being executed twice.
I removed the Java config and then is working well now!
要解决
@Scheduled
方法的两次工作问题,只需从 web.xml 中删除ContextLoaderListener
(如果您使用基于 web.xml 的应用程序):或者如果您使用基于
WebApplicationInitializer
的应用程序,只需删除添加ContextLoaderListener的字符串:To solve twice-working of
@Scheduled
method just deleteContextLoaderListener
from you web.xml (if you use web.xml-based application):Or if you use
WebApplicationInitializer
-based application just delete a string that adds ContextLoaderListener:在您的 bean 上使用 @Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
Use @Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE) on your bean
我建议的一种解决方案是像这样进行组件扫描
- 在应用程序上下文中
在 yourservlet-servlet.xml 中
我这样只有在加载 web.xml 时才会加载 servlet
可以对任务执行类似的操作
One solution I would suggest is to do component scat like this
-In application context
In yourservlet-servlet.xml
I this way the servlet is only loaded if the web.xml is loaded
Similar can be done for task