1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * 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   **/
18  package org.codehaus.activemq.web;
19  
20  import javax.jms.Destination;
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  import javax.jms.Session;
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  import java.util.Hashtable;
30  import java.util.Map;
31  
32  /***
33   * A servlet which will publish dummy market data prices
34   *
35   * @version $Revision: 1.1 $
36   */
37  public class PortfolioPublishServlet extends MessageServletSupport {
38  
39      private static final int maxDeltaPercent = 1;
40      private static final Map lastPrices = new Hashtable();
41  
42      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
43          PrintWriter out = response.getWriter();
44          String[] stocks = request.getParameterValues("stocks");
45          if (stocks == null || stocks.length == 0) {
46              out.println("<html><body>No <b>stocks</b> query parameter specified. Cannot publish market data</body></html>");
47          } else {
48              int count = getNumberOfMessages(request);
49              try {
50                  WebClient client = getWebClient(request);
51                  for (int i = 0; i < count; i++) {
52                      sendMessage(client, stocks);
53                  }
54                  out.println("<html><head><meta http-equiv='refresh' content='1'/></head>");
55                  out.println("<body>Published <b>" + count + "</b> price messages");
56                  out.println("</body></html>");
57  
58              } catch (JMSException e) {
59                  out.println("<html><body>Failed sending price messages due to <b>" + e + "</b></body></html>");
60                  log("Failed to send message: " + e, e);
61              }
62          }
63      }
64  
65      protected void sendMessage(WebClient client, String[] stocks) throws JMSException {
66          Session session = client.getSession();
67  
68          int idx = 0;
69          while (true) {
70              idx = (int) Math.round(stocks.length * Math.random());
71              if (idx < stocks.length) {
72                  break;
73              }
74          }
75          String stock = stocks[idx];
76          Destination destination = session.createTopic("STOCKS." + stock);
77          String stockText = createStockText(stock);
78          log("Sending: " + stockText);
79          Message message = session.createTextMessage(stockText);
80          client.send(destination, message);
81      }
82  
83      protected String createStockText(String stock) {
84          Double value = (Double) lastPrices.get(stock);
85          if (value == null) {
86              value = new Double(Math.random() * 100);
87          }
88  
89          // lets mutate the value by some percentage
90          double oldPrice = value.doubleValue();
91          value = new Double(mutatePrice(oldPrice));
92          lastPrices.put(stock, value);
93          double price = value.doubleValue();
94  
95          double offer = price * 1.001;
96          String movement = (price > oldPrice) ? "up" : "down";
97          return "<price stock='" + stock + "' bid='" + price + "' offer='" + offer + "' movement='" + movement + "'/>";
98  
99      }
100 
101     protected double mutatePrice(double price) {
102         double percentChange = (2 * Math.random() * maxDeltaPercent) - maxDeltaPercent;
103 
104         return price * (100 + percentChange) / 100;
105     }
106 
107     protected int getNumberOfMessages(HttpServletRequest request) {
108         String name = request.getParameter("count");
109         if (name != null) {
110             return Integer.parseInt(name);
111         }
112         return 1;
113     }
114 }