1 17 package org.apache.geronimo.jms.test.simple; 18 19 import java.io.IOException ; 20 import javax.jms.Message ; 21 import javax.jms.Queue ; 22 import javax.jms.QueueConnection ; 23 import javax.jms.QueueConnectionFactory ; 24 import javax.jms.QueueSender ; 25 import javax.jms.QueueSession ; 26 import javax.jms.Session ; 27 import javax.jms.TextMessage ; 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.servlet.Servlet ; 32 import javax.servlet.ServletConfig ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import java.io.PrintWriter ; 38 39 public class JMSQueueSender extends HttpServlet implements Servlet { 40 41 Context initialContext = null; 42 QueueConnectionFactory qcf = null; 43 Queue queue = null; 44 45 48 public JMSQueueSender() { 49 super(); 50 } 51 52 55 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 56 doPost(request,response); 57 } 58 59 62 protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException , IOException { 63 try { 64 65 PrintWriter out = arg1.getWriter(); 66 QueueConnection connection = qcf.createQueueConnection(); 67 connection.start(); 68 QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 69 QueueSender queueSender = session.createSender(queue); 70 TextMessage tmsg = session.createTextMessage("JMS - Test Queue Message"); 71 queueSender.send(tmsg); 72 queueSender.close(); 73 session.close(); 74 connection.stop(); 75 out.println("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>"); 76 out.println("<head><title>JMS Sender</title></head>"); 77 out.println("<body>Sent JMS Queue Message</body></html>"); 78 } 79 catch ( Exception e ) { 80 e.printStackTrace(); 81 } 82 83 } 84 85 88 public void init(ServletConfig arg0) throws ServletException { 89 90 try { 91 initialContext = new InitialContext (); 92 qcf = (QueueConnectionFactory ) initialContext.lookup("java:comp/env/jms/QCF"); 93 queue = (Queue ) initialContext.lookup("java:comp/env/jms/TestQ"); 94 } 95 catch ( NamingException e ) { 96 e.printStackTrace(); 97 } 98 99 } 100 101 } | Popular Tags |