コード例 #1
0
 @Override
 public void tick(Stock stock) {
   ticks.add((Stock) stock.clone());
   Iterator<AsyncContext> it = clients.iterator();
   while (it.hasNext()) {
     AsyncContext actx = it.next();
     try {
       writeStock(actx, stock);
     } catch (Exception e) {
       // Ignore. The async error handling will deal with this.
     }
   }
 }
 public void writeStock(AsyncContext actx, Stock stock) {
   HttpServletResponse response = (HttpServletResponse) actx.getResponse();
   try {
     PrintWriter writer = response.getWriter();
     writer.write("STOCK#"); // make client parsing easier
     writer.write(stock.getSymbol());
     writer.write("#");
     writer.write(stock.getValueAsString());
     writer.write("#");
     writer.write(stock.getLastChangeAsString());
     writer.write("#");
     writer.write(String.valueOf(stock.getCnt()));
     writer.write("\n");
     writer.flush();
     response.flushBuffer();
   } catch (IOException x) {
     try {
       actx.complete();
     } catch (Exception ignore) {
       /* Ignore */
     }
   }
 }