001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jms.test.simple;
017:
018: import java.io.IOException;
019:
020: import javax.jms.Message;
021: import javax.jms.Session;
022: import javax.jms.MessageListener;
023: import javax.jms.JMSException;
024: import javax.jms.Topic;
025: import javax.jms.TopicConnection;
026: import javax.jms.TopicConnectionFactory;
027: import javax.jms.TopicSubscriber;
028: import javax.jms.TopicPublisher;
029: import javax.jms.TopicSession;
030:
031: import javax.jms.TextMessage;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035: import javax.servlet.Servlet;
036: import javax.servlet.ServletConfig;
037: import javax.servlet.ServletException;
038: import javax.servlet.http.HttpServlet;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041: import java.io.PrintWriter;
042:
043: public class JMSTopicSenderReceiver extends HttpServlet implements
044: Servlet {
045:
046: Context initialContext = null;
047: TopicConnectionFactory tcf = null;
048: Topic topic = null;
049: String msg = null;
050:
051: /* (non-Java-doc)
052: * @see javax.servlet.http.HttpServlet#HttpServlet()
053: */
054: public JMSTopicSenderReceiver() {
055: super ();
056: }
057:
058: /* (non-Java-doc)
059: * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0, HttpServletResponse arg1)
060: */
061: protected void doGet(HttpServletRequest request,
062: HttpServletResponse response) throws ServletException,
063: IOException {
064: doPost(request, response);
065: }
066:
067: /* (non-Java-doc)
068: * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0, HttpServletResponse arg1)
069: */
070: protected void doPost(HttpServletRequest arg0,
071: HttpServletResponse arg1) throws ServletException,
072: IOException {
073:
074: try {
075:
076: String type = arg0.getParameter("type");
077: PrintWriter out = arg1.getWriter();
078:
079: TopicConnection connection = tcf.createTopicConnection();
080: TopicSession session = connection.createTopicSession(false,
081: Session.AUTO_ACKNOWLEDGE);
082: TopicSubscriber topicSubscriber = session
083: .createSubscriber(topic);
084: TestListener test = new TestListener();
085: topicSubscriber.setMessageListener(test);
086: connection.start();
087: TopicPublisher topicPublisher = session
088: .createPublisher(topic);
089: TextMessage tmsg = session
090: .createTextMessage("JMS - Test Topic Message");
091: topicPublisher.publish(tmsg);
092: if (msg != null) {
093: out
094: .println("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>");
095: out
096: .println("<head><title>JMS Topic Sender Receiver</title></head>");
097: out
098: .println("<body>Received JMS Topic Message</body></html>");
099: } else {
100: out
101: .println("<body>Did Not Receive JMS Topic Message</body></html>");
102: }
103:
104: topicSubscriber.close();
105: session.close();
106: connection.stop();
107:
108: } catch (Exception e) {
109: e.printStackTrace();
110: }
111: }
112:
113: /* (non-Java-doc)
114: * @see javax.servlet.Servlet#init(ServletConfig arg0)
115: */
116: public void init(ServletConfig arg0) throws ServletException {
117: try {
118: initialContext = new InitialContext();
119: tcf = (TopicConnectionFactory) initialContext
120: .lookup("java:comp/env/jms/TCF");
121: topic = (Topic) initialContext
122: .lookup("java:comp/env/jms/TestT");
123: } catch (NamingException e) {
124: e.printStackTrace();
125: }
126: }
127:
128: private class TestListener implements MessageListener {
129:
130: public void onMessage(Message message) {
131: try {
132: TextMessage textMessage = (TextMessage) message;
133: msg = textMessage.getText();
134: System.out.println("Message : " + msg);
135:
136: } catch (JMSException jmse) {
137: jmse.printStackTrace();
138: }
139: }
140: }
141: }
|