1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.jmx; |
24 |
| |
25 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
26 |
| import org.jboss.cache.notifications.annotation.CacheStarted; |
27 |
| import org.jboss.cache.notifications.annotation.CacheStopped; |
28 |
| import org.jboss.cache.notifications.annotation.NodeActivated; |
29 |
| import org.jboss.cache.notifications.annotation.NodeCreated; |
30 |
| import org.jboss.cache.notifications.annotation.NodeEvicted; |
31 |
| import org.jboss.cache.notifications.annotation.NodeLoaded; |
32 |
| import org.jboss.cache.notifications.annotation.NodeModified; |
33 |
| import org.jboss.cache.notifications.annotation.NodeMoved; |
34 |
| import org.jboss.cache.notifications.annotation.NodePassivated; |
35 |
| import org.jboss.cache.notifications.annotation.NodeRemoved; |
36 |
| import org.jboss.cache.notifications.annotation.NodeVisited; |
37 |
| import org.jboss.cache.notifications.annotation.ViewChanged; |
38 |
| import org.jboss.cache.notifications.event.Event; |
39 |
| import org.jboss.cache.notifications.event.NodeEvent; |
40 |
| import org.jboss.cache.notifications.event.NodeMovedEvent; |
41 |
| import org.jboss.cache.notifications.event.ViewChangedEvent; |
42 |
| |
43 |
| import javax.management.MBeanNotificationInfo; |
44 |
| import javax.management.Notification; |
45 |
| import java.util.concurrent.atomic.AtomicLong; |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| @CacheListener |
55 |
| public class CacheNotificationListener |
56 |
| { |
57 |
| |
58 |
| private static final String MSG_CACHE_STARTED = "Cache has been started."; |
59 |
| private static final String MSG_CACHE_STOPPED = "Cache has been stopped."; |
60 |
| private static final String MSG_NODE_CREATED = "Node has been created."; |
61 |
| private static final String MSG_NODE_MODIFIED = "Node has been modifed."; |
62 |
| private static final String MSG_NODE_REMOVED = "Node has been removed."; |
63 |
| private static final String MSG_NODE_MOVED = "Node has been moved."; |
64 |
| private static final String MSG_NODE_VISITED = "Node has been visited."; |
65 |
| private static final String MSG_NODE_EVICTED = "Node has been evicted."; |
66 |
| private static final String MSG_NODE_LOADED = "Node has been loaded."; |
67 |
| private static final String MSG_NODE_ACTIVATED = "Node has been activated."; |
68 |
| private static final String MSG_NODE_PASSIVATED = "Node has been passivated."; |
69 |
| private static final String MSG_VIEW_CHANGED = "Cache cluster view has changed."; |
70 |
| |
71 |
| |
72 |
| private static final String NOTIFICATION_NAME = Notification.class.getName(); |
73 |
| private static final String NOTIFICATION_DESCR = "JBossCache event notifications"; |
74 |
| |
75 |
| private final CacheNotificationBroadcaster broadcaster; |
76 |
| private String serviceName; |
77 |
| |
78 |
| |
79 |
| |
80 |
76
| CacheNotificationListener(CacheNotificationBroadcaster support)
|
81 |
| { |
82 |
76
| this.broadcaster = support;
|
83 |
| } |
84 |
| |
85 |
| |
86 |
| |
87 |
0
| public String getServiceName()
|
88 |
| { |
89 |
0
| return serviceName;
|
90 |
| } |
91 |
| |
92 |
99
| public void setServiceName(String serviceName)
|
93 |
| { |
94 |
99
| this.serviceName = serviceName;
|
95 |
| } |
96 |
| |
97 |
0
| public static MBeanNotificationInfo[] getNotificationInfo()
|
98 |
| { |
99 |
0
| String[] types = new String[]
|
100 |
| { |
101 |
| CacheNotificationBroadcaster.NOTIF_CACHE_STARTED, |
102 |
| CacheNotificationBroadcaster.NOTIF_CACHE_STOPPED, |
103 |
| CacheNotificationBroadcaster.NOTIF_NODE_CREATED, |
104 |
| CacheNotificationBroadcaster.NOTIF_NODE_EVICTED, |
105 |
| CacheNotificationBroadcaster.NOTIF_NODE_LOADED, |
106 |
| CacheNotificationBroadcaster.NOTIF_NODE_MODIFIED, |
107 |
| CacheNotificationBroadcaster.NOTIF_NODE_ACTIVATED, |
108 |
| CacheNotificationBroadcaster.NOTIF_NODE_PASSIVATED, |
109 |
| CacheNotificationBroadcaster.NOTIF_NODE_REMOVED, |
110 |
| CacheNotificationBroadcaster.NOTIF_NODE_VISITED, |
111 |
| CacheNotificationBroadcaster.NOTIF_VIEW_CHANGED, |
112 |
| }; |
113 |
| |
114 |
0
| MBeanNotificationInfo info = new MBeanNotificationInfo(types, NOTIFICATION_NAME, NOTIFICATION_DESCR);
|
115 |
0
| return new MBeanNotificationInfo[]{info};
|
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
164
| @CacheStarted
|
121 |
| @CacheStopped |
122 |
| @NodeCreated |
123 |
| @NodeEvicted |
124 |
| @NodeLoaded |
125 |
| @NodeModified |
126 |
| @NodeRemoved |
127 |
| @NodeMoved |
128 |
| @NodeVisited |
129 |
| @NodeActivated |
130 |
| @NodePassivated |
131 |
| @ViewChanged |
132 |
| public void broadcast(Event e) |
133 |
| { |
134 |
164
| NodeEvent ne;
|
135 |
164
| Notification n = null;
|
136 |
164
| switch (e.getType())
|
137 |
| { |
138 |
14
| case CACHE_STARTED:
|
139 |
14
| n = new Notification(CacheNotificationBroadcaster.NOTIF_CACHE_STARTED, broadcaster, seq(), MSG_CACHE_STARTED);
|
140 |
14
| n.setUserData(serviceName);
|
141 |
14
| break;
|
142 |
14
| case CACHE_STOPPED:
|
143 |
14
| n = new Notification(CacheNotificationBroadcaster.NOTIF_CACHE_STOPPED, broadcaster, seq(), MSG_CACHE_STOPPED);
|
144 |
14
| n.setUserData(serviceName);
|
145 |
14
| break;
|
146 |
40
| case NODE_CREATED:
|
147 |
40
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_CREATED, broadcaster, seq(), MSG_NODE_CREATED);
|
148 |
40
| ne = (NodeEvent) e;
|
149 |
40
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre(), ne.isOriginLocal()});
|
150 |
40
| break;
|
151 |
8
| case NODE_EVICTED:
|
152 |
8
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_EVICTED, broadcaster, seq(), MSG_NODE_EVICTED);
|
153 |
8
| ne = (NodeEvent) e;
|
154 |
8
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre(), ne.isOriginLocal()});
|
155 |
8
| break;
|
156 |
8
| case NODE_LOADED:
|
157 |
8
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_LOADED, broadcaster, seq(), MSG_NODE_LOADED);
|
158 |
8
| ne = (NodeEvent) e;
|
159 |
8
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre()});
|
160 |
8
| break;
|
161 |
24
| case NODE_MODIFIED:
|
162 |
24
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_MODIFIED, broadcaster, seq(), MSG_NODE_MODIFIED);
|
163 |
24
| ne = (NodeEvent) e;
|
164 |
24
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre(), ne.isOriginLocal()});
|
165 |
24
| break;
|
166 |
8
| case NODE_REMOVED:
|
167 |
8
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_REMOVED, broadcaster, seq(), MSG_NODE_REMOVED);
|
168 |
8
| ne = (NodeEvent) e;
|
169 |
8
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre(), ne.isOriginLocal()});
|
170 |
8
| break;
|
171 |
0
| case NODE_MOVED:
|
172 |
0
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_MOVED, broadcaster, seq(), MSG_NODE_MOVED);
|
173 |
0
| NodeMovedEvent nme = (NodeMovedEvent) e;
|
174 |
0
| n.setUserData(new Object[]{nme.getFqn().toString(), nme.getTargetFqn().toString(), e.isPre()});
|
175 |
0
| break;
|
176 |
16
| case NODE_VISITED:
|
177 |
16
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_VISITED, broadcaster, seq(), MSG_NODE_VISITED);
|
178 |
16
| ne = (NodeEvent) e;
|
179 |
16
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre()});
|
180 |
16
| break;
|
181 |
12
| case NODE_ACTIVATED:
|
182 |
12
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_ACTIVATED, broadcaster, seq(), MSG_NODE_ACTIVATED);
|
183 |
12
| ne = (NodeEvent) e;
|
184 |
12
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre()});
|
185 |
12
| break;
|
186 |
8
| case NODE_PASSIVATED:
|
187 |
8
| n = new Notification(CacheNotificationBroadcaster.NOTIF_NODE_PASSIVATED, broadcaster, seq(), MSG_NODE_PASSIVATED);
|
188 |
8
| ne = (NodeEvent) e;
|
189 |
8
| n.setUserData(new Object[]{ne.getFqn().toString(), e.isPre()});
|
190 |
8
| break;
|
191 |
12
| case VIEW_CHANGED:
|
192 |
12
| n = new Notification(CacheNotificationBroadcaster.NOTIF_VIEW_CHANGED, broadcaster, seq(), MSG_VIEW_CHANGED);
|
193 |
12
| n.setUserData(((ViewChangedEvent) e).getNewView().toString());
|
194 |
12
| break;
|
195 |
| } |
196 |
| |
197 |
164
| broadcaster.sendNotification(n);
|
198 |
| } |
199 |
| |
200 |
164
| private long seq()
|
201 |
| { |
202 |
164
| return broadcaster.getNextNotificationSequenceNumber();
|
203 |
| } |
204 |
| } |