title: Multiple Instances Of Spring Scheduler Task
body: <p>We have an EJB application running on Webshere Application Server. Part of the processing there are several scheduled tasks that need to run with a fix delay of 50 to 200 milliseconds so we had to use Spring scheduled task to achieve this.</p>
  
  <p>The Spring application context is only loaded once when the application starts using  <code>javax.ejb.Singleton javax.ejb.Startup</code> annotated bean:</p>
  
  <pre><code>@Singleton
  @Startup
  public class ApplicationLifeCyleBean {
      private ApplicationContext applicationContext;
  
      @PostConstruct
      public void initialize() {
          applicationContext = Application.CONTEXT.getApplicationContext();
      }
  }
  </code></pre>
  
  <p>The <code>Application.CONTEXT</code> is just a singleton created as an enum to make sure it is only loaded once. Note there is only one class loader configured in WAS for this application.</p>
  
  <pre><code>public enum Application {
      CONTEXT;
  
      applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
  
      public ApplicationContext getApplicationContext() {
          return applicationContext;
      }
  }
  </code></pre>
  
  <p>Now inside the <code>application-context.xml</code> we define our scheduled tasks as below:</p>
  
  <pre><code>&lt;bean id="taskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor"&gt;
      &lt;property name="resourceRef" value="true"/&gt;
      &lt;property name="workManagerName" value="wm/default" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="taskScheduler" class="org.springframework.scheduling.commonj.TimerManagerTaskScheduler"&gt;
      &lt;property name="resourceRef" value="true"/&gt;
      &lt;property name="timerManagerName" value="java:app/env/timerManager"/&gt;
      &lt;property name="shared" value="false"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myProcessor" class="my.company.MyProcessor"/&gt;
  
  &lt;task:scheduled-tasks scheduler="taskScheduler"&gt;
      &lt;task:scheduled ref="myProcessor" method="process" fixed-delay="${my.proc.interval}"/&gt;
      ...
  &lt;/task:scheduled-tasks&gt;
  </code></pre>
  
  <p>What this processor does is to delete some expired records from the database and we noticed some dead lock exceptions coming up in the logs now and then. The only one explanation we had was that the <code>DELETE</code> was executed by more than one process at the same time. With this in mind to help us with our investigations we added one extra singleton to store the running state and changed the <code>MyProcessor</code> code to record the running state as below:</p>
  
  <pre><code>public enum Processors {
      MY_PROCESSOR;
  
      private boolean running;
  
      public synchronized boolean isRunning() {
          return running;
      }
  
      public synchronized void setRunning(boolean running) {
          this.running = running;
      }
  }
  
  public class MyProcessor {
      public void process () {
          if (Processors.MY_PROCESSOR.isRunning()) {
              logger.warn("My process was found to be running. Please investigate.");
          } else {
              Processors.MY_PROCESSOR.setRunning(true);
              // Do some work here  
              Processors.MY_PROCESSOR.setRunning(false); 
          }
      }
  }
  </code></pre>
  
  <p>As expected the fact that there were two instances of processor running came in the logs:</p>
  
  <pre><code>[18/10/17 0:01:32:613 EST] 00000245 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  [18/10/17 0:03:32:626 EST] 00000244 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  [18/10/17 0:05:32:640 EST] 00000245 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  [18/10/17 0:07:32:654 EST] 00000245 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  [18/10/17 0:09:32:668 EST] 00000245 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  [18/10/17 0:11:32:682 EST] 00000245 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  [18/10/17 0:13:32:696 EST] 00000245 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  [18/10/17 0:15:32:710 EST] 00000244 SystemOut     O    [WARN ] my.company.MyProcessor - My process was found to be running. Please investigate.
  </code></pre>
  
  <p>Now we no longer get the dead locks but there is still the question what is causing the two instances to be created.</p>
  
  <p>Thank you in advance for your inputs/suggestions</p>