001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 **/ 018 package org.activemq.web; 019 020 import javax.jms.Destination; 021 import javax.jms.JMSException; 022 import javax.jms.Message; 023 import javax.jms.Session; 024 import javax.servlet.ServletException; 025 import javax.servlet.http.HttpServletRequest; 026 import javax.servlet.http.HttpServletResponse; 027 import java.io.IOException; 028 import java.io.PrintWriter; 029 import java.util.Hashtable; 030 import java.util.Map; 031 032 /** 033 * A servlet which will publish dummy market data prices 034 * 035 * @version $Revision: 1.1.1.1 $ 036 */ 037 public class PortfolioPublishServlet extends MessageServletSupport { 038 039 private static final int maxDeltaPercent = 1; 040 private static final Map lastPrices = new Hashtable(); 041 private boolean ricoStyle = true; 042 043 044 public void init() throws ServletException { 045 super.init(); 046 047 ricoStyle = asBoolean(getServletConfig().getInitParameter("rico"), true); 048 } 049 050 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 051 PrintWriter out = response.getWriter(); 052 long sleep = 0; 053 String sleepText = request.getParameter("sleep"); 054 if (sleepText != null && sleepText.length() > 0) { 055 sleep = Long.parseLong(sleepText); 056 } 057 String[] stocks = request.getParameterValues("stocks"); 058 if (stocks == null || stocks.length == 0) { 059 out.println("<html><body>No <b>stocks</b> query parameter specified. Cannot publish market data</body></html>"); 060 } 061 else { 062 int count = getNumberOfMessages(request); 063 try { 064 WebClient client = getWebClient(request); 065 for (int i = 0; i < count; i++) { 066 if (i > 0 && sleep > 0) { 067 try { 068 Thread.sleep(sleep); 069 } 070 catch (InterruptedException e) { 071 // ignore 072 } 073 } 074 sendMessage(client, stocks); 075 } 076 out.print("<html><head><meta http-equiv='refresh' content='"); 077 String refreshRate = request.getParameter("refresh"); 078 if (refreshRate == null || refreshRate.length() == 0) { 079 refreshRate = "1"; 080 } 081 out.print(refreshRate); 082 out.println("'/></head>"); 083 out.println("<body>Published <b>" + count + "</b> price messages"); 084 out.println("</body></html>"); 085 086 } 087 catch (JMSException e) { 088 out.println("<html><body>Failed sending price messages due to <b>" + e + "</b></body></html>"); 089 log("Failed to send message: " + e, e); 090 } 091 } 092 } 093 094 protected void sendMessage(WebClient client, String[] stocks) throws JMSException { 095 Session session = client.getSession(); 096 097 int idx = 0; 098 while (true) { 099 idx = (int) Math.round(stocks.length * Math.random()); 100 if (idx < stocks.length) { 101 break; 102 } 103 } 104 String stock = stocks[idx]; 105 Destination destination = session.createTopic("STOCKS." + stock); 106 String stockText = createStockText(stock); 107 log("Sending: " + stockText + " on destination: " + destination); 108 Message message = session.createTextMessage(stockText); 109 client.send(destination, message); 110 } 111 112 protected String createStockText(String stock) { 113 Double value = (Double) lastPrices.get(stock); 114 if (value == null) { 115 value = new Double(Math.random() * 100); 116 } 117 118 // lets mutate the value by some percentage 119 double oldPrice = value.doubleValue(); 120 value = new Double(mutatePrice(oldPrice)); 121 lastPrices.put(stock, value); 122 double price = value.doubleValue(); 123 124 double offer = price * 1.001; 125 126 String movement = (price > oldPrice) ? "up" : "down"; 127 return "<price stock='" + stock + "' bid='" + price + "' offer='" + offer + "' movement='" + movement + "'/>"; 128 } 129 130 protected double mutatePrice(double price) { 131 double percentChange = (2 * Math.random() * maxDeltaPercent) - maxDeltaPercent; 132 133 return price * (100 + percentChange) / 100; 134 } 135 136 protected int getNumberOfMessages(HttpServletRequest request) { 137 String name = request.getParameter("count"); 138 if (name != null) { 139 return Integer.parseInt(name); 140 } 141 return 1; 142 } 143 }