|
3 | 3 | import java.util.*;
|
4 | 4 | import javaxt.utils.Value;
|
5 | 5 | import javaxt.express.utils.DateUtils;
|
| 6 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 7 | + |
6 | 8 |
|
7 | 9 | //******************************************************************************
|
8 | 10 | //** NotificationService
|
|
14 | 16 |
|
15 | 17 | public class NotificationService {
|
16 | 18 |
|
17 |
| -private static List events = null; |
18 |
| -private static List<Listener> listeners; |
| 19 | +private static List events = new LinkedList(); |
| 20 | +private static List<Listener> listeners = new LinkedList(); |
| 21 | +private static AtomicBoolean isRunning = new AtomicBoolean(false); |
19 | 22 |
|
20 | 23 |
|
21 | 24 | //**************************************************************************
|
@@ -27,7 +30,7 @@ public class NotificationService {
|
27 | 30 | * @param data Additional information related to the event (e.g. User ID)
|
28 | 31 | */
|
29 | 32 | public static void notify(String event, String model, Value data){
|
30 |
| -if (events==null) return; |
| 33 | +if (!isRunning.get()) return; |
31 | 34 |
|
32 | 35 | if (data==null) data = new Value(null);
|
33 | 36 |
|
@@ -49,15 +52,14 @@ public static void start(){
|
49 | 52 | }
|
50 | 53 |
|
51 | 54 | public static void start(int numThreads){
|
52 |
| -if (events!=null) return; |
| 55 | +if (isRunning.get()) return; |
53 | 56 |
|
54 |
| -listeners = new LinkedList(); |
55 | 57 |
|
56 |
| -events = new LinkedList(); |
57 | 58 | for (int i=0; i<numThreads; i++){
|
58 | 59 | Thread thread = new Thread(new NotificationProcessor());
|
59 | 60 | thread.start();
|
60 | 61 | }
|
| 62 | +isRunning.set(true); |
61 | 63 | }
|
62 | 64 |
|
63 | 65 |
|
@@ -78,16 +80,22 @@ public static void stop(){
|
78 | 80 | events.add(0, null);
|
79 | 81 | events.notify();
|
80 | 82 | }
|
81 |
| -events = null; |
82 | 83 | }
|
83 | 84 |
|
84 | 85 |
|
85 | 86 | //**************************************************************************
|
86 | 87 | //** addListener
|
87 | 88 | //**************************************************************************
|
88 |
| -/** Used to add an event listener that will consume events |
| 89 | +/** Used to add an event listener that will consume events. Example: |
| 90 | +<pre> |
| 91 | +NotificationService.addListener(( |
| 92 | +String event, String model, javaxt.utils.Value data, long timestamp)->{ |
| 93 | +console.log(event, model, data); |
| 94 | +}); |
| 95 | +</pre> |
89 | 96 | */
|
90 | 97 | public static void addListener(Listener listener){
|
| 98 | +if (!isRunning.get()) return; |
91 | 99 | synchronized(listeners){
|
92 | 100 | listeners.add(listener);
|
93 | 101 | listeners.notify();
|
@@ -139,6 +147,7 @@ public void run() {
|
139 | 147 |
|
140 | 148 | }
|
141 | 149 | else{
|
| 150 | +isRunning.set(false); |
142 | 151 | return;
|
143 | 152 | }
|
144 | 153 | }
|
|
0 commit comments