KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jms > test > simple > JMSTopicSenderReceiver


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jms.test.simple;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.jms.Message JavaDoc;
22 import javax.jms.Session JavaDoc;
23 import javax.jms.MessageListener JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Topic JavaDoc;
26 import javax.jms.TopicConnection JavaDoc;
27 import javax.jms.TopicConnectionFactory JavaDoc;
28 import javax.jms.TopicSubscriber JavaDoc;
29 import javax.jms.TopicPublisher JavaDoc;
30 import javax.jms.TopicSession JavaDoc;
31
32 import javax.jms.TextMessage JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.servlet.Servlet JavaDoc;
37 import javax.servlet.ServletConfig JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.http.HttpServlet JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.PrintWriter JavaDoc;
43
44 public class JMSTopicSenderReceiver extends HttpServlet JavaDoc implements Servlet JavaDoc {
45
46
47     Context JavaDoc initialContext = null;
48     TopicConnectionFactory JavaDoc tcf = null;
49     Topic JavaDoc topic = null;
50     String JavaDoc msg = null;
51
52     /* (non-Java-doc)
53      * @see javax.servlet.http.HttpServlet#HttpServlet()
54      */

55     public JMSTopicSenderReceiver() {
56         super();
57     }
58
59     /* (non-Java-doc)
60      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0, HttpServletResponse arg1)
61      */

62     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
63         doPost(request,response);
64     }
65
66     /* (non-Java-doc)
67      * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0, HttpServletResponse arg1)
68      */

69     protected void doPost(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws ServletException JavaDoc, IOException JavaDoc {
70
71         try {
72
73             String JavaDoc type = arg0.getParameter("type");
74             PrintWriter JavaDoc out = arg1.getWriter();
75
76             TopicConnection JavaDoc connection = tcf.createTopicConnection();
77             TopicSession JavaDoc session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
78             TopicSubscriber JavaDoc topicSubscriber = session.createSubscriber(topic);
79             TestListener test = new TestListener();
80             topicSubscriber.setMessageListener(test);
81             connection.start();
82             TopicPublisher JavaDoc topicPublisher = session.createPublisher(topic);
83             TextMessage JavaDoc tmsg = session.createTextMessage("JMS - Test Topic Message");
84             topicPublisher.publish(tmsg);
85             if ( msg != null ) {
86                 out.println("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>");
87                 out.println("<head><title>JMS Topic Sender Receiver</title></head>");
88                 out.println("<body>Received JMS Topic Message</body></html>");
89             }
90             else {
91                 out.println("<body>Did Not Receive JMS Topic Message</body></html>");
92             }
93
94             topicSubscriber.close();
95             session.close();
96             connection.stop();
97
98         }
99         catch ( Exception JavaDoc e ) {
100             e.printStackTrace();
101         }
102     }
103
104     /* (non-Java-doc)
105      * @see javax.servlet.Servlet#init(ServletConfig arg0)
106      */

107     public void init(ServletConfig JavaDoc arg0) throws ServletException JavaDoc {
108         try {
109             initialContext = new InitialContext JavaDoc();
110             tcf = (TopicConnectionFactory JavaDoc) initialContext.lookup("java:comp/env/jms/TCF");
111             topic = (Topic JavaDoc) initialContext.lookup("java:comp/env/jms/TestT");
112         }
113         catch ( NamingException JavaDoc e ) {
114             e.printStackTrace();
115         }
116     }
117
118     private class TestListener implements MessageListener JavaDoc {
119
120         public void onMessage(Message JavaDoc message)
121         {
122             try {
123                 TextMessage JavaDoc textMessage = (TextMessage JavaDoc)message;
124                 msg = textMessage.getText( );
125                 System.out.println("Message : "+msg);
126
127             }
128             catch ( JMSException JavaDoc jmse ) {
129                 jmse.printStackTrace( );
130             }
131         }
132     }
133 }
134
135
Popular Tags