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 the Non Transactional SLA Processor when the servlet
35   * container starts
36   *
37   * @author AO
38   */
39  public class NTSLAStarter implements ServletContextListener {
40  
41      public static final String JOB_NAME = "NTSLA";
42      public static final String TRIGGER_NAME = "NTSLA Trigger";
43      private static Scheduler sc;
44      static Logger log = Logger.getLogger("fgsms.Aux");
45  
46      public void contextInitialized(ServletContextEvent sce) {
47      }
48  
49      protected static void StartupCheck() {
50          try {
51              KeyNameValueEnc interval = DBSettingsLoader.GetPropertiesFromDB(true, "NTSLAProcessor", "Interval");
52              long intinterval = 10000;
53              if (interval != null) {
54                  try {
55                      intinterval = Long.parseLong(interval.getKeyNameValue().getPropertyValue());
56                      if (intinterval < 1000) {
57                          intinterval = 1000;
58                      }
59                  } catch (Exception ex) {
60                  }
61              }
62  
63              sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
64              if (sc == null) {
65                  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);
66              }
67              JobDetail job = null;
68              Trigger triger = null;
69  
70              if (!AuxConstants.QuartzJobExists(JOB_NAME, sc)) {
71                  job = new JobDetail(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME, org.miloss.fgsms.auxsrv.NTSLAProcessorScheduler.class);
72                  job.setRequestsRecovery(true);
73                  triger = TriggerUtils.makeSecondlyTrigger((int) intinterval / 1000);
74                  triger.setName(TRIGGER_NAME);
75                  triger.setGroup(AuxConstants.QUARTZ_TRIGGER_GROUP_NAME);
76                  sc.scheduleJob(job, triger);
77                  log.log(Level.INFO, "NTSLA 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 ntsla", 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          * try { sc = new StdSchedulerFactory().getScheduler();
101          *
102          *
103          * sc.unscheduleJob("NTSLA", "fgsms Aux Services ");
104          * sc.deleteJob("NTSLA", "fgsms Aux Services ");
105          *
106          *
107          * log.log(Level.WARN, "Undeploying fgsms Aux Services Succeeded. Jobs
108          * removed from Quartz scheduler."); } catch (SchedulerException ex) {
109          * log.log(Level.WARN, "error unscheduling NTSLA from quartz", ex); }
110          */
111     }
112 }