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: import javax.jms.Message;
020: import javax.jms.Queue;
021: import javax.jms.QueueConnection;
022: import javax.jms.QueueConnectionFactory;
023: import javax.jms.QueueSender;
024: import javax.jms.QueueSession;
025: import javax.jms.Session;
026: import javax.jms.TextMessage;
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030: import javax.servlet.Servlet;
031: import javax.servlet.ServletConfig;
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpServlet;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036: import java.io.PrintWriter;
037:
038: public class JMSQueueSender extends HttpServlet implements Servlet {
039:
040: Context initialContext = null;
041: QueueConnectionFactory qcf = null;
042: Queue queue = null;
043:
044: /* (non-Java-doc)
045: * @see javax.servlet.http.HttpServlet#HttpServlet()
046: */
047: public JMSQueueSender() {
048: super ();
049: }
050:
051: /* (non-Java-doc)
052: * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0, HttpServletResponse arg1)
053: */
054: protected void doGet(HttpServletRequest request,
055: HttpServletResponse response) throws ServletException,
056: IOException {
057: doPost(request, response);
058: }
059:
060: /* (non-Java-doc)
061: * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0, HttpServletResponse arg1)
062: */
063: protected void doPost(HttpServletRequest arg0,
064: HttpServletResponse arg1) throws ServletException,
065: IOException {
066: try {
067:
068: PrintWriter out = arg1.getWriter();
069: QueueConnection connection = qcf.createQueueConnection();
070: connection.start();
071: QueueSession session = connection.createQueueSession(false,
072: Session.AUTO_ACKNOWLEDGE);
073: QueueSender queueSender = session.createSender(queue);
074: TextMessage tmsg = session
075: .createTextMessage("JMS - Test Queue Message");
076: queueSender.send(tmsg);
077: queueSender.close();
078: session.close();
079: connection.stop();
080: out
081: .println("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>");
082: out.println("<head><title>JMS Sender</title></head>");
083: out.println("<body>Sent JMS Queue Message</body></html>");
084: } catch (Exception e) {
085: e.printStackTrace();
086: }
087:
088: }
089:
090: /* (non-Java-doc)
091: * @see javax.servlet.Servlet#init(ServletConfig arg0)
092: */
093: public void init(ServletConfig arg0) throws ServletException {
094:
095: try {
096: initialContext = new InitialContext();
097: qcf = (QueueConnectionFactory) initialContext
098: .lookup("java:comp/env/jms/QCF");
099: queue = (Queue) initialContext
100: .lookup("java:comp/env/jms/TestQ");
101: } catch (NamingException e) {
102: e.printStackTrace();
103: }
104:
105: }
106:
107: }
|