Spring @Scheduled在使用注释时执行任务两次

new*_*bie 7 spring scheduled-tasks

我使用Spring @Scheduled注释创建了任务,但由于某种原因,它执行任务两次.我的Spring Framework版本是3.0.2.

@Service
public class ReportService {

    @Scheduled(fixedDelay=1000 * 60 * 60* 24)
    @Transactional
    public void dailyReportTask()
    {
        ... code here ...
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

小智 6

它是由于上下文监听器而发生的

只需删除

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

web.xml它应该工作.


Bri*_*ian 0

你实际上在哪里运行它?你的电脑?单服务器?2 个负载平衡的应用程序服务器?

它可能在 (a) 你的电脑和 (b) 你的服务器上运行,所以它看起来就像运行了两次,如果你明白我的意思:它正确运行一次,只是在两个不同的位置。