1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
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
50
51 public static boolean isRunning() {
52 return running;
53 }
54
55
56
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 }