Announcement Announcement Module
Collapse
No announcement yet.
★I have found a bug of @Scheduled in Spring 3.0 RC1 !★ Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ★I have found a bug of @Scheduled in Spring 3.0 RC1 !★

    I used Spring MVC & Spring in my application, the root container confiuration /WEB-INF/applicationContext.xml is:

    Code:
    ...
    <context:component-scan base-package="net.ilkj.spring">
    <context:exclude-filter type="aspectj"
    expression="net.ilkj.spring.mvc.conntroller.*"/>
    </context:component-scan>
    <task:annotation-driven/>
    ...
    ------------------------------
    A Scheduled method in Class net.ilkj.spring.scheduled.MyTask is:

    Code:
    @Component
    public class MyTask {
    
    	@Scheduled(cron="1 0 0 * * *")
    	public void execute() {
                      System.out.println("******");
    	}
    }
    ------------------------------

    sw-servlet.xml:
    Code:
    <context:component-scan base-package="net.ilkj.spring.mvc.conntroller" />
    <bean
    class="org.springframework.web.servlet.view.Intern alResourceViewResolver"
    prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    <bean
    class="org.springframework.web.servlet.view.Defaul tRequestToViewNameTranslator" />
    ------------------------------

    This Timer-Task run duplicated! I see the sourcecode of Spring 3.0 RC1, a ScheduledAnnotationBeanPostProcessor that register @Scheduled method two times, the 1st is in ApplicationContext.xml - Root Container, the 2nd is in sw-servlet.xml - Spring MVC Container that extends Root Container, so the reason of duplicate run.

    ------------------------------
    How do I resolve the problem?
    Last edited by alkj; Nov 10th, 2009, 03:08 AM.

  • #2
    My guess is that you are scanning for components twice (in both the applicationContexts) and your bean is thus registered twice. Either that or you are loading a xml file twice.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      My guess is that you are scanning for components twice (in both the applicationContexts) and your bean is thus registered twice. Either that or you are loading a xml file twice.
      If I do not use Spring MVC , Timer-Task run only Once! So it is not a xml file loading twice! Because Spring MVC Container extened the Root Container, <task:annotation-driven/> is defined in Root Container , So the Child Container of Spring MVC execute Timer-Task duplicated !

      Comment


      • #4
        The BeanFactoryPostProcessor do not operate on the child contexts.

        You yourself state that if you don't use mvc it executes once. If you do it executes multiple times, which basically means your bean gets registered twice. Post your configuration xmls and web.xml.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          The BeanFactoryPostProcessor do not operate on the child contexts.

          You yourself state that if you don't use mvc it executes once. If you do it executes multiple times, which basically means your bean gets registered twice. Post your configuration xmls and web.xml.

          applicationContext.xml:

          Code:
          <context:component-scan base-package="net.ilkj.spring">
             <context:exclude-filter type="aspectj"
          			expression="net.ilkj.spring.mvc.conntroller.*"/>
          </context:component-scan>
          <task:annotation-driven/>
          ----------------------------------------
          sw-servlet.xml:

          Code:
          <context:component-scan base-package="net.ilkj.spring.mvc.conntroller" />
          <bean
          		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
          <bean
          		class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" />
          ----------------------------------------
          The web.xml is:

          Code:
          <context-param>
          		<param-name>contextConfigLocation</param-name>
          		<param-value>
          			/WEB-INF/applicationContext.xml
          		</param-value>
          	</context-param>
          	<listener>
          		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          	</listener>
          <servlet>
          		<servlet-name>sw</servlet-name>
          		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          		<load-on-startup>1</load-on-startup>
          	</servlet>
          	<servlet-mapping>
          		<servlet-name>sw</servlet-name>
          		<url-pattern>*.do</url-pattern>
          	</servlet-mapping>
          ----------------------------------------
          The Timer-Task @Scheduled method in net.ilkj.spring.scheduled.MyTask.

          Thanks for your help!
          Last edited by alkj; Nov 10th, 2009, 03:07 AM.

          Comment


          • #6
            Use [ code][/code ] tags when posting code!

            Is that your full xml? Interesting, nothing indicates duplication... Hmm...

            Is it possible to zip the project and place it here (if you want I can PM you an emailaddress if you want, that way I can have a closer look).

            Comment


            • #7
              Are you running your web application within Eclipse?

              Someone else reported that they were getting double logs because Eclipse started up two instances of their application. This could be the cause of your problem... see http://forum.springsource.org/showthread.php?t=80314

              Comment

              Working...
              X