title: Creation and Usage of TimerTask: How not to have circular dependency?
body: <p>I am working with Eclipse Luna and WildFly 8.2.1.Final.</p>
  
  <p>I have an Enterprise Application project named <strong>P001_EAR</strong> which consist of:</p>
  
  <ol>
  <li>A <code>Dynamic Web Project</code> named <strong>P001_WAR</strong>.</li>
  <li>An <code>EJB Project</code> named <strong>P001_EJB</strong>.</li>
  <li>An <code>EJB Client Project</code> named <strong>P001_EJBClient</strong>.</li>
  <li>An <code>Utility Project</code> named <strong>P001_SRC</strong>.</li>
  </ol>
  
  <p>This is how each project references:</p>
  
  <ul>
  <li><strong>P001_SRC</strong> does not reference any other project.<br></li>
  <li><strong>P001_EJB</strong> references <strong>P001_SRC</strong> and <strong>P001_EJBClient</strong>. <br></li>
  <li><strong>P001_EJBClient</strong> references <strong>P001_SRC</strong>. <br></li>
  <li><strong>P001_WAR</strong> references <strong>P001_SRC</strong> and <strong>P001_EJBClient</strong>. <br></li>
  </ul>
  
  <p>Note: In <strong>P001_WAR</strong> I have 1 <code>Business Delegate</code> for each EJB which call EJB methods of that EJB. </p>
  
  <hr>
  
  <p>I need to create a <code>java.util.TimerTask</code> class which will be scheduled to run (the schedule information like first time and frequency is in database). On each run it will call couple of methods of an EJB. This is what I come up with:</p>
  
  <pre><code>public final class FirstTimerTask extends TimerTask {
     @Override
     public void run() {
        // get EJB1
        // call method of EJB1
        // call another method of EJB1
     }
  }
  </code></pre>
  
  <p>I need to create a class which will create 1 or more <code>java.util.TimerTask</code> class and schedule them. It will have the <code>java.util.Timer</code>. The schedule information of a task is in database which I was hoping to get through an EJB. This is what I come up with:</p>
  
  <pre><code>public final class ScheduleTasksManager {
     private static Timer TIMER = new TIMER();
  
     public static void scheduleFirstTimerTask() {
        TimerTask timerTask = null;
        Date taskStartDate = null;
        long period = -1;
        // get EJB2
        // call method of EJB2 to retrieve the schedule information from database and populate taskStartDate and period
        timerTask = new FirstTimerTask();
        TIMER.scheduleAtFixedRate(timerTask, taskStartDate, period);
     }
  }
  </code></pre>
  
  <p>My question is where to put <code>FirstTimerTask</code> and <code>ScheduleTasksManager</code> classes so that I dont have any circular dependencies? I am stumped. </p>
  
  <p>I think <code>FirstTimerTask</code> would need to be in a project which has reference to <strong>P001_EJBClient</strong> as it needs to call EJB. </p>
  
  <p>I think <code>ScheduleTasksManager</code> would need to be in a project which has reference to <strong>P001_EJBClient</strong> as it needs to call EJB. </p>