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