1 package org.getahead.dwrdemo.gidemo; 2 3 import java.util.Collection ; 4 import java.util.Random ; 5 6 import javax.servlet.ServletContext ; 7 8 import org.directwebremoting.ServerContext; 9 import org.directwebremoting.ServerContextFactory; 10 import org.directwebremoting.WebContext; 11 import org.directwebremoting.WebContextFactory; 12 import org.directwebremoting.proxy.ScriptProxy; 13 import org.directwebremoting.util.Logger; 14 15 19 public class Publisher implements Runnable  20 { 21 24 public Publisher() 25 { 26 WebContext webContext = WebContextFactory.get(); 27 ServletContext servletContext = webContext.getServletContext(); 28 29 serverContext = ServerContextFactory.get(servletContext); 30 31 webContext.getScriptSessionsByPage(""); 37 38 synchronized (Publisher.class) 39 { 40 if (worker == null) 41 { 42 worker = new Thread (this, "Publisher"); 43 worker.start(); 44 } 45 } 46 } 47 48 51 public void run() 52 { 53 try 54 { 55 log.info("Starting Publisher thread"); 56 57 while (!Thread.currentThread().isInterrupted()) 58 { 59 Collection sessions = serverContext.getScriptSessionsByPage("/dwr/gi/index.html"); 60 ScriptProxy proxy = new ScriptProxy(sessions); 61 62 Corporation corp = corporations.getNextChangedCorporation(); 63 proxy.addFunctionCall("OpenAjax.publish", "gidemo", "corporation", corp); 64 65 int timeToSleep = random.nextInt(2500); 66 Thread.sleep(timeToSleep); 67 } 68 } 69 catch (InterruptedException ex) 70 { 71 } 73 finally 74 { 75 log.info("Stopping Publisher thread"); 76 } 77 } 78 79 82 protected static Thread worker; 83 84 87 private Corporations corporations = new Corporations(); 88 89 92 private ServerContext serverContext; 93 94 97 private Random random = new Random (); 98 99 102 private static final Logger log = Logger.getLogger(Publisher.class); 103 } 104
| Popular Tags
|