diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-06-24 10:34:08 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-06-24 10:34:08 +0000 |
commit | 2d74722e60aa6fc98604ee4a555f6bb689d7f584 (patch) | |
tree | 9924f80d1516fb13e08f9be3a7ec9652188a3325 /sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample | |
parent | 08c4940824a4cdf84e9d1900a2d876a8d02950ec (diff) |
TUSCANY-3517: Apply latest updates to the async servelt sample in async-servlet-sca-0.0.3.zip
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@957497 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample')
-rw-r--r-- | sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServlet.java | 58 |
1 files changed, 47 insertions, 11 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(); } - + } - + } |