1 18 package org.apache.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 37 public class PortfolioPublishServlet extends MessageServletSupport { 38 39 private static final int maxDeltaPercent = 1; 40 private static final Map lastPrices = new Hashtable (); 41 private boolean ricoStyle = true; 42 43 44 public void init() throws ServletException { 45 super.init(); 46 47 ricoStyle = asBoolean(getServletConfig().getInitParameter("rico"), true); 48 } 49 50 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 51 PrintWriter out = response.getWriter(); 52 String [] 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 total=(Integer )request.getSession(true).getAttribute("total"); 58 if (total==null) 59 total=new Integer (0); 60 61 62 int count = getNumberOfMessages(request); 63 total=new Integer (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 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 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 [] stocks) throws JMSException { 90 Session 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 stock = stocks[idx]; 100 Destination destination = session.createTopic("STOCKS." + stock); 101 String stockText = createStockText(stock); 102 log("Sending: " + stockText + " on destination: " + destination); 103 Message message = session.createTextMessage(stockText); 104 client.send(destination, message); 105 } 106 107 protected String createStockText(String stock) { 108 Double value = (Double ) lastPrices.get(stock); 109 if (value == null) { 110 value = new Double (Math.random() * 100); 111 } 112 113 double oldPrice = value.doubleValue(); 115 value = new Double (mutatePrice(oldPrice)); 116 lastPrices.put(stock, value); 117 double price = value.doubleValue(); 118 119 double offer = price * 1.001; 120 121 String 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 request) { 132 String name = request.getParameter("count"); 133 if (name != null) { 134 return Integer.parseInt(name); 135 } 136 return 1; 137 } 138 } 139 | Popular Tags |