1 16 package org.getahead.dwrdemo.clock; 17 18 import java.util.Collection ; 19 import java.util.Date ; 20 21 import javax.servlet.ServletContext ; 22 23 import org.directwebremoting.ServerContext; 24 import org.directwebremoting.ServerContextFactory; 25 import org.directwebremoting.WebContextFactory; 26 import org.directwebremoting.proxy.dwr.Util; 27 import org.directwebremoting.util.Logger; 28 29 32 public class Clock implements Runnable 33 { 34 37 public Clock() 38 { 39 ServletContext servletContext = WebContextFactory.get().getServletContext(); 40 sctx = ServerContextFactory.get(servletContext); 41 } 42 43 46 public synchronized void toggle() 47 { 48 active = !active; 49 50 if (active) 51 { 52 new Thread (this).start(); 53 } 54 } 55 56 59 public void run() 60 { 61 try 62 { 63 log.debug("CLOCK: Starting server-side thread"); 64 65 while (active) 66 { 67 Collection sessions = sctx.getScriptSessionsByPage("/dwr/clock/index.html"); 68 Util pages = new Util(sessions); 69 pages.setValue("clockDisplay", new Date ().toString()); 70 71 log.debug("Sent message"); 72 Thread.sleep(1000); 73 } 74 75 Collection sessions = sctx.getScriptSessionsByPage("/dwr/clock/index.html"); 76 Util pages = new Util(sessions); 77 pages.setValue("clockDisplay", ""); 78 79 log.debug("CLOCK: Stopping server-side thread"); 80 } 81 catch (InterruptedException ex) 82 { 83 ex.printStackTrace(); 84 } 85 } 86 87 90 private ServerContext sctx; 91 92 95 private transient boolean active = false; 96 97 100 private static final Logger log = Logger.getLogger(Clock.class); 101 } 102 | Popular Tags |