1 17 package org.apache.geronimo.jms.test.simple; 18 19 import java.io.IOException ; 20 21 import javax.jms.Message ; 22 import javax.jms.Session ; 23 import javax.jms.MessageListener ; 24 import javax.jms.JMSException ; 25 import javax.jms.Topic ; 26 import javax.jms.TopicConnection ; 27 import javax.jms.TopicConnectionFactory ; 28 import javax.jms.TopicSubscriber ; 29 import javax.jms.TopicPublisher ; 30 import javax.jms.TopicSession ; 31 32 import javax.jms.TextMessage ; 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 import javax.servlet.Servlet ; 37 import javax.servlet.ServletConfig ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.io.PrintWriter ; 43 44 public class JMSTopicSenderReceiver extends HttpServlet implements Servlet { 45 46 47 Context initialContext = null; 48 TopicConnectionFactory tcf = null; 49 Topic topic = null; 50 String msg = null; 51 52 55 public JMSTopicSenderReceiver() { 56 super(); 57 } 58 59 62 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 63 doPost(request,response); 64 } 65 66 69 protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException , IOException { 70 71 try { 72 73 String type = arg0.getParameter("type"); 74 PrintWriter out = arg1.getWriter(); 75 76 TopicConnection connection = tcf.createTopicConnection(); 77 TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 78 TopicSubscriber topicSubscriber = session.createSubscriber(topic); 79 TestListener test = new TestListener(); 80 topicSubscriber.setMessageListener(test); 81 connection.start(); 82 TopicPublisher topicPublisher = session.createPublisher(topic); 83 TextMessage 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 e ) { 100 e.printStackTrace(); 101 } 102 } 103 104 107 public void init(ServletConfig arg0) throws ServletException { 108 try { 109 initialContext = new InitialContext (); 110 tcf = (TopicConnectionFactory ) initialContext.lookup("java:comp/env/jms/TCF"); 111 topic = (Topic ) initialContext.lookup("java:comp/env/jms/TestT"); 112 } 113 catch ( NamingException e ) { 114 e.printStackTrace(); 115 } 116 } 117 118 private class TestListener implements MessageListener { 119 120 public void onMessage(Message message) 121 { 122 try { 123 TextMessage textMessage = (TextMessage )message; 124 msg = textMessage.getText( ); 125 System.out.println("Message : "+msg); 126 127 } 128 catch ( JMSException jmse ) { 129 jmse.printStackTrace( ); 130 } 131 } 132 } 133 } 134 135 | Popular Tags |