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