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.sla;
23  
24  import org.miloss.fgsms.plugins.sla.AlertContainer;
25  import java.util.Queue;
26  import java.util.UUID;
27  import java.util.concurrent.ConcurrentLinkedQueue;
28  import java.util.concurrent.locks.ReentrantLock;
29  import org.apache.log4j.Level;
30  import org.miloss.fgsms.common.Logger;;
31  
32  /**
33   * SLA Processor Singleton Provides basic thread pool
34   * management and is only used when jboss's thread pooling isn't available
35   *
36   * @author AO
37   */
38  public class SLAProcessorSingleton {
39  
40      private static boolean running = true;
41  
42      static synchronized void EnqueueAlert(AlertContainer alertContainer) {
43          queue.add(alertContainer);
44          log.log(Level.DEBUG, "Enqueue alert " + alertContainer.getSLAID());
45          run();
46      }
47  
48      /**
49       * @return the running
50       */
51      public static boolean isRunning() {
52          return running;
53      }
54  
55      /**
56       * @param aRunning the running to set
57       */
58      public static void setRunning(boolean aRunning) {
59          running = aRunning;
60      }
61  
62      private SLAProcessorSingleton() {
63      }
64  
65      public static SLAProcessorSingleton getInstance() {
66          return SLAProcessorSingletonHolder.INSTANCE;
67      }
68  
69      private static class SLAProcessorSingletonHolder {
70  
71          private static final SLAProcessorSingleton INSTANCE = new SLAProcessorSingleton();
72      }
73      private static Thread t = null;
74      private static final Queue<AlertContainer> queue = new ConcurrentLinkedQueue<AlertContainer>();
75      static final Logger log = Logger.getLogger("fgsms.SLAProcessor");
76  
77      protected static int GetQueueSize() {
78          if (queue == null) {
79              return -1;
80          }
81          return queue.size();
82      }
83  
84      protected static void run() {
85          ReentrantLock lock = new ReentrantLock();
86          lock.lock();
87          try {
88              if (!isRunning() || t == null) {
89                  log.log(Level.INFO, " == fgsms Alerting== launched new thread send out alerts.");
90                  try {
91                      t = new Thread(new AlertRunner(queue), "SLA Alerting Thread " + UUID.randomUUID().toString());
92                      t.start();
93                      if (!t.isAlive()) {
94                          throw new NullPointerException("completely unexpected error starting the SLA alerting thread");
95                      }
96                  } catch (Exception ex) {
97  		     ex.printStackTrace();
98                      log.log(Level.FATAL, "******************************************************* fgsms could not start the SLA Processor Alerting Thread. This is most likely due to server overloading, memory limits or hitting the maxium thread pool for the container. Please consider revising. "
99                              + "Purging " + queue.size() + " from the outbound queue to prevent container overload. *********************************************", ex);
100                     try {
101                         queue.clear();
102                     } catch (Exception ex2) {
103                     }
104                 }
105             }
106         } finally {
107             lock.unlock();
108         }
109     }
110 }