KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jms > test > simple > JMSQueueReceiver


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jms.test.simple;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.jms.Message JavaDoc;
22 import javax.jms.Queue JavaDoc;
23 import javax.jms.QueueConnection JavaDoc;
24 import javax.jms.QueueConnectionFactory JavaDoc;
25 import javax.jms.QueueReceiver JavaDoc;
26 import javax.jms.QueueSender JavaDoc;
27 import javax.jms.QueueSession JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.MessageListener JavaDoc;
30 import javax.jms.JMSException JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.servlet.Servlet JavaDoc;
36 import javax.servlet.ServletConfig JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.http.HttpServlet JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.io.PrintWriter JavaDoc;
42
43 public class JMSQueueReceiver extends HttpServlet JavaDoc implements Servlet JavaDoc {
44
45
46     Context JavaDoc initialContext = null;
47     QueueConnectionFactory JavaDoc qcf = null;
48     Queue JavaDoc queue = null;
49
50     /* (non-Java-doc)
51      * @see javax.servlet.http.HttpServlet#HttpServlet()
52      */

53     public JMSQueueReceiver() {
54         super();
55     }
56
57     /* (non-Java-doc)
58      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0, HttpServletResponse arg1)
59      */

60     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
61         doPost(request,response);
62     }
63
64     /* (non-Java-doc)
65      * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0, HttpServletResponse arg1)
66      */

67     protected void doPost(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws ServletException JavaDoc, IOException JavaDoc {
68
69         try {
70
71             PrintWriter JavaDoc out = arg1.getWriter();
72             QueueConnection JavaDoc connection = qcf.createQueueConnection();
73             connection.start();
74             QueueSession JavaDoc session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
75             QueueReceiver JavaDoc queueReceiver = session.createReceiver(queue);
76             Message JavaDoc msg = queueReceiver.receiveNoWait();
77
78             if ( msg instanceof TextMessage JavaDoc ) {
79                 TextMessage JavaDoc txtMsg = (TextMessage JavaDoc)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 JavaDoc e ) {
95             e.printStackTrace();
96         }
97     }
98
99     /* (non-Java-doc)
100      * @see javax.servlet.Servlet#init(ServletConfig arg0)
101      */

102     public void init(ServletConfig JavaDoc arg0) throws ServletException JavaDoc {
103         try {
104             initialContext = new InitialContext JavaDoc();
105             qcf = (QueueConnectionFactory JavaDoc) initialContext.lookup("java:comp/env/jms/QCF");
106             queue = (Queue JavaDoc) initialContext.lookup("java:comp/env/jms/TestQ");
107         }
108         catch ( NamingException JavaDoc e ) {
109             e.printStackTrace();
110         }
111     }
112
113 }
Popular Tags