1 3 package org.objectweb.alarm; 4 5 import java.rmi.RemoteException ; 6 import javax.naming.Context ; 7 import javax.naming.InitialContext ; 8 import javax.naming.NamingException ; 9 import javax.jms.*; 10 11 class ClientThread extends Thread { 12 String name; 13 14 public ClientThread(int num) { 15 name = AlarmGenerator.m_device; 16 if (name == null) { 17 name = "device"+num; 18 } 19 setName(name); 20 } 21 22 25 private int random(int max) { 26 27 double d = Math.random(); 28 int ret = (int) (max * d); 29 return ret; 30 } 31 32 private String getReason(int sev) { 33 String reason; 34 int num = AlarmGenerator.m_mess; 35 if (num == 0) { 36 num = random(10) + 1; 37 } 38 switch (sev) { 39 case 1: 40 reason = "Severe Error "+num; 41 break; 42 case 2: 43 reason = "Warning "+num; 44 break; 45 case 3: 46 reason = "Running OK"; 47 break; 48 default: 49 reason = "Unknown Alarm"; 50 break; 51 } 52 return reason; 53 } 54 55 public void run() { 56 57 TopicSession session = null; 59 TopicPublisher tp = null; 60 try { 61 session = AlarmGenerator.mytc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 62 tp = session.createPublisher(AlarmGenerator.mytopic); 63 } catch (Exception e) { 64 System.err.println("Cannot create JMS Publisher:"+ e); 65 } 66 67 int severity = AlarmGenerator.m_severity; 69 try { 70 for (int i = 0; i < AlarmGenerator.m_loops; i++) { 71 try { 73 MapMessage message = session.createMapMessage(); 74 if (severity == 0) 76 severity = random(2)+1; 77 message.setInt("Severity", severity); 78 message.setString("From", name); 79 message.setString("Reason", getReason(severity)); 80 tp.publish(message); 81 } catch (JMSException e) { 82 System.err.println("Exception occurred: "+ e); 83 } 84 } 85 } catch (Exception e) { 86 System.err.println("Exception in main loop"+ e); 87 } 88 } 89 } 90 91 92 public class AlarmGenerator { 93 94 static Context ictx = null; 95 static Topic mytopic = null; 96 static TopicConnection mytc = null; 97 98 99 static boolean m_reinit = false; 100 static int m_threads = 1; 101 static int m_loops = 1; 102 static int m_severity = 0; 103 static int m_mess = 0; 104 static String m_device = null; 105 106 private static void usage() { 107 System.out.println("AlarmGenerator [-d device] [-l loops] [-t threads] [-s severity] [-m num]"); 108 } 109 110 public static void main(String [] args) { 111 112 TopicConnectionFactory tcf = null; 113 114 for (int argn = 0; argn < args.length; argn++) { 117 String s_arg = args[argn]; 118 Integer i_arg; 119 if (s_arg.equals("-l")) { 120 s_arg = args[++argn]; 121 i_arg = java.lang.Integer.valueOf(s_arg); 122 m_loops = i_arg.intValue(); 123 } else if (s_arg.equals("-d")) { 124 m_device = args[++argn]; 125 } else if (s_arg.equals("-t")) { 126 s_arg = args[++argn]; 127 i_arg = java.lang.Integer.valueOf(s_arg); 128 m_threads = i_arg.intValue(); 129 } else if (s_arg.equals("-s")) { 130 s_arg = args[++argn]; 131 i_arg = java.lang.Integer.valueOf(s_arg); 132 m_severity = i_arg.intValue();; 133 } else if (s_arg.equals("-m")) { 134 s_arg = args[++argn]; 135 i_arg = java.lang.Integer.valueOf(s_arg); 136 m_mess = i_arg.intValue(); 137 } else { 138 usage(); 139 System.exit(2); 140 } 141 } 142 143 try { 145 ictx = new InitialContext (); 146 } catch (NamingException e) { 147 System.err.println("Cannot get InitialContext:"+ e); 148 } 149 150 try { 152 tcf = (TopicConnectionFactory) ictx.lookup("JTCF"); 154 mytopic = (Topic) ictx.lookup("AlarmTopic"); 156 } catch (NamingException e) { 157 System.err.println("Cannot lookup JMS Resources:"+ e); 158 } 159 160 try { 162 mytc = tcf.createTopicConnection(); 163 } catch (Exception e) { 164 System.err.println("Cannot create JMS Connection:"+ e); 165 } 166 167 if (m_reinit) { 171 TopicSession session = null; 172 TopicPublisher tp = null; 173 MapMessage message; 174 try { 175 session = AlarmGenerator.mytc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 176 tp = session.createPublisher(AlarmGenerator.mytopic); 177 message = session.createMapMessage(); 178 message.setInt("Severity", 1); 179 message.setString("From", "reinit"); 180 message.setString("Reason", "reinit"); 181 tp.publish(message); 182 } catch (JMSException e) { 183 System.err.println("Exception occurred: "+ e); 184 } finally { 185 try { 186 session.close(); 187 } catch (Exception i) {} 188 } 189 } 190 191 ClientThread[] t_thr = new ClientThread[m_threads]; 193 for (int i = 0; i < m_threads; i++) { 194 t_thr[i] = new ClientThread(i+1); 195 t_thr[i].start(); 196 } 197 198 for (int p = 0; p < m_threads; p++) { 200 try { 201 t_thr[p].join(); 202 } catch (InterruptedException e) { 203 System.err.println("ERROR: Problem in ClientThread.join"+ e); 204 } 205 } 206 207 try { 209 mytc.close(); 210 } catch (Exception e) { 211 e.printStackTrace(); 212 } 213 } 214 } 215 | Popular Tags |