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 Apache ServiceMix and ActiveMQ Agent when the servlet
35   * container starts
36   *
37   * @author AO
38   */
39  public class SMXJMXStarter implements ServletContextListener {
40  
41      public static final String JOB_NAME = "SMX JMX";
42      public static final String TRIGGER_NAME = "SMX JMX Trigger";
43      private static Scheduler sc;
44      static Logger log = Logger.getLogger("fgsms.Aux");
45  
46      public void contextInitialized(ServletContextEvent sce) {
47          StartupCheck();
48      }
49  
50      protected static void StartupCheck() {
51          try {
52              JobDetail job = null;
53              Trigger triger = null;
54              sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
55              if (sc == null) {
56                  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);
57              }
58  
59              KeyNameValueEnc interval = DBSettingsLoader.GetPropertiesFromDB(true, "SMXJMXAgent", "Interval");
60              long intinterval = 30000;
61              if (interval != null) {
62                  try {
63                      intinterval = Long.parseLong(interval.getKeyNameValue().getPropertyValue());
64                      if (intinterval < 1000) {
65                          intinterval = 1000;
66                      }
67                  } catch (Exception ex) {
68                  }
69              }
70  
71  
72              if (!AuxConstants.QuartzJobExists(JOB_NAME, sc)) {
73                  job = new JobDetail(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME, org.miloss.fgsms.auxsrv.SMXJMXScheduler.class);
74                  job.setRequestsRecovery(true);
75  
76                  try {
77                      triger = TriggerUtils.makeSecondlyTrigger((int) (intinterval / 1000));
78  
79                      triger.setStartTime(new Date());
80                  } catch (Exception ex) {
81                      log.log(Level.WARN, "the configuration parameter for smx via jmx interval is " + intinterval + " and could not be parsed as an integer. Defaulting to 30 second pings");
82                      triger = TriggerUtils.makeSecondlyTrigger(30);
83                      intinterval = 30 * 1000;
84                  }
85                  triger.setName(TRIGGER_NAME);
86                  triger.setGroup(AuxConstants.QUARTZ_TRIGGER_GROUP_NAME);
87                  sc.scheduleJob(job, triger);
88                  log.log(Level.INFO, "SMX JMX added at interval " + intinterval + "ms");
89  
90                  if (sc.isShutdown()) {
91                      log.log(Level.WARN, "starting quartz");
92                  }
93                  sc.start();
94              }
95          } catch (Exception ex) {
96              log.log(Level.WARN, "error scheduling smx agent", ex);
97          }
98      }
99  
100     protected static void Unschedule() {
101         try {
102 
103             Scheduler sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
104             sc.deleteJob(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME);
105         } catch (Exception ex) {
106             log.log(Level.DEBUG, "Error removing job for " + JOB_NAME, ex);
107         }
108     }
109 
110     public void contextDestroyed(ServletContextEvent sce) {
111         /*
112          * try { sc = new StdSchedulerFactory().getScheduler();
113          *
114          *
115          * sc.unscheduleJob("SMX JMX", "fgsms Aux Services ");
116          * sc.deleteJob("SMX JMX", "fgsms Aux Services ");
117          *
118          *
119          * log.log(Level.WARN, "Undeploying fgsms Aux Services Succeeded. Jobs
120          * removed from Quartz scheduler."); } catch (SchedulerException ex) {
121          * log.log(Level.WARN, "error unscheduling smx agent", ex); }
122          */
123     }
124 }