| EventPullSource.java |
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 * Abstract Event source from which Events are pulled.
10 *
11 * @version $Id: EventPullSource.java,v 1.15 2007/11/23 14:33:07 justb Exp $
12 * @author Just van den Broecke - Just Objects ©
13 **/
14
15/**
16 * ABC for specifc EventPullSources.
17 */
18abstract public class EventPullSource implements EventSource, Runnable {
19 private volatile boolean alive = false;
20 private volatile boolean active = false;
21 private static int threadNum = 0;
22 private Thread thread;
23
24 public EventPullSource() {
25 }
26
27 abstract protected long getSleepTime();
28
29 abstract protected Event pullEvent();
30
31 public void start() {
32 thread = new Thread(this, "EventPullSource-" + (++threadNum));
33 thread.setDaemon(true);
34 thread.start();
35 }
36
37 public boolean isAlive() {
38 return alive;
39 }
40
41 /**
42 * Stop the event generator thread.
43 */
44 public void stop() {
45 alive = false;
46
47 if (thread != null) {
48 thread.interrupt();
49 thread = null;
50 }
51
52 }
53
54 /**
55 * Activate the event generator thread.
56 */
57 synchronized public void activate() {
58 if (active) {
59 return;
60 }
61 active = true;
62 if (!alive) {
63 start();
64 return;
65 }
66 Log.debug(getClass().getName() + ": notifying...");
67 notifyAll();
68 }
69
70 /**
71 * Deactivate the event generator thread.
72 */
73 public void passivate() {
74 if (!active) {
75 return;
76 }
77 active = false;
78 }
79
80 /**
81 * Main loop: sleep, generate event and publish.
82 */
83 public void run() {
84 Log.debug(getClass().getName() + ": starting...");
85 alive = true;
86 while (alive) {
87 try {
88
89 Thread.sleep(getSleepTime());
90
91 // Stopped during sleep: end loop.
92 if (!alive) {
93 break;
94 }
95
96 // If passivated wait until we get
97 // get notify()-ied. If there are no subscribers
98 // it wasts CPU to remain producing events...
99 synchronized (this) {
00 while (!active) {
01 Log.debug(getClass().getName() + ": waiting...");
02 wait();
03 }
04 }
05
06 } catch (InterruptedException e) {
07 break;
08 }
09
10 try {
11 // Derived class should produce an event.
12 Event event = pullEvent();
13
14 // Let the publisher push it to subscribers.
15 Dispatcher.getInstance().multicast(event);
16 } catch (Throwable t) {
17 Log.warn("EventPullSource exception while multicasting ", t);
18 t.printStackTrace();
19 }
20 }
21 Log.debug(getClass().getName() + ": stopped");
22 }
23}
24
25/*
26 * $Log: EventPullSource.java,v $
27 * Revision 1.15 2007/11/23 14:33:07 justb
28 * core classes now configurable through factory
29 *
30 * Revision 1.14 2005/02/28 09:14:55 justb
31 * sessmgr/dispatcher factory/singleton support
32 *
33 * Revision 1.13 2005/02/21 16:59:08 justb
34 * SessionManager and session lease introduced
35 *
36 * Revision 1.12 2005/02/21 11:50:46 justb
37 * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
38 *
39 * Revision 1.11 2005/02/18 10:07:23 justb
40 * many renamings of classes (make names compact)
41 *
42 * Revision 1.10 2005/02/18 09:54:15 justb
43 * refactor: rename Publisher Dispatcher and single Subscriber class
44 *
45 * Revision 1.9 2004/09/20 22:01:38 justb
46 * more changes for new protocol
47 *
48 * Revision 1.8 2004/09/03 22:35:37 justb
49 * Almost complete rewrite, just checking in now
50 *
51 * Revision 1.7 2004/08/15 16:00:15 justb
52 * enhancements to pull mode
53 *
54 * Revision 1.6 2004/08/13 23:36:05 justb
55 * rewrite of Pullet into Pushlet "pull" mode
56 *
57 * Revision 1.5 2004/03/10 14:01:55 justb
58 * formatting and *Subscriber refactoring
59 *
60 * Revision 1.4 2003/08/15 08:37:40 justb
61 * fix/add Copyright+LGPL file headers and footers
62 *
63 * Revision 1.3 2003/08/12 09:57:05 justb
64 * replaced all print statements to Log.*() calls
65 *
66 * Revision 1.2 2003/05/18 16:15:08 justb
67 * support for XML encoded Events
68 *
69 * Revision 1.1.1.1 2002/09/24 21:02:31 justb
70 * import to sourceforge
71 *
72 * Revision 1.1.1.1 2002/09/20 22:48:17 justb
73 * import to SF
74 *
75 * Revision 1.1.1.1 2002/09/20 14:19:03 justb
76 * first import into SF
77 *
78 * Revision 1.3 2002/04/15 20:42:41 just
79 * reformatting and renaming GuardedQueue to EventQueue
80 *
81 * Revision 1.2 2000/08/21 20:48:29 just
82 * added CVS log and id tags plus copyrights
83 *
84 *
85 */
86| EventPullSource.java |