001: // AlarmGenerator.java
002:
003: package org.objectweb.alarm;
004:
005: import java.rmi.RemoteException;
006: import javax.naming.Context;
007: import javax.naming.InitialContext;
008: import javax.naming.NamingException;
009: import javax.jms.*;
010:
011: class ClientThread extends Thread {
012: String name;
013:
014: public ClientThread(int num) {
015: name = AlarmGenerator.m_device;
016: if (name == null) {
017: name = "device" + num;
018: }
019: setName(name);
020: }
021:
022: /**
023: * random returns an integer between 0 and max - 1
024: */
025: private int random(int max) {
026:
027: double d = Math.random();
028: int ret = (int) (max * d);
029: return ret;
030: }
031:
032: private String getReason(int sev) {
033: String reason;
034: int num = AlarmGenerator.m_mess;
035: if (num == 0) {
036: num = random(10) + 1;
037: }
038: switch (sev) {
039: case 1:
040: reason = "Severe Error " + num;
041: break;
042: case 2:
043: reason = "Warning " + num;
044: break;
045: case 3:
046: reason = "Running OK";
047: break;
048: default:
049: reason = "Unknown Alarm";
050: break;
051: }
052: return reason;
053: }
054:
055: public void run() {
056:
057: // Create Session + Publisher
058: TopicSession session = null;
059: TopicPublisher tp = null;
060: try {
061: session = AlarmGenerator.mytc.createTopicSession(false,
062: Session.AUTO_ACKNOWLEDGE);
063: tp = session.createPublisher(AlarmGenerator.mytopic);
064: } catch (Exception e) {
065: System.err.println("Cannot create JMS Publisher:" + e);
066: }
067:
068: // main loop
069: int severity = AlarmGenerator.m_severity;
070: try {
071: for (int i = 0; i < AlarmGenerator.m_loops; i++) {
072: // publish messages to the topic
073: try {
074: MapMessage message = session.createMapMessage();
075: // randomize the severity level if not specified
076: if (severity == 0)
077: severity = random(2) + 1;
078: message.setInt("Severity", severity);
079: message.setString("From", name);
080: message.setString("Reason", getReason(severity));
081: tp.publish(message);
082: } catch (JMSException e) {
083: System.err.println("Exception occurred: " + e);
084: }
085: }
086: } catch (Exception e) {
087: System.err.println("Exception in main loop" + e);
088: }
089: }
090: }
091:
092: public class AlarmGenerator {
093:
094: static Context ictx = null;
095: static Topic mytopic = null;
096: static TopicConnection mytc = null;
097:
098: static boolean m_reinit = false;
099: static int m_threads = 1;
100: static int m_loops = 1;
101: static int m_severity = 0;
102: static int m_mess = 0;
103: static String m_device = null;
104:
105: private static void usage() {
106: System.out
107: .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: // Get Args
115: // Get command args
116: 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: ;
134: } else if (s_arg.equals("-m")) {
135: s_arg = args[++argn];
136: i_arg = java.lang.Integer.valueOf(s_arg);
137: m_mess = i_arg.intValue();
138: } else {
139: usage();
140: System.exit(2);
141: }
142: }
143:
144: // Get InitialContext
145: try {
146: ictx = new InitialContext();
147: } catch (NamingException e) {
148: System.err.println("Cannot get InitialContext:" + e);
149: }
150:
151: // Lookup JMS resources
152: try {
153: // lookup the TopicConnectionFactory through its JNDI name
154: tcf = (TopicConnectionFactory) ictx.lookup("JTCF");
155: // lookup the Topic through its JNDI name
156: mytopic = (Topic) ictx.lookup("AlarmTopic");
157: } catch (NamingException e) {
158: System.err.println("Cannot lookup JMS Resources:" + e);
159: }
160:
161: // Create Connection
162: try {
163: mytc = tcf.createTopicConnection();
164: } catch (Exception e) {
165: System.err.println("Cannot create JMS Connection:" + e);
166: }
167:
168: // If reinit: send a special message to reinit AlarmTable first.
169: // This doesn't work (DuplicateKey exception)
170: // Code to remove!
171: if (m_reinit) {
172: TopicSession session = null;
173: TopicPublisher tp = null;
174: MapMessage message;
175: try {
176: session = AlarmGenerator.mytc.createTopicSession(false,
177: Session.AUTO_ACKNOWLEDGE);
178: tp = session.createPublisher(AlarmGenerator.mytopic);
179: message = session.createMapMessage();
180: message.setInt("Severity", 1);
181: message.setString("From", "reinit");
182: message.setString("Reason", "reinit");
183: tp.publish(message);
184: } catch (JMSException e) {
185: System.err.println("Exception occurred: " + e);
186: } finally {
187: try {
188: session.close();
189: } catch (Exception i) {
190: }
191: }
192: }
193:
194: // Create and start threads
195: ClientThread[] t_thr = new ClientThread[m_threads];
196: for (int i = 0; i < m_threads; i++) {
197: t_thr[i] = new ClientThread(i + 1);
198: t_thr[i].start();
199: }
200:
201: // Wait end of all threads
202: for (int p = 0; p < m_threads; p++) {
203: try {
204: t_thr[p].join();
205: } catch (InterruptedException e) {
206: System.err
207: .println("ERROR: Problem in ClientThread.join"
208: + e);
209: }
210: }
211:
212: // close connection
213: try {
214: mytc.close();
215: } catch (Exception e) {
216: e.printStackTrace();
217: }
218: }
219: }
|