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.activemq.web;
018
019 import java.io.IOException;
020 import java.io.PrintWriter;
021 import java.util.Hashtable;
022 import java.util.Map;
023
024 import javax.jms.Destination;
025 import javax.jms.JMSException;
026 import javax.jms.Message;
027 import javax.jms.Session;
028 import javax.servlet.ServletException;
029 import javax.servlet.http.HttpServletRequest;
030 import javax.servlet.http.HttpServletResponse;
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 MAX_DELTA_PERCENT = 1;
040 private static final Map<String, Double> LAST_PRICES = new Hashtable<String, Double>();
041
042 public void init() throws ServletException {
043 super.init();
044 }
045
046 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
047 PrintWriter out = response.getWriter();
048 String[] stocks = request.getParameterValues("stocks");
049 if (stocks == null || stocks.length == 0) {
050 out.println("<html><body>No <b>stocks</b> query parameter specified. Cannot publish market data</body></html>");
051 } else {
052 Integer total = (Integer)request.getSession(true).getAttribute("total");
053 if (total == null) {
054 total = Integer.valueOf(0);
055 }
056
057 int count = getNumberOfMessages(request);
058 total = Integer.valueOf(total.intValue() + count);
059 request.getSession().setAttribute("total", total);
060
061 try {
062 WebClient client = WebClient.getWebClient(request);
063 for (int i = 0; i < count; i++) {
064 sendMessage(client, stocks);
065 }
066 out.print("<html><head><meta http-equiv='refresh' content='");
067 String refreshRate = request.getParameter("refresh");
068 if (refreshRate == null || refreshRate.length() == 0) {
069 refreshRate = "1";
070 }
071 out.print(refreshRate);
072 out.println("'/></head>");
073 out.println("<body>Published <b>" + count + "</b> of " + total + " price messages. Refresh = " + refreshRate + "s");
074 out.println("</body></html>");
075
076 } catch (JMSException e) {
077 out.println("<html><body>Failed sending price messages due to <b>" + e + "</b></body></html>");
078 log("Failed to send message: " + e, e);
079 }
080 }
081 }
082
083 protected void sendMessage(WebClient client, String[] stocks) throws JMSException {
084 Session session = client.getSession();
085
086 int idx = 0;
087 while (true) {
088 idx = (int)Math.round(stocks.length * Math.random());
089 if (idx < stocks.length) {
090 break;
091 }
092 }
093 String stock = stocks[idx];
094 Destination destination = session.createTopic("STOCKS." + stock);
095 String stockText = createStockText(stock);
096 log("Sending: " + stockText + " on destination: " + destination);
097 Message message = session.createTextMessage(stockText);
098 client.send(destination, message);
099 }
100
101 protected String createStockText(String stock) {
102 Double value = LAST_PRICES.get(stock);
103 if (value == null) {
104 value = new Double(Math.random() * 100);
105 }
106
107 // lets mutate the value by some percentage
108 double oldPrice = value.doubleValue();
109 value = new Double(mutatePrice(oldPrice));
110 LAST_PRICES.put(stock, value);
111 double price = value.doubleValue();
112
113 double offer = price * 1.001;
114
115 String movement = (price > oldPrice) ? "up" : "down";
116 return "<price stock='" + stock + "' bid='" + price + "' offer='" + offer + "' movement='" + movement + "'/>";
117 }
118
119 protected double mutatePrice(double price) {
120 double percentChange = (2 * Math.random() * MAX_DELTA_PERCENT) - MAX_DELTA_PERCENT;
121
122 return price * (100 + percentChange) / 100;
123 }
124
125 protected int getNumberOfMessages(HttpServletRequest request) {
126 String name = request.getParameter("count");
127 if (name != null) {
128 return Integer.parseInt(name);
129 }
130 return 1;
131 }
132 }