KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > getahead > dwrdemo > gidemo > Publisher


1 package org.getahead.dwrdemo.gidemo;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Random JavaDoc;
5
6 import javax.servlet.ServletContext JavaDoc;
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 /**
16  * A generator of random objects to push to GI
17  * @author Joe Walker [joe at getahead dot ltd dot uk]
18  */

19 public class Publisher implements Runnable JavaDoc
20 {
21     /**
22      * Create a new publish thread and start it
23      */

24     public Publisher()
25     {
26         WebContext webContext = WebContextFactory.get();
27         ServletContext JavaDoc servletContext = webContext.getServletContext();
28
29         serverContext = ServerContextFactory.get(servletContext);
30
31         // A bit nasty: the call to serverContext.getScriptSessionsByPage()
32
// below could fail because the system might need to read web.xml which
33
// means it needs a ServletContext, which is only available using
34
// WebContext, which in turn requires a DWR thread. We can cache the
35
// results simply by calling this in a DWR thread, as we are now.
36
webContext.getScriptSessionsByPage("");
37
38         synchronized (Publisher.class)
39         {
40             if (worker == null)
41             {
42                 worker = new Thread JavaDoc(this, "Publisher");
43                 worker.start();
44             }
45         }
46     }
47
48     /* (non-Javadoc)
49      * @see java.lang.Runnable#run()
50      */

51     public void run()
52     {
53         try
54         {
55             log.info("Starting Publisher thread");
56
57             while (!Thread.currentThread().isInterrupted())
58             {
59                 Collection JavaDoc 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 JavaDoc ex)
70         {
71             // Ignore, we expect this
72
}
73         finally
74         {
75             log.info("Stopping Publisher thread");
76         }
77     }
78
79     /**
80      * The thread that does the work
81      */

82     protected static Thread JavaDoc worker;
83
84     /**
85      * The set of corporations that we manage
86      */

87     private Corporations corporations = new Corporations();
88
89     /**
90      * We use DWRs ServerContext to find users of a given page
91      */

92     private ServerContext serverContext;
93
94     /**
95      * Used to generate random data
96      */

97     private Random JavaDoc random = new Random JavaDoc();
98
99     /**
100      * The log stream
101      */

102     private static final Logger log = Logger.getLogger(Publisher.class);
103 }
104
Popular Tags