View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10   *
11   * 
12   */
13  /*  ---------------------------------------------------------------------------
14   *  U.S. Government, Department of the Army
15   *  Army Materiel Command
16   *  Research Development Engineering Command
17   *  Communications Electronics Research Development and Engineering Center
18   *  ---------------------------------------------------------------------------
19   */
20  package org.miloss.fgsms.auxsrv;
21  
22  import java.util.Date;
23  import javax.servlet.ServletContextEvent;
24  import javax.servlet.ServletContextListener;
25  import org.apache.log4j.Level;
26  import org.miloss.fgsms.common.Logger;;
27  import org.quartz.*;
28  import org.quartz.impl.StdSchedulerFactory;
29  
30  /**
31   * Starts up Report Generator when the servlet container
32   * starts
33   *
34   * @author AO
35   */
36  public class ReportGenStarter implements ServletContextListener {
37  
38      public static final String JOB_NAME = "Report Generator";
39      public static final String TRIGGER_NAME = "Report Generator Trigger";
40      private static Scheduler sc;
41      static Logger log = Logger.getLogger("fgsms.Aux");
42  
43      public void contextInitialized(ServletContextEvent sce) {
44          StartupCheck();
45  
46      }
47  
48      protected static void StartupCheck() {
49          JobDetail job = null;
50          Trigger triger = null;
51          try {
52              sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
53              if (sc == null) {
54                  log.fatal("Unable to reference the Quartz instance of " + AuxConstants.QUARTZ_SCHEDULER_NAME + " ensure that it exists and is started. Unable to schedule job for " + JOB_NAME);
55              }
56  
57              if (!AuxConstants.QuartzJobExists(JOB_NAME, sc)) {
58                  job = new JobDetail(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME, org.miloss.fgsms.auxsrv.ReportGenScheduler.class);
59                  job.setRequestsRecovery(true);
60                  triger = TriggerUtils.makeMinutelyTrigger();
61                  triger.setStartTime(new Date());
62                  triger.setName(TRIGGER_NAME);
63                  triger.setGroup(AuxConstants.QUARTZ_TRIGGER_GROUP_NAME);
64                  sc.scheduleJob(job, triger);
65                  log.log(Level.INFO, "Report Generator added");
66                  if (sc.isShutdown()) {
67                      log.log(Level.WARN, "starting quartz");
68                  }
69                  sc.start();
70              }
71          } catch (Exception ex) {
72              log.log(Level.WARN, "error scheduling Report Generator", ex);
73          }
74      }
75  
76      public void contextDestroyed(ServletContextEvent sce) {
77          /*
78           * try { sc = new StdSchedulerFactory().getScheduler();
79           *
80           * sc.unscheduleJob("Report Generator", "fgsms Aux Services ");
81           * sc.deleteJob("Report Generator", "fgsms Aux Services ");
82           *
83           *
84           * log.log(Level.WARN, "Undeploying fgsms Aux Services Succeeded. Jobs
85           * removed from Quartz scheduler."); } catch (SchedulerException ex) {
86           * log.log(Level.WARN, "error unscheduling bueller", ex);
87          }
88           */
89      }
90  }