KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.jms.Message JavaDoc;
21 import javax.jms.Queue JavaDoc;
22 import javax.jms.QueueConnection JavaDoc;
23 import javax.jms.QueueConnectionFactory JavaDoc;
24 import javax.jms.QueueSender JavaDoc;
25 import javax.jms.QueueSession JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.servlet.Servlet JavaDoc;
32 import javax.servlet.ServletConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38
39 public class JMSQueueSender extends HttpServlet JavaDoc implements Servlet JavaDoc {
40
41     Context JavaDoc initialContext = null;
42     QueueConnectionFactory JavaDoc qcf = null;
43     Queue JavaDoc queue = null;
44
45     /* (non-Java-doc)
46      * @see javax.servlet.http.HttpServlet#HttpServlet()
47      */

48     public JMSQueueSender() {
49         super();
50     }
51
52     /* (non-Java-doc)
53      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0, HttpServletResponse arg1)
54      */

55     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
56         doPost(request,response);
57     }
58
59     /* (non-Java-doc)
60      * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0, HttpServletResponse arg1)
61      */

62     protected void doPost(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws ServletException JavaDoc, IOException JavaDoc {
63         try {
64
65             PrintWriter JavaDoc out = arg1.getWriter();
66             QueueConnection JavaDoc connection = qcf.createQueueConnection();
67             connection.start();
68             QueueSession JavaDoc session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
69             QueueSender JavaDoc queueSender = session.createSender(queue);
70             TextMessage JavaDoc 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 JavaDoc e ) {
80             e.printStackTrace();
81         }
82
83     }
84
85     /* (non-Java-doc)
86      * @see javax.servlet.Servlet#init(ServletConfig arg0)
87      */

88     public void init(ServletConfig JavaDoc arg0) throws ServletException JavaDoc {
89
90         try {
91             initialContext = new InitialContext JavaDoc();
92             qcf = (QueueConnectionFactory JavaDoc) initialContext.lookup("java:comp/env/jms/QCF");
93             queue = (Queue JavaDoc) initialContext.lookup("java:comp/env/jms/TestQ");
94         }
95         catch ( NamingException JavaDoc e ) {
96             e.printStackTrace();
97         }
98
99     }
100
101 }
Popular Tags