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