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     */
017    package org.apache.geronimo.samples.daytrader.web.prims;
018    
019    import java.io.IOException;
020    
021    import javax.jms.Connection;
022    import javax.jms.ConnectionFactory;
023    import javax.jms.MessageProducer;
024    import javax.jms.Queue;
025    import javax.jms.Session;
026    import javax.jms.TextMessage;
027    import javax.naming.InitialContext;
028    import javax.naming.NamingException;
029    import javax.servlet.ServletConfig;
030    import javax.servlet.ServletException;
031    import javax.servlet.http.HttpServlet;
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    import org.apache.geronimo.samples.daytrader.TradeConfig;
036    import org.apache.geronimo.samples.daytrader.util.Log;
037    
038    /**
039     * This primitive is designed to run inside the TradeApplication and relies upon
040     * the {@link org.apache.geronimo.samples.daytrader.TradeConfig} class to set configuration parameters.
041     * PingServlet2MDBQueue tests key functionality of a servlet call to a
042     * post a message to an MDB Queue. The TradeBrokerMDB receives the message
043     * This servlet makes use of the MDB EJB {@link org.apache.geronimo.samples.daytrader.ejb.TradeBrokerMDB}
044     * by posting a message to the MDB Queue
045     */
046    public class PingServlet2MDBQueue extends HttpServlet {
047    
048        private static String initTime;
049        private static int hitCount;
050        private static ConnectionFactory connFactory;
051        private static Queue queue;
052    
053        /**
054         * forwards post requests to the doGet method
055         * Creation date: (11/6/2000 10:52:39 AM)
056         *
057         * @param req javax.servlet.http.HttpServletRequest
058         * @param res javax.servlet.http.HttpServletResponse
059         */
060        public void doPost(HttpServletRequest req, HttpServletResponse res)
061                throws ServletException, IOException {
062            doGet(req, res);
063        }
064    
065    
066        /**
067         * this is the main method of the servlet that will service all get requests.
068         *
069         * @param req HttpServletRequest
070         * @param res HttpServletResponce
071         */
072    
073        public void doGet(HttpServletRequest req, HttpServletResponse res)
074                throws IOException, ServletException {
075    
076            res.setContentType("text/html");
077            java.io.PrintWriter out = res.getWriter();
078            //use a stringbuffer to avoid concatenation of Strings
079            StringBuffer output = new StringBuffer(100);
080            output.append(
081                    "<html><head><title>PingServlet2MDBQueue</title></head>"
082                            + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2MDBQueue<BR></FONT>"
083                            + "<FONT size=\"-1\" color=\"#000066\">"
084                            + "Tests the basic operation of a servlet posting a message to an EJB MDB through a JMS Queue.<BR>"
085                            + "<FONT color=\"red\"><B>Note:</B> Not intended for performance testing.</FONT>");
086    
087            try {
088    
089                Connection conn = connFactory.createConnection();
090    
091                try {
092                    TextMessage message = null;
093                    int iter = TradeConfig.getPrimIterations();
094                    for (int ii = 0; ii < iter; ii++) {
095                        Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
096                        try {
097                            MessageProducer producer = sess.createProducer(queue);
098    
099                            message = sess.createTextMessage();
100    
101                            String command = "ping";
102                            message.setStringProperty("command", command);
103                            message.setLongProperty("publishTime", System.currentTimeMillis());
104                            message.setText("Ping message for queue java:comp/env/jms/TradeBrokerQueue sent from PingServlet2MDBQueue at " + new java.util.Date());
105                            producer.send(message);
106                        } finally {
107                            sess.close();
108                        }
109                    }
110    
111                    //write out the output
112                    output.append("<HR>initTime: ").append(initTime);
113                    output.append("<BR>Hit Count: ").append(hitCount++);
114                    output.append("<HR>Posted Text message to java:comp/env/jms/TradeBrokerQueue destination");
115                    output.append("<BR>Message: ").append(message);
116                    output.append("<BR><BR>Message text: ").append(message.getText());
117                    output.append(
118                            "<BR><HR></FONT></BODY></HTML>");
119                    out.println(output.toString());
120    
121                }
122                catch (Exception e) {
123                    Log.error("PingServlet2MDBQueue.doGet(...):exception posting message to TradeBrokerQueue destination ");
124                    throw e;
125                } finally {
126                    conn.close();
127                }
128            } //this is where I actually handle the exceptions
129            catch (Exception e) {
130                Log.error(e, "PingServlet2MDBQueue.doGet(...): error");
131                res.sendError(500, "PingServlet2MDBQueue.doGet(...): error, " + e.toString());
132    
133            }
134        }
135    
136    
137        /**
138         * returns a string of information about the servlet
139         *
140         * @return info String: contains info about the servlet
141         */
142    
143        public String getServletInfo() {
144            return "web primitive, configured with trade runtime configs, tests Servlet to Session EJB path";
145    
146        }
147    
148        /**
149         * called when the class is loaded to initialize the servlet
150         *
151         * @param config ServletConfig:
152         */
153        public void init(ServletConfig config) throws ServletException {
154            super.init(config);
155            hitCount = 0;
156            initTime = new java.util.Date().toString();
157            try {
158                InitialContext context = new InitialContext();
159                connFactory = (ConnectionFactory) context.lookup("java:comp/env/jms/QueueConnectionFactory");
160                queue = (Queue) context.lookup("java:comp/env/jms/TradeBrokerQueue");
161            } catch (NamingException e) {
162                Log.error("PingServlet2MDBQueue:init() -- error on intialization of JMS factories, queues", e);
163                throw new ServletException(e);
164            }
165        }
166    
167    }