Spring - TimerTask implementation is executed twice

I have a simple TimerTask implementation that just prints to 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 context of a Spring application 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 should start every 30 seconds. Which it does, but does it twice for each run, as seen from 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 this might be the case.

EDIT 1: I'm on Tomcat7

EDIT 2: When adding the hashCode, as Matt suspects, there are 2 separate instances of RefreshUserAndOLStatsJob. It seems that the application context is indeed initialized twice.

As far as initializing refreshUserAndOLStatsJob in the xml application context, it is not explicitly declared, but since RefreshUserAndOLStatsJob is a class marked with @Component, it seems that Spring can load it automatically.

+3


source to share





All Articles