KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > PortfolioPublishServlet


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.web;
19
20 import javax.jms.Destination JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.Session JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * A servlet which will publish dummy market data prices
34  *
35  * @version $Revision: 1.1.1.1 $
36  */

37 public class PortfolioPublishServlet extends MessageServletSupport {
38
39     private static final int maxDeltaPercent = 1;
40     private static final Map JavaDoc lastPrices = new Hashtable JavaDoc();
41     private boolean ricoStyle = true;
42
43     
44     public void init() throws ServletException JavaDoc {
45         super.init();
46         
47         ricoStyle = asBoolean(getServletConfig().getInitParameter("rico"), true);
48     }
49
50     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
51         PrintWriter JavaDoc out = response.getWriter();
52         String JavaDoc[] stocks = request.getParameterValues("stocks");
53         if (stocks == null || stocks.length == 0) {
54             out.println("<html><body>No <b>stocks</b> query parameter specified. Cannot publish market data</body></html>");
55         }
56         else {
57             Integer JavaDoc total=(Integer JavaDoc)request.getSession(true).getAttribute("total");
58             if (total==null)
59                 total=new Integer JavaDoc(0);
60             
61             
62             int count = getNumberOfMessages(request);
63             total=new Integer JavaDoc(total.intValue()+count);
64             request.getSession().setAttribute("total",total);
65             
66             try {
67                 WebClient client = WebClient.getWebClient(request);
68                 for (int i = 0; i < count; i++) {
69                     sendMessage(client, stocks);
70                 }
71                 out.print("<html><head><meta http-equiv='refresh' content='");
72                 String JavaDoc refreshRate = request.getParameter("refresh");
73                 if (refreshRate == null || refreshRate.length() == 0) {
74                     refreshRate = "1";
75                 }
76                 out.print(refreshRate);
77                 out.println("'/></head>");
78                 out.println("<body>Published <b>" + count + "</b> of "+total+ " price messages. Refresh = "+refreshRate+"s");
79                 out.println("</body></html>");
80
81             }
82             catch (JMSException JavaDoc e) {
83                 out.println("<html><body>Failed sending price messages due to <b>" + e + "</b></body></html>");
84                 log("Failed to send message: " + e, e);
85             }
86         }
87     }
88
89     protected void sendMessage(WebClient client, String JavaDoc[] stocks) throws JMSException JavaDoc {
90         Session JavaDoc session = client.getSession();
91
92         int idx = 0;
93         while (true) {
94             idx = (int) Math.round(stocks.length * Math.random());
95             if (idx < stocks.length) {
96                 break;
97             }
98         }
99         String JavaDoc stock = stocks[idx];
100         Destination JavaDoc destination = session.createTopic("STOCKS." + stock);
101         String JavaDoc stockText = createStockText(stock);
102         log("Sending: " + stockText + " on destination: " + destination);
103         Message JavaDoc message = session.createTextMessage(stockText);
104         client.send(destination, message);
105     }
106
107     protected String JavaDoc createStockText(String JavaDoc stock) {
108         Double JavaDoc value = (Double JavaDoc) lastPrices.get(stock);
109         if (value == null) {
110             value = new Double JavaDoc(Math.random() * 100);
111         }
112
113         // lets mutate the value by some percentage
114
double oldPrice = value.doubleValue();
115         value = new Double JavaDoc(mutatePrice(oldPrice));
116         lastPrices.put(stock, value);
117         double price = value.doubleValue();
118
119         double offer = price * 1.001;
120
121         String JavaDoc movement = (price > oldPrice) ? "up" : "down";
122         return "<price stock='" + stock + "' bid='" + price + "' offer='" + offer + "' movement='" + movement + "'/>";
123     }
124
125     protected double mutatePrice(double price) {
126         double percentChange = (2 * Math.random() * maxDeltaPercent) - maxDeltaPercent;
127
128         return price * (100 + percentChange) / 100;
129     }
130
131     protected int getNumberOfMessages(HttpServletRequest JavaDoc request) {
132         String JavaDoc name = request.getParameter("count");
133         if (name != null) {
134             return Integer.parseInt(name);
135         }
136         return 1;
137     }
138 }
139
Popular Tags