2010-05-31 156 views
6

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

@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> 
+0

你可以粘贴一些相关的应用程序上下文或其他这样的配置吗?有没有可能是另一个调用这个的计划任务? – jasonmp85 2010-05-31 10:37:40

回答

0

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

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

+0

只在我的开发机器上运行...它只有一个运行...我从eclipse运行它... – newbie 2010-05-31 10:52:36

+0

它是否运行在您的机器上的应用程序服务器上,例如:您从Eclipse部署的Tomcat,JBoss,Glassfish?或者它是作为一个独立的应用程序运行? Eclipse可能让你运行第二个应用程序,而第一个应用程序仍然在运行。 – Brian 2010-05-31 14:04:30

+0

其Eclipse嵌入式tomcat 6服务器。而且我只有一个实例在运行,我从任务管理器中检查过它。 – newbie 2010-06-01 13:42:08

-1

一个解决方案,我建议是做组件SCAT这样

- 在应用程序上下文

<context:component-scan base-package="com.abc.cde.dao" /> 

在yourservlet-servlet.xml中

<!-- package that had all the @Controller classes --> 

我这样,如果在web.xml加载 类似这个servlet只能够装载任务完成

5

那是因为上下文侦听的发生

刚刚从删除

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

web.xml它应该工作。

+1

为什么它工作两次到Spring Scheduler?我也删除上下文监听器,它没有工作两次。但我想知道原因。 – 2016-09-04 06:06:40

+0

从web.xml中删除我的ContextLoaderListener是否安全?使这项工作 – shareef 2017-07-01 07:30:37

1

我刚刚有这个问题,这是由于我的应用程序在eclipse中部署了两次Tomcat。问题在于我在eclipse中重命名了我的应用程序,但在“org.eclipse.wst.common.component”.settings文件中指定的“wb-module deploy-name”仍旧具有旧名称。

在tomcat管理器中,我可以看到我有两个以不同名称运行的应用程序。

+0

有相同的问题,因为我已经将符号链接到我的应用程序文件夹,并且都被部署。 – Zipper 2014-08-05 23:22:46

2

我有这个相同的问题,我最终发现问题是由于在root context以及servlet context中创建的bean而发生的。

因此,要解决这个问题,您需要将bean的创建分离到适当的上下文中。

This answer很好的解释了如何解决问题。

0

检查您的配置文件(通过Java/XML)是否有任何手动调度程序配置。 I'ved同样的问题,我发现,我的配置是我的加载类调度两次:

在Java:

package com.mywork.br.myschuedulerpackage; 
{...} 
@Configuration 
@EnableScheduling 
public class SchedulerConfiguration { 

    @Bean 
    public CTXDataImporterScheduler ctxDataImporterScheduler() { 
     return new CTXDataImporterScheduler(); 
    } 
} 

在XML中的applicationContext.xml:

<context:component-scan base-package="com.mywork.br.myschuedulerpackage" /> 

在我调度程序类,我有@Component注释这是由组件扫描捕获和第二次加载导致@scheduler方法执行两次。 我删除了Java配置,然后现在运行良好!

0

为了解决@Scheduled方法的两次工作从您刚删除ContextLoaderListener的web.xml(如果你使用的web.xml的应用程序):如果你使用基于WebApplicationInitializer

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

或者应用程序只是删除路线,增加的ContextLoaderListener:

package com.dropbox.shortener.config; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.filter.CharacterEncodingFilter; 
import org.springframework.web.servlet.DispatcherServlet; 
import javax.servlet.FilterRegistration; 
import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration; 

public class DropboxShortenerWebApplicationInitializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext container) throws ServletException { 
     AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
     rootContext.register(AppConfig.class); 

     // (!) Delete the next string 
     // container.addListener(new ContextLoaderListener(rootContext)); 

     AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); 
     dispatcherContext.register(WebConfig.class); 

     ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/"); 

     setupCharEncodingFilter(container); 
    } 

    private void setupCharEncodingFilter(ServletContext container) { 
     container.setInitParameter("defaultHtmlEscape", "true"); 

     FilterRegistration charEncodingFilterReg = container.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class); 
     charEncodingFilterReg.setInitParameter("encoding", "UTF-8"); 
     charEncodingFilterReg.setInitParameter("forceEncoding", "true"); 
     charEncodingFilterReg.addMappingForUrlPatterns(null, false, "/*"); 
    } 
} 
0

使用@Scope(值= ConfigurableBeanFactory.SCOPE_ PROTOTYPE)

0
Disabling below will work. 
<!-- <listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> --> 
相关问题