title: Multiple Invocations of Spring Scheduled Task
body: <p>So I am having a bit of a problem with some scheduled tasks I have declared using Spring's <code>@Scheduled</code> annotation. First off here is my app-config:</p>
  
  <pre><code>&lt;beans &lt;!--Namespaces--&gt;&gt;
      &lt;!-- Search for annotated beans --&gt;
      &lt;context:component-scan  base-package="com.my.package.task"/&gt;
  
      &lt;task:annotation-driven executor="myExecutor" scheduler="myScheduler"/&gt;
  
      &lt;task:executor id="myExecutor" pool-size="5"/&gt;
  
      &lt;task:scheduler id="myScheduler" pool-size="10"/&gt;
  
      &lt;tx:annotation-driven/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I also have a AppConfig class:</p>
  
  <pre><code>@Configuration
  @ImportResource("classpath:/config/app-config.xml")
  public class AppConfig {
       //Bean declarations but NO references to the Scheduled tasks or their classes
  }
  </code></pre>
  
  <p>And here are the scheduled tasks I have declared:</p>
  
  <pre><code>public abstract class ScheduledTask {
       private ApplicationContext appContext;
  
       abstract void doSomething();
  
       protected getAppContext() {
            if(appContext == null) {
                 appContext = new AnnotationConfigApplicationContext(AppConfig.class);
            }
  
            return appContext;
       }
       //Other common methods
  }
  
  @Service
  public class Task1 extends ScheduledTask {    
       @Scheduled(cron="0 0 9 * * MON-FRI")
       public void doSomething() {
          System.out.println("Task1 -- Thread ID: " + Thread.currentThread().getId() + " Thread Name: " + Thread.currentThread().getName());
           //Update the database with new values
       }
  }
  
  @Service
  public class Task2 extends Scheduledtask {
       @Scheduled(cron="0 0 10 * * MON-FRI")
       public void doSomething() {
            System.out.println("Task2 -- Thread ID: " + Thread.currentThread().getId() + " Thread Name: " + Thread.currentThread().getName());
            //Get some data from database and send email with data from DB
       }
  }
  </code></pre>
  
  <p>Now the problem I am getting is that when these tasks are run, I get multiple emails sent (see Task2). I added in the print statements to see if the methods were being called multiple times and Task2 is executed twice by 2 different threads. I would hope that Spring would only invoke a scheduled task once. Here is the output I get:</p>
  
  <pre><code>Task1 -- Thread ID: 10 Thread Name: myScheduler-1
  Task2 -- Thread ID: 23 Thread Name: myScheduler-2
  Task2 -- Thread ID: 11 Thread Name: myScheduler-2 
  </code></pre>
  
  <p>And if I run it at night and set the cron jobs to be like 4hrs apart I start to get even more emails sent (thought I havent had a chance to see if this is due to multiple threads / method calls).</p>
  
  <p>Does anyone have any ideas what could be causing the multiple invocations of the second task?  I looked at the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled" rel="nofollow">Spring Documentation</a> and under the 'Note' it mentions that you can get multiple invocation if you initialize multiple instances of the <code>@Scheduled</code> class at runtime. Could that be happening here with my AppConfig and app-config.xml?</p>