title: Run a thread automatically from a WAR
body: <p>Im having a war file which contains some jsp files. I have a thread in a separate java file which updates the database periodically and  my jsp files will get data from the database on demand.</p>
  
  <p>Now , I need the thread to start automatically from a war archive so that it can do the tasks periodically and the user can get updated results from the view component.</p>
  
  <pre><code>public class AttendanceThread {
      private long DEFAULT_SLEEP_TIME = 10000;// 10 seconds.
      private long MAX_SLEEP_TIME = 30000;// 30 seconds.
      private int shift;
  
      public void MonitorAtttendance() {
          final UpdateAttendance updateObject = new UpdateAttendance();
          Runnable attThread = new Runnable() {
              @Override
              public void run() {
                  try {
                            // logic to calculate the current shift time
  
                              updateObject.CheckEmpAttendance(shift);
                              updateObject.resetUpdatedAttendance(shift);
                              Thread.sleep(MAX_SLEEP_TIME);
                          } else {
                              Thread.sleep(DEFAULT_SLEEP_TIME);
                          }
                      }// end of while (true)
                  } catch (Exception e) {
                                          }
              }// end of run()
          }; // end of attThread definition
          Thread t = new Thread(attThread);
          t.setName(this.getClass().getSimpleName());
          t.start();
      } 
  
      public static void main(String args[]) {
          // logger.log(Level.INFO, "Monitor Atttendance");
          AttendanceThread ma = new AttendanceThread();
          ma.MonitorAtttendance();
      }
  }
  </code></pre>
  
  <p>Currently for test purposes, im running this java file separetely so that the thread can run and i can get updated results in my JSP files.</p>
  
  <p>Is there any way we can start the thread from the WAR itself or automatically when the web server starts. Any help will be much appreciated. :)</p>