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.Queue ; 23 import javax.jms.QueueConnection ; 24 import javax.jms.QueueConnectionFactory ; 25 import javax.jms.QueueReceiver ; 26 import javax.jms.QueueSender ; 27 import javax.jms.QueueSession ; 28 import javax.jms.Session ; 29 import javax.jms.MessageListener ; 30 import javax.jms.JMSException ; 31 import javax.jms.TextMessage ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 import javax.servlet.Servlet ; 36 import javax.servlet.ServletConfig ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.http.HttpServlet ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.io.PrintWriter ; 42 43 public class JMSQueueReceiver extends HttpServlet implements Servlet { 44 45 46 Context initialContext = null; 47 QueueConnectionFactory qcf = null; 48 Queue queue = null; 49 50 53 public JMSQueueReceiver() { 54 super(); 55 } 56 57 60 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 61 doPost(request,response); 62 } 63 64 67 protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException , IOException { 68 69 try { 70 71 PrintWriter out = arg1.getWriter(); 72 QueueConnection connection = qcf.createQueueConnection(); 73 connection.start(); 74 QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 75 QueueReceiver queueReceiver = session.createReceiver(queue); 76 Message msg = queueReceiver.receiveNoWait(); 77 78 if ( msg instanceof TextMessage ) { 79 TextMessage txtMsg = (TextMessage )msg; 80 System.out.println("Message : "+txtMsg.getText()); 81 out.println("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>"); 82 out.println("<head><title>JMS Receiver</title></head>"); 83 out.println("<body>Received JMS Queue Message</body></html>"); 84 } 85 else { 86 out.println("<body>Did Not Receive JMS Queue Message</body></html>"); 87 } 88 89 queueReceiver.close(); 90 session.close(); 91 connection.stop(); 92 93 } 94 catch ( Exception e ) { 95 e.printStackTrace(); 96 } 97 } 98 99 102 public void init(ServletConfig arg0) throws ServletException { 103 try { 104 initialContext = new InitialContext (); 105 qcf = (QueueConnectionFactory ) initialContext.lookup("java:comp/env/jms/QCF"); 106 queue = (Queue ) initialContext.lookup("java:comp/env/jms/TestQ"); 107 } 108 catch ( NamingException e ) { 109 e.printStackTrace(); 110 } 111 } 112 113 } | Popular Tags |