KickJava   Java API By Example, From Geeks To Geeks.

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


1 // "plain/text"
2
package org.objectweb.alarm;
3
4 import java.io.*;
5 import javax.jms.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.naming.NamingException JavaDoc;
11 import javax.rmi.PortableRemoteObject JavaDoc;
12
13 /**
14  * This servlet is used to send Alarms on a JMS topic
15  */

16 public class Sender extends HttpServlet {
17
18     public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
19
20     res.setContentType("text/html");
21         PrintWriter out = res.getWriter();
22     String JavaDoc device = req.getParameter("device");
23     String JavaDoc level = req.getParameter("level");
24     String JavaDoc message = req.getParameter("message");
25     String JavaDoc sloop = req.getParameter("number");
26     String JavaDoc stimeout = req.getParameter("timeout");
27     int severity;
28     String JavaDoc error = "";
29
30     out.write("<html><head><title>Alarm Sender Servlet</title></head><body>");
31     if (level.startsWith("S")) {
32         severity = 1;
33     } else if (level.startsWith("W")) {
34         severity = 2;
35     } else {
36         severity = 3;
37     }
38     int loop = (new Integer JavaDoc(sloop)).intValue();
39     int timeout = (new Integer JavaDoc(stimeout)).intValue();
40     
41     Context JavaDoc ictx = null;
42     try {
43         ictx = new InitialContext JavaDoc();
44     } catch (Exception JavaDoc e) {
45         error += "Cannot get initial context:"+e;
46     }
47
48     // Lookup JMS resources
49
TopicConnectionFactory tcf = null;
50     Topic mytopic = null;
51     TopicConnection mytc = null;
52     try {
53         // lookup the TopicConnectionFactory through its JNDI name
54
tcf = (TopicConnectionFactory) ictx.lookup("java:comp/env/jms/TopicFactory");
55         // lookup the Topic through its JNDI name
56
mytopic = (Topic) ictx.lookup("java:comp/env/jms/AlarmTopic");
57     } catch (NamingException JavaDoc e) {
58         error += "Cannot lookup JMS Resources:"+ e;
59     }
60
61     // Create Connection
62
try {
63         mytc = tcf.createTopicConnection();
64      } catch (Exception JavaDoc e) {
65         error += "Cannot create JMS Connection:"+ e;
66     }
67
68     // Create Session + Publisher
69
TopicSession session = null;
70     TopicPublisher tp = null;
71     try {
72         session = mytc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
73         tp = session.createPublisher(mytopic);
74      } catch (Exception JavaDoc e) {
75         error += "Cannot create JMS Publisher:"+ e;
76     }
77
78     // publish messages to the topic
79
try {
80         for (int i = 0 ; i < loop; i++) {
81         if (timeout > 0) {
82             Thread.currentThread().sleep(1000*timeout);
83         }
84         MapMessage mess = session.createMapMessage();
85         mess.setInt("Severity", severity);
86         mess.setString("From", device);
87         mess.setString("Reason", message);
88         tp.publish(mess);
89         }
90     } catch (JMSException e) {
91         String JavaDoc l = e.getLinkedException().toString();
92         error += " Exception while creating or sending message: "+ e;
93         error += " Linked Exception: "+l;
94     } catch (java.lang.InterruptedException JavaDoc e) {
95         out.println("Alarm Generator Interrupted");
96     }
97
98     if (error.equals("")) {
99         out.println("Message sent");
100     } else {
101         out.println(error);
102     }
103     out.println("<p>return to <a HREF=\"../generator.html\">Alarm Generator</a>");
104     out.println("<p>go to <a HREF=\"list.jsp\">Alarm List</a>");
105
106     // close connection
107
try {
108         mytc.close();
109     } catch (Exception JavaDoc e) {
110     }
111
112     out.println("</body>");
113     out.println("</html>");
114     }
115 }
116
Popular Tags