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