1 |
| package org.jboss.cache; |
2 |
| |
3 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
4 |
| import org.jboss.cache.notifications.annotation.CacheStarted; |
5 |
| import org.jboss.cache.notifications.annotation.CacheStopped; |
6 |
| import org.jboss.cache.notifications.annotation.NodeActivated; |
7 |
| import org.jboss.cache.notifications.annotation.NodeCreated; |
8 |
| import org.jboss.cache.notifications.annotation.NodeEvicted; |
9 |
| import org.jboss.cache.notifications.annotation.NodeLoaded; |
10 |
| import org.jboss.cache.notifications.annotation.NodeModified; |
11 |
| import org.jboss.cache.notifications.annotation.NodeMoved; |
12 |
| import org.jboss.cache.notifications.annotation.NodePassivated; |
13 |
| import org.jboss.cache.notifications.annotation.NodeRemoved; |
14 |
| import org.jboss.cache.notifications.annotation.NodeVisited; |
15 |
| import org.jboss.cache.notifications.annotation.ViewChanged; |
16 |
| import org.jboss.cache.notifications.event.Event; |
17 |
| import org.jboss.cache.notifications.event.NodeEvent; |
18 |
| import org.jboss.cache.notifications.event.ViewChangedEvent; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| @CacheListener |
34 |
| public class ConsoleListener |
35 |
| { |
36 |
| private CacheImpl _cache; |
37 |
| private boolean _startCache; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
0
| public ConsoleListener(CacheImpl cache)
|
48 |
| throws Exception |
49 |
| { |
50 |
0
| this(cache, true, true);
|
51 |
| } |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
0
| public ConsoleListener(CacheImpl cache,
|
63 |
| boolean startCache, boolean stopCache) |
64 |
| throws Exception |
65 |
| { |
66 |
0
| _cache = cache;
|
67 |
0
| _startCache = startCache;
|
68 |
| |
69 |
0
| if (stopCache)
|
70 |
| { |
71 |
0
| new ListenerShutdownHook().register();
|
72 |
| } |
73 |
| } |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
0
| public void listen()
|
83 |
| throws Exception |
84 |
| { |
85 |
0
| listen(true);
|
86 |
| } |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
0
| public void listen(boolean wait)
|
98 |
| throws Exception |
99 |
| { |
100 |
0
| _cache.getNotifier().addCacheListener(this);
|
101 |
| |
102 |
0
| if (_startCache)
|
103 |
| { |
104 |
0
| _cache.start();
|
105 |
| } |
106 |
| |
107 |
0
| synchronized (this)
|
108 |
| { |
109 |
0
| while (wait)
|
110 |
| { |
111 |
0
| wait();
|
112 |
| } |
113 |
| } |
114 |
| } |
115 |
| |
116 |
| |
117 |
0
| @CacheStarted
|
118 |
| @CacheStopped |
119 |
| public void printDetails(Event e) |
120 |
| { |
121 |
0
| printEvent("Cache started.");
|
122 |
| } |
123 |
| |
124 |
| |
125 |
0
| @NodeCreated
|
126 |
| @NodeLoaded |
127 |
| @NodeModified |
128 |
| @NodeRemoved |
129 |
| @NodeVisited |
130 |
| @NodeMoved |
131 |
| @NodeEvicted |
132 |
| @NodeActivated |
133 |
| @NodePassivated |
134 |
| public void printDetailsWithFqn(NodeEvent e) |
135 |
| { |
136 |
0
| if (e.isPre())
|
137 |
| { |
138 |
0
| printEvent("Event " + e.getType() + " on node [" + e.getFqn() + "] about to be invoked");
|
139 |
| } |
140 |
| else |
141 |
| { |
142 |
0
| printEvent("Event " + e.getType() + " on node [" + e.getFqn() + "] invoked");
|
143 |
| } |
144 |
| } |
145 |
| |
146 |
0
| @ViewChanged
|
147 |
| public void printNewView(ViewChangedEvent e) |
148 |
| { |
149 |
0
| printEvent("View change: " + e.getNewView());
|
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
0
| private void printEvent(String eventSuffix)
|
158 |
| { |
159 |
0
| System.out.print("EVENT");
|
160 |
0
| System.out.print(' ');
|
161 |
| |
162 |
0
| System.out.println(eventSuffix);
|
163 |
| } |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| private class ListenerShutdownHook extends Thread |
169 |
| { |
170 |
| |
171 |
| |
172 |
| |
173 |
0
| public void register()
|
174 |
| { |
175 |
0
| Runtime.getRuntime().addShutdownHook(this);
|
176 |
| } |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
0
| public void run()
|
183 |
| { |
184 |
0
| _cache.stop();
|
185 |
| } |
186 |
| } |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
0
| public static void main(String[] args)
|
205 |
| { |
206 |
0
| final String DEFAULT_CONFIG_FILE_NAME = "jboss-cache.xml";
|
207 |
| |
208 |
0
| try
|
209 |
| { |
210 |
0
| String configFileName = DEFAULT_CONFIG_FILE_NAME;
|
211 |
| |
212 |
0
| if (args.length >= 1)
|
213 |
| { |
214 |
0
| configFileName = args[0];
|
215 |
| } |
216 |
| else |
217 |
| { |
218 |
0
| System.out.print("No xml config file argument is supplied. Will use jboss-cache.xml from classpath");
|
219 |
| } |
220 |
| |
221 |
0
| CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(configFileName);
|
222 |
0
| ConsoleListener listener = new ConsoleListener(cache);
|
223 |
0
| listener.listen();
|
224 |
| } |
225 |
| catch (Throwable throwable) |
226 |
| { |
227 |
0
| throwable.printStackTrace();
|
228 |
| } |
229 |
| } |
230 |
| } |