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  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  package org.miloss.fgsms.auxsrv;
22  
23  import java.util.Date;
24  import javax.servlet.ServletContextEvent;
25  import javax.servlet.ServletContextListener;
26  import org.miloss.fgsms.common.DBSettingsLoader;
27  import org.miloss.fgsms.services.interfaces.policyconfiguration.KeyNameValueEnc;
28  import org.apache.log4j.Level;
29  import org.miloss.fgsms.common.Logger;;
30  import org.quartz.*;
31  import org.quartz.impl.StdSchedulerFactory;
32  
33  /**
34   * Starts up Bueller when the servlet container starts
35   *
36   * @author AO
37   */
38  public class BuellerStarter implements ServletContextListener {
39  
40      public static final String JOB_NAME = "Status Bueller";
41      public static final String TRIGGER_NAME = "Status BuellerTrigger";
42      private static Scheduler sc;
43      static Logger log = Logger.getLogger("fgsms.Aux");
44  
45      public void contextInitialized(ServletContextEvent sce) {
46          StartupCheck();
47      }
48  
49      protected static void StartupCheck() {
50          JobDetail job = null;
51          Trigger triger = null;
52  
53          try {
54              sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
55              if (!AuxConstants.QuartzJobExists(JOB_NAME, sc)) {
56                  KeyNameValueEnc interval = DBSettingsLoader.GetPropertiesFromDB(true, "Bueller", "Interval");
57                  long intinterval = 30000;
58                  if (interval != null) {
59                      try {
60                          intinterval = Long.parseLong(interval.getKeyNameValue().getPropertyValue());
61                          if (intinterval < 1000) {
62                              intinterval = 30000;
63                          }
64                      } catch (Exception ex) {
65                      }
66                  }
67  
68                  job = new JobDetail(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME, BuellerScheduler.class);
69                  triger = TriggerUtils.makeSecondlyTrigger((int) intinterval / 1000);
70                  triger.setStartTime(new Date());
71                  triger.setName(TRIGGER_NAME);
72                  triger.setGroup(AuxConstants.QUARTZ_TRIGGER_GROUP_NAME);
73                  job.setRequestsRecovery(true);
74                  sc.scheduleJob(job, triger);
75                  log.log(Level.INFO, "Status Bueller job scheduled at " + intinterval + " ms");
76                  if (sc.isShutdown()) {
77                      log.log(Level.WARN, "starting quartz");
78                      sc.start();
79                  }
80              }
81          } catch (Exception ex) {
82              log.log(Level.WARN, "error scheduling bueller", ex);
83          }
84      }
85  
86      protected static void Unschedule() {
87          try {
88              Scheduler sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
89              sc.deleteJob(BuellerStarter.JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME);
90          } catch (Exception ex) {
91              log.log(Level.DEBUG, "Unscheduling " + JOB_NAME + " failed.", ex);
92          }
93      }
94  
95      public void contextDestroyed(ServletContextEvent sce) {
96      }
97  }