diff options
Diffstat (limited to 'sca-java-2.x/contrib/samples/async-servlet-sca')
3 files changed, 78 insertions, 13 deletions
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java index 0c0cc6fb2d..dde6b83c34 100644 --- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java +++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java @@ -42,7 +42,7 @@ public class StockServlet extends HttpServlet { private StockService service = new StockServiceImpl(); private AsyncContext asyncContext; private Timer timer; - private long updatePeriod = 1L; // default 1 sec + private long updatePeriod = 1L; // default 1 sec private Logger logger = Logger.getLogger(StockServlet.class); protected void doGet(HttpServletRequest req, HttpServletResponse resp) @@ -50,40 +50,76 @@ public class StockServlet extends HttpServlet { logger.debug("Received request."); logger.debug("Starting async context..."); asyncContext = req.startAsync(); + resp.setContentType("text/html"); + + logger.debug("Sending initial response..."); + try { + Writer writer = asyncContext.getResponse().getWriter(); + writer.write("<html>\n"); + writer.write("<head>\n"); + writer.write("<title>Stock Monitor</title>\n"); + + writer.write("<script type=\"text/javascript\">\n"); + writer.write("function updateData(data) {\n"); + writer.write("var aux = data.split('#');\n"); + writer + .write("document.getElementById('symbol').textContent = aux[0];\n"); + writer + .write("document.getElementById('price').textContent = aux[1];\n"); + writer.write("}\n"); + writer.write("</script>\n"); + writer.write("</head>\n"); + writer.write("<body>\n"); + writer.write("<h2>Apache Tuscany Asynchronous Servlet Sample</h2>\n"); + writer.write("<h3>Stock Monitor</h3>\n"); + writer.write("<b><label>Company Symbol: </label></b>\n"); + writer.write("<span id=\"symbol\"></span>\n"); + writer.write("<b><label>Price: </label></b>\n"); + writer.write("<span id=\"price\"></span>\n"); + writer.write("</body>\n"); + writer.write("</html>\n"); + resp.flushBuffer(); + } catch (IOException e) { + e.printStackTrace(); + } + logger.debug("Service reference: " + service); logger.debug("Setting timer..."); timer = new Timer(); logger.debug("Setting update period..."); - this.updatePeriod = Long.parseLong(req.getParameter("period")); + if (req.getParameter("interval") != null) { + this.updatePeriod = Long.parseLong(req.getParameter("interval")); + } timer.scheduleAtFixedRate(new StockTask(), 0, updatePeriod * MILLIS_PER_SECOND); logger.debug("Exiting doGet method..."); } - + public void updateClient() { try { logger.debug("Updating response..."); - asyncContext.getResponse().setContentType("text/plain"); Writer writer = asyncContext.getResponse().getWriter(); - writer.write("Symbol: " + service.getSymbol() + ", Price " - + service.getValue() + "\n"); + writer.write("<script type=\"text/javascript\">\n"); + writer.write("updateData('" + service.getSymbol() + "#" + service.getValue() + "');\n"); + writer.write("</script>\n"); + // writer.write("Symbol: " + service.getSymbol() + ", Price: " + service.getValue() + "\n"); writer.flush(); asyncContext.getResponse().flushBuffer(); logger.debug("Flushed response."); } catch (IOException e) { logger.debug(e.getMessage(), e); - asyncContext.complete(); + asyncContext.complete(); } } - //----------------------------------------------------------- - + // ----------------------------------------------------------- + public class StockTask extends TimerTask { @Override public void run() { updateClient(); } - + } - + } diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/monitor.html b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/monitor.html new file mode 100644 index 0000000000..631ad1909b --- /dev/null +++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/monitor.html @@ -0,0 +1,29 @@ +<html> +<head> +<title>Stock Monitor</title> + +<script type="text/javascript"> +function updateData(data) { + var aux = data.split('#'); + document.getElementById('symbol').textContent = aux[0]; + document.getElementById('price').textContent = aux[1]; +} +</script> + +</head> +<body> +<h2>Apache Tuscany Asynchronous Servlet Sample</h2> + +<h3>Stock Monitor</h3> + +<b><label>Company Symbol: </label></b> +<span id="symbol">a</span> +<b><label>Price: </label></b> +<span id="price">1</span> + +</body> +</html> + +<script type="text/javascript"> + updateData('Apache#1234'); +</script> diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html index 9fc1d37791..3910434d33 100644 --- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html +++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/webapp/stock.html @@ -29,8 +29,8 @@ <h3>Stock Monitor</h3> <form action="stock" method="GET"> - <label>Enter the update period (seconds): </label> - <input type="text" name="period" size="2"/> + <label>Enter the update interval (seconds): </label> + <input type="text" name="interval" size="2"/> <br/> <button type="submit">Start monitoring</button> </form> |