3

I have a simple TimerTask implementation that just prints to the System.out

public class RefreshUserAndOLStatsJob extends TimerTask {  
   public void run() {
     System.out.println("RefreshUserAndOLStatsJob running at: " + new Date(this.scheduledExecutionTime()));     
   }
}

It is configured in the Spring application context as follows

<bean id="refreshUserAndOLStatsTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <!-- run every 30 secs -->
    <property name="period" value="30000" />
    <property name="timerTask" ref="refreshUserAndOLStatsJob" />
</bean>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="refreshUserAndOLStatsTask" />
        </list>
    </property>
</bean>

It is supposed to run every 30 seconds. Which it does but does so twice for each run, as is evident from the System out

RefreshUserAndOLStatsJob running at: Thu Feb 14 20:59:01 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 20:59:02 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 20:59:31 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 20:59:32 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 21:00:01 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 21:00:02 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 21:00:31 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 21:00:32 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 21:01:01 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 21:01:02 IST 2013
RefreshUserAndOLStatsJob running at: Thu Feb 14 21:01:31 IST 2013

Can someone guess why it could be so.

EDIT 1 : I am on Tomcat7

EDIT 2 : On adding the hashCode, as suspected by Matt, there are 2 separate instances of RefreshUserAndOLStatsJob. It seems that the application context IS indeed being initialized twice.

As for the initialization of refreshUserAndOLStatsJob in application context xml, it is not explicitly declared, but because RefreshUserAndOLStatsJob is a class marked as @Component, it seems Spring can load it automatically.

9
  • 2
    Nothing seems to stand out, so a few questions: Is this the only TimerFactoryBean in your application context? Do you know if the context get loaded multiple times? Where is the refreshUserAndOLStatsJob bean initialized? Commented Feb 14, 2013 at 16:10
  • 2
    Maybe another thing to try to help debug this would be to add System.out.println(hashCode()); to your task's run method to see if it's the same RefreshUserAndOLStatsJob object or two separate ones. Assuming you haven't overridden hashCode, it'll execute the Object implementation which should be different for two separate objects. Commented Feb 14, 2013 at 16:15
  • Just out of interest is there any reason you aren't using the newer method of scheduling tasks? See here blog.springsource.org/2010/01/05/… Commented Feb 14, 2013 at 16:15
  • @Matt, super suggestions. See the updated answer. It seems the context is being loaded multiple times. I wonder why that is. Commented Feb 15, 2013 at 4:10
  • 1
    Is it possible that you are manually loading the app context in your code as well as loading it via the ContextLoaderListener? Commented Feb 15, 2013 at 4:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.