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   *
35   * @author AO
36   */
37  public class StatisticsStarter implements ServletContextListener {
38  
39      public static final String JOB_NAME = "Statistics Aggregator";
40      public static final String TRIGGER_NAME = "Stat Agg Trigger";
41      private static Scheduler sc;
42      static Logger log = Logger.getLogger("fgsms.Aux");
43  
44      public void contextInitialized(ServletContextEvent sce) {
45          StartupCheck();
46      }
47  
48      protected static void StartupCheck() {
49          try {
50              JobDetail job = null;
51              Trigger triger = null;
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              KeyNameValueEnc interval = DBSettingsLoader.GetPropertiesFromDB(true, "StatisticsAggregator", "Interval");
58              //defautl is 5 minutes
59              long intinterval = 5 * 60 * 1000;
60              if (interval != null) {
61                  try {
62                      intinterval = Long.parseLong(interval.getKeyNameValue().getPropertyValue());
63                      if (intinterval < 1000) {
64                          intinterval = 5 * 60 * 1000;
65                      }
66                  } catch (Exception ex) {
67                  }
68              }
69  
70              if (!AuxConstants.QuartzJobExists(JOB_NAME, sc)) {
71                  job = new JobDetail(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME, org.miloss.fgsms.auxsrv.StatisticsAggregatorScheduler.class);
72                  triger = TriggerUtils.makeSecondlyTrigger((int) intinterval / 1000);
73                  triger.setStartTime(new Date());
74                  triger.setName(TRIGGER_NAME);
75                  triger.setGroup(AuxConstants.QUARTZ_TRIGGER_GROUP_NAME);
76                  sc.scheduleJob(job, triger);
77                  log.log(Level.INFO, "Stats Agg job added at interval " + intinterval);
78                  if (sc.isShutdown()) {
79                      log.log(Level.WARN, "starting quartz");
80                  }
81                  sc.start();
82              }
83          } catch (Exception ex) {
84              log.log(Level.WARN, "error scheduling statistics agg", ex);
85          }
86      }
87  
88      protected static void Unschedule() {
89          try {
90  
91              Scheduler sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
92              sc.deleteJob(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME);
93          } catch (Exception ex) {
94              log.log(Level.DEBUG, "Error removing job for " + JOB_NAME, ex);
95          }
96      }
97  
98      public void contextDestroyed(ServletContextEvent sce) {
99      }
100 }