diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-06-24 10:50:21 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-06-24 10:50:21 +0000 |
commit | a8e57e028096e4ee3463a0d58fde1923ef6f96ae (patch) | |
tree | eab330087f030904a6c8498bbe0b7d27e903738e /sca-java-2.x/contrib/samples | |
parent | f865c76a194ed84161310b2f4e254700de033479 (diff) |
TUSCANY-3517: Apply latest updates from async-servlet-sca-0.0.4.zip
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@957504 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/contrib/samples')
3 files changed, 161 insertions, 134 deletions
diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java index e49a68120e..237f63c950 100644 --- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java +++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/StockServiceImpl.java @@ -26,21 +26,21 @@ import org.apache.log4j.Logger; public class StockServiceImpl implements StockService { - public static final int MAX_VALUE = 1000; - private Random random = new Random(new Date().getTime()); - private Logger logger = Logger.getLogger(StockServiceImpl.class); + public static final int MAX_VALUE = 1000; + private Random random = new Random(new Date().getTime()); + private Logger logger = Logger.getLogger(StockServiceImpl.class); - @Override - public String getSymbol() { - logger.debug("Getting symbol..."); - return "ASF"; - } + @Override + public String getSymbol() { + logger.debug("Getting symbol..."); + return "ASF"; + } - @Override - public Double getValue() { - logger.debug("Getting value..."); - Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE)); - return Double.valueOf(new DecimalFormat("#.##").format(value)); - } + @Override + public Double getValue() { + logger.debug("Getting value..."); + Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE)); + return Double.valueOf(new DecimalFormat("#.##").format(value)); + } } 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 dde6b83c34..5471d9a3c8 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 @@ -20,10 +20,9 @@ package sample; import java.io.IOException; import java.io.Writer; -import java.util.Timer; -import java.util.TimerTask; import javax.servlet.AsyncContext; +import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -31,95 +30,124 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; +import org.oasisopen.sca.ComponentContext; import org.oasisopen.sca.annotation.Reference; @WebServlet(asyncSupported = true, urlPatterns = "/stock") public class StockServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - private static final long MILLIS_PER_SECOND = 1000L; - - // @Reference - private StockService service = new StockServiceImpl(); - private AsyncContext asyncContext; - private Timer timer; - private long updatePeriod = 1L; // default 1 sec - private Logger logger = Logger.getLogger(StockServlet.class); - - protected void doGet(HttpServletRequest req, HttpServletResponse resp) - throws ServletException { - 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..."); - 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..."); - Writer writer = asyncContext.getResponse().getWriter(); - 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(); - } - } - - // ----------------------------------------------------------- - - public class StockTask extends TimerTask { - - @Override - public void run() { - updateClient(); - } - - } + private static final long serialVersionUID = 1L; + private static final long MILLIS_PER_SECOND = 1000L; + private static final long DEFAULT_UPDATE_INTERVAL = 1L; // default 1 sec + + @Reference + protected StockService service; + private Logger logger = Logger.getLogger(StockServlet.class); + + public void init(ServletConfig servletConfig) throws ServletException { + if (service == null) { + logger.debug("Service reference injection failed, using ComponentContext..."); + ComponentContext cc = + (ComponentContext)servletConfig.getServletContext().getAttribute("org.oasisopen.sca.ComponentContext"); + service = cc.getService(StockService.class, "service"); + } else { + logger.debug("Service reference succesfully injected."); + } + } + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { + logger.debug("Received request."); + logger.debug("Starting async context..."); + + AsyncContext 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("Starting updates..."); + if (req.getParameter("interval") != null) { + logger.debug("Using custom update interval value..."); + long updateInterval = Long.parseLong(req.getParameter("interval")); + new Thread(new StockTask(asyncContext, updateInterval * MILLIS_PER_SECOND)).start(); + } else { + logger.debug("Using default update interval value..."); + new Thread(new StockTask(asyncContext, DEFAULT_UPDATE_INTERVAL * MILLIS_PER_SECOND)).start(); + } + + logger.debug("Exiting doGet method..."); + } + + // ----------------------------------------------------------- + + public class StockTask implements Runnable { + + private AsyncContext asyncContext; + private long updateInterval; + + public StockTask(AsyncContext asyncContext, long updateInterval) { + super(); + this.asyncContext = asyncContext; + this.updateInterval = updateInterval; + } + + public void setAsyncContext(AsyncContext asyncContext) { + this.asyncContext = asyncContext; + } + + public void setUpdateInterval(long updateInterval) { + this.updateInterval = updateInterval; + } + + @Override + public void run() { + try { + while (true) { + logger.debug("Updating response..."); + Writer writer = asyncContext.getResponse().getWriter(); + writer.write("<script type=\"text/javascript\">\n"); + writer.write("updateData('" + service.getSymbol() + "#" + service.getValue() + "');\n"); + writer.write("</script>\n"); + writer.flush(); + asyncContext.getResponse().flushBuffer(); + logger.debug("Flushed response."); + Thread.sleep(updateInterval); + } + } catch (IOException e) { + logger.debug(e.getMessage(), e); + asyncContext.complete(); + } catch (InterruptedException e) { + logger.debug(e.getMessage(), e); + asyncContext.complete(); + } + } + } } diff --git a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java index 25857dac58..bc4d16dd27 100644 --- a/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java +++ b/sca-java-2.x/contrib/samples/async-servlet-sca/src/main/java/sample/TestServlet.java @@ -32,40 +32,39 @@ import org.apache.log4j.Logger; @WebServlet(asyncSupported = true, urlPatterns = "/test") public class TestServlet extends HttpServlet { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private AsyncContext asyncContext; - private Logger logger = Logger.getLogger(TestServlet.class); + private AsyncContext asyncContext; + private Logger logger = Logger.getLogger(TestServlet.class); - protected void doGet(HttpServletRequest req, HttpServletResponse resp) - throws ServletException { - logger.debug("Received test request."); - logger.debug("Gathering async context..."); - asyncContext = req.startAsync(); - resp.setContentType("text/plain"); + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { + logger.debug("Received test request."); + logger.debug("Gathering async context..."); + asyncContext = req.startAsync(); + resp.setContentType("text/plain"); - logger.debug("Gathering writer..."); - Writer writer; - try { - writer = asyncContext.getResponse().getWriter(); - for (int i = 0; i < 10; i++) { - Thread.sleep(1000); - logger.debug("Sending " + i + "..."); - writer.write("" + i + "\n"); - writer.flush(); - resp.flushBuffer(); - } + logger.debug("Gathering writer..."); + Writer writer; + try { + writer = asyncContext.getResponse().getWriter(); + for (int i = 0; i < 10; i++) { + Thread.sleep(1000); + logger.debug("Sending " + i + "..."); + writer.write("" + i + "\n"); + writer.flush(); + resp.flushBuffer(); + } - logger.debug("Completing response..."); - asyncContext.complete(); - writer.close(); - } catch (IOException e1) { - asyncContext.complete(); - e1.printStackTrace(); - } catch (InterruptedException e) { - asyncContext.complete(); - e.printStackTrace(); - } - } + logger.debug("Completing response..."); + asyncContext.complete(); + writer.close(); + } catch (IOException e1) { + asyncContext.complete(); + e1.printStackTrace(); + } catch (InterruptedException e) { + asyncContext.complete(); + e.printStackTrace(); + } + } -}
\ No newline at end of file +} |