KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > EventPullSource


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.core;
5
6 import nl.justobjects.pushlet.util.Log;
7
8
9 /**
10  * Abstract Event source from which Events are pulled.
11  *
12  * @version $Id: EventPullSource.java,v 1.14 2005/02/28 09:14:55 justb Exp $
13  * @author Just van den Broecke - Just Objects &copy;
14  **/

15
16 /** ABC for specifc EventPullSources. */
17 abstract public class EventPullSource implements EventSource, Runnable JavaDoc {
18     private volatile boolean alive = false;
19     private volatile boolean active = false;
20     private static int threadNum = 0;
21     private Thread JavaDoc thread;
22
23     public EventPullSource() {
24     }
25
26     abstract protected long getSleepTime();
27
28     abstract protected Event pullEvent();
29
30     public void start() {
31         thread = new Thread JavaDoc(this, "EventPullSource-" + (++threadNum));
32         thread.setDaemon(true);
33         thread.start();
34     }
35
36     public boolean isAlive() {
37         return alive;
38     }
39
40     /** Stop the event generator thread. */
41     public void stop() {
42         alive = false;
43
44         if (thread != null) {
45             thread.interrupt();
46             thread = null;
47         }
48
49     }
50
51     /** Activate the event generator thread. */
52     synchronized public void activate() {
53         if (active) {
54             return;
55         }
56         active = true;
57         if (!alive) {
58             start();
59             return;
60         }
61         Log.debug(getClass().getName() + ": notifying...");
62         notifyAll();
63     }
64
65     /** Deactivate the event generator thread. */
66     public void passivate() {
67         if (!active) {
68             return;
69         }
70         active = false;
71     }
72
73     /** Main loop: sleep, generate event and publish. */
74     public void run() {
75         Log.debug(getClass().getName() + ": starting...");
76         alive = true;
77         while (alive) {
78             try {
79
80                 Thread.sleep(getSleepTime());
81
82                 // Stopped during sleep: end loop.
83
if (!alive) {
84                     break;
85                 }
86
87                 // If passivated wait until we get
88
// get notify()-ied. If there are no subscribers
89
// it wasts CPU to remain producing events...
90
synchronized (this) {
91                     while (!active) {
92                         Log.debug(getClass().getName() + ": waiting...");
93                         wait();
94                     }
95                 }
96
97             } catch (InterruptedException JavaDoc e) {
98                 break;
99             }
100
101             try {
102                 // Derived class should produce an event.
103
Event event = pullEvent();
104
105                 // Let the publisher push it to subscribers.
106
Dispatcher.getInstance().multicast(event);
107             } catch (Throwable JavaDoc t) {
108                 Log.warn("EventPullSource exception while multicasting ", t);
109                 t.printStackTrace();
110             }
111         }
112         Log.debug(getClass().getName() + ": stopped");
113     }
114 }
115
116 /*
117   * $Log: EventPullSource.java,v $
118   * Revision 1.14 2005/02/28 09:14:55 justb
119   * sessmgr/dispatcher factory/singleton support
120   *
121   * Revision 1.13 2005/02/21 16:59:08 justb
122   * SessionManager and session lease introduced
123   *
124   * Revision 1.12 2005/02/21 11:50:46 justb
125   * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
126   *
127   * Revision 1.11 2005/02/18 10:07:23 justb
128   * many renamings of classes (make names compact)
129   *
130   * Revision 1.10 2005/02/18 09:54:15 justb
131   * refactor: rename Publisher Dispatcher and single Subscriber class
132   *
133   * Revision 1.9 2004/09/20 22:01:38 justb
134   * more changes for new protocol
135   *
136   * Revision 1.8 2004/09/03 22:35:37 justb
137   * Almost complete rewrite, just checking in now
138   *
139   * Revision 1.7 2004/08/15 16:00:15 justb
140   * enhancements to pull mode
141   *
142   * Revision 1.6 2004/08/13 23:36:05 justb
143   * rewrite of Pullet into Pushlet "pull" mode
144   *
145   * Revision 1.5 2004/03/10 14:01:55 justb
146   * formatting and *Subscriber refactoring
147   *
148   * Revision 1.4 2003/08/15 08:37:40 justb
149   * fix/add Copyright+LGPL file headers and footers
150   *
151   * Revision 1.3 2003/08/12 09:57:05 justb
152   * replaced all print statements to Log.*() calls
153   *
154   * Revision 1.2 2003/05/18 16:15:08 justb
155   * support for XML encoded Events
156   *
157   * Revision 1.1.1.1 2002/09/24 21:02:31 justb
158   * import to sourceforge
159   *
160   * Revision 1.1.1.1 2002/09/20 22:48:17 justb
161   * import to SF
162   *
163   * Revision 1.1.1.1 2002/09/20 14:19:03 justb
164   * first import into SF
165   *
166   * Revision 1.3 2002/04/15 20:42:41 just
167   * reformatting and renaming GuardedQueue to EventQueue
168   *
169   * Revision 1.2 2000/08/21 20:48:29 just
170   * added CVS log and id tags plus copyrights
171   *
172   *
173   */

174
Popular Tags