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  
22  package org.miloss.fgsms.auxsrv;
23  
24  import javax.servlet.ServletContextEvent;
25  import javax.servlet.ServletContextListener;
26  import org.apache.log4j.Level;
27  import org.miloss.fgsms.common.Logger;;
28  import org.quartz.*;
29  import org.quartz.impl.StdSchedulerFactory;
30  
31  /**
32   * This quartz job will periodically check to ensure that all necessary jobs are
33   * functioning and that the schedule intervals are synchronized with the desired
34   * settings of each job.
35   *
36   * @author AO
37   */
38  public class JobController implements StatefulJob, ServletContextListener {
39  
40      public static final String JOB_NAME = "JobController";
41      public static final String TRIGGER_NAME = "JobController";
42      private static Scheduler sc;
43      static Logger log = Logger.getLogger("fgsms.Aux");
44  
45      @Override
46      public void execute(JobExecutionContext jec) throws JobExecutionException {
47          /*
48           * unschedule all jobs schedule all jobs based on new intervals confirm
49           * startup
50           */
51          TriggerStartups();
52  
53          try {
54              if (sc == null) {
55                  sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
56              }
57              String[] jobNames = sc.getJobNames(AuxConstants.QUARTZ_GROUP_NAME);
58              int count = 0;
59              for (int i = 0; i < jobNames.length; i++) {
60                  if (jobNames[i].equalsIgnoreCase(JOB_NAME)
61                          
62                          || jobNames[i].equalsIgnoreCase(BuellerStarter.JOB_NAME)
63                          || jobNames[i].equalsIgnoreCase(QpidJMXStarter.JOB_NAME)
64                          || jobNames[i].equalsIgnoreCase(NTSLAStarter.JOB_NAME)
65                          || jobNames[i].equalsIgnoreCase(StatisticsStarter.JOB_NAME)
66                          || jobNames[i].equalsIgnoreCase(SMXJMXStarter.JOB_NAME)
67                          || jobNames[i].equalsIgnoreCase(HornetQStarter.JOB_NAME)
68                          || jobNames[i].equalsIgnoreCase(FederationStarter.JOB_NAME)
69                          || jobNames[i].equalsIgnoreCase(DataPrunerStarter.JOB_NAME)
70                          || jobNames[i].equalsIgnoreCase(ReportGenStarter.JOB_NAME)
71                          || jobNames[i].equalsIgnoreCase(DependencyScannerStarter.JOB_NAME)) {
72                      count++;
73                  }
74              }
75              if (count < 12) {
76                  TriggerStartups();
77              }
78          } catch (Exception ex) {
79              log.log(Level.FATAL, null, ex);
80          }
81      }
82  
83      void TriggerStartups() {
84          //agents
85          BuellerStarter.Unschedule();
86          BuellerStarter.StartupCheck();
87  
88          HornetQStarter.Unschedule();
89          HornetQStarter.StartupCheck();
90  
91          QpidJMXStarter.Unschedule();
92          QpidJMXStarter.StartupCheck();
93  
94          SMXJMXStarter.Unschedule();
95          SMXJMXStarter.StartupCheck();
96  
97          //core services
98          NTSLAStarter.Unschedule();
99          NTSLAStarter.StartupCheck();
100 
101         StatisticsStarter.Unschedule();
102         StatisticsStarter.StartupCheck();
103 
104         //items that aren't adjustable via quartz (that need to report their operating status)
105         DataPrunerStarter.StartupCheck();
106         DependencyScannerStarter.StartupCheck();
107         FederationStarter.StartupCheck();
108         ReportGenStarter.StartupCheck();
109 
110         //check it again to ensure started
111 
112         BuellerStarter.StartupCheck();
113         HornetQStarter.StartupCheck();
114         SMXJMXStarter.StartupCheck();
115         StatisticsStarter.StartupCheck();
116         NTSLAStarter.StartupCheck();
117         QpidJMXStarter.StartupCheck();
118     }
119 
120     @Override
121     public void contextInitialized(ServletContextEvent sce) {
122         StartupCheck();
123     }
124 
125     protected static void StartupCheck() {
126         JobDetail job = null;
127         Trigger triger = null;
128 
129         try {
130             sc = new StdSchedulerFactory().getScheduler(AuxConstants.QUARTZ_SCHEDULER_NAME);
131             if (sc == null) {
132                 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);
133             }
134 
135             if (!AuxConstants.QuartzJobExists(JOB_NAME, sc)) {
136                 job = new JobDetail(JOB_NAME, AuxConstants.QUARTZ_GROUP_NAME, JobController.class);
137                 job.setRequestsRecovery(true);
138                 job.setDurability(true);
139                 triger = TriggerUtils.makeMinutelyTrigger(15);
140                 triger.setGroup(AuxConstants.QUARTZ_TRIGGER_GROUP_NAME);
141                 triger.setName(TRIGGER_NAME);
142                 sc.scheduleJob(job, triger);
143                 log.log(Level.INFO, JOB_NAME + " job added, running every 15");
144                 if (sc.isShutdown()) {
145                     log.log(Level.WARN, "starting quartz");
146                     sc.start();
147                 }
148             } else {
149                 log.log(Level.DEBUG, JOB_NAME + " already scheduled");
150             }
151         } catch (Exception ex) {
152             log.log(Level.WARN, "error scheduling data pruner", ex);
153         }
154     }
155 
156     @Override
157     public void contextDestroyed(ServletContextEvent sce) {
158     }
159 }