KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > alarm > AlarmGenerator


1 // AlarmGenerator.java
2

3 package org.objectweb.alarm;
4
5 import java.rmi.RemoteException JavaDoc;
6 import javax.naming.Context JavaDoc;
7 import javax.naming.InitialContext JavaDoc;
8 import javax.naming.NamingException JavaDoc;
9 import javax.jms.*;
10
11 class ClientThread extends Thread JavaDoc {
12     String JavaDoc 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     /**
23      * random returns an integer between 0 and max - 1
24      */

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 JavaDoc getReason(int sev) {
33     String JavaDoc 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     // Create Session + Publisher
58
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 JavaDoc e) {
64         System.err.println("Cannot create JMS Publisher:"+ e);
65     }
66
67     // main loop
68
int severity = AlarmGenerator.m_severity;
69     try {
70         for (int i = 0; i < AlarmGenerator.m_loops; i++) {
71         // publish messages to the topic
72
try {
73             MapMessage message = session.createMapMessage();
74             // randomize the severity level if not specified
75
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 JavaDoc e) {
86         System.err.println("Exception in main loop"+ e);
87     }
88     }
89 }
90
91
92 public class AlarmGenerator {
93
94     static Context JavaDoc 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 JavaDoc 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 JavaDoc[] 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 JavaDoc s_arg = args[argn];
118         Integer JavaDoc 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     // Get InitialContext
144
try {
145         ictx = new InitialContext JavaDoc();
146     } catch (NamingException JavaDoc e) {
147         System.err.println("Cannot get InitialContext:"+ e);
148     }
149
150     // Lookup JMS resources
151
try {
152         // lookup the TopicConnectionFactory through its JNDI name
153
tcf = (TopicConnectionFactory) ictx.lookup("JTCF");
154         // lookup the Topic through its JNDI name
155
mytopic = (Topic) ictx.lookup("AlarmTopic");
156     } catch (NamingException JavaDoc e) {
157         System.err.println("Cannot lookup JMS Resources:"+ e);
158     }
159
160     // Create Connection
161
try {
162         mytc = tcf.createTopicConnection();
163      } catch (Exception JavaDoc e) {
164         System.err.println("Cannot create JMS Connection:"+ e);
165     }
166
167     // If reinit: send a special message to reinit AlarmTable first.
168
// This doesn't work (DuplicateKey exception)
169
// Code to remove!
170
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 JavaDoc i) {}
188         }
189     }
190
191     // Create and start threads
192
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     // Wait end of all threads
199
for (int p = 0; p < m_threads; p++) {
200         try {
201         t_thr[p].join();
202         } catch (InterruptedException JavaDoc e) {
203         System.err.println("ERROR: Problem in ClientThread.join"+ e);
204         }
205     }
206
207     // close connection
208
try {
209         mytc.close();
210     } catch (Exception JavaDoc e) {
211         e.printStackTrace();
212     }
213     }
214 }
215
Popular Tags