views:

115

answers:

2

I have a very simple method scheduled to run every 10 seconds like this:

@Component
public class SimpleTask {

    @Scheduled(fixedRate=10000)
    public void first() {
        System.out.println("Simple Task  " + new Date());
    }
}

Config:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5"  /> 
<task:scheduler id="myScheduler" pool-size="10"  />

My problem is that my method is being invoked 3 times every 10 seconds. It should be invoked just once. What am I doing wrong? I use Spring Source ToolSuite with SpringSource tc Server 6.

A: 

you are mixing annotations with configuration and I dont believe you need both

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-namespace

From Documentation

Note Make sure that you are not initializing multiple instances of the same @Scheduled annotation class at runtime, unless you do want to schedule callbacks to each such instance. Related to this, make sure that you do not use @Configurable on bean classes which are annotated with @Scheduled and registered as regular Spring beans with the container: You would get double initialization otherwise, once through the container and once through the @Configurable aspect, with the consequence of each @Scheduled method being invoked twice.

Aaron Saunders
A: 

may be you load applicationContext multiple times ?

davideconsonni