KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ClusterMembershipEventJMXTestApp


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import org.apache.commons.io.FileUtils;
8
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.TransparencyClassSpec;
12 import com.tc.objectserver.control.ExtraL1ProcessControl;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tc.util.Assert;
16 import com.tctest.runner.AbstractTransparentApp;
17
18 import java.io.File JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MBeanServerFactory JavaDoc;
27 import javax.management.MBeanServerNotification JavaDoc;
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationFilter JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 public class ClusterMembershipEventJMXTestApp extends AbstractTransparentApp implements NotificationListener JavaDoc {
34
35   public static final String JavaDoc CONFIG_FILE = "config-file";
36   public static final String JavaDoc PORT_NUMBER = "port-number";
37   public static final String JavaDoc HOST_NAME = "host-name";
38
39   private final ApplicationConfig config;
40
41   private MBeanServer JavaDoc server = null;
42   private ObjectName JavaDoc clusterBean = null;
43   private List JavaDoc clusterBeanBag = new ArrayList JavaDoc();
44   private Map JavaDoc eventsCount = new HashMap JavaDoc();
45
46   public ClusterMembershipEventJMXTestApp(String JavaDoc appId, ApplicationConfig config, ListenerProvider listenerProvider) {
47     super(appId, config, listenerProvider);
48     this.config = config;
49
50     try {
51       registerMBeanListener();
52     } catch (Exception JavaDoc ex) {
53       ex.printStackTrace();
54     }
55   }
56
57   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
58     String JavaDoc testClass = ClusterMembershipEventJMXTestApp.class.getName();
59     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
60     config.addWriteAutolock(methodExpression);
61     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
62     config.addIncludePattern(testClass + "$*");
63   }
64
65   public void run() {
66     try {
67       runTest();
68     } catch (Throwable JavaDoc t) {
69       notifyError(t);
70     }
71   }
72
73   private void runTest() throws Throwable JavaDoc {
74     config.getServerControl().crash();
75     while (config.getServerControl().isRunning()) {
76       Thread.sleep(5000);
77     }
78     config.getServerControl().start(30 * 1000);
79     while (!config.getServerControl().isRunning()) {
80       Thread.sleep(5000);
81     }
82     echo("Server restarted successfully.");
83     spawnNewClient();
84     synchronized (eventsCount) {
85       while (eventsCount.size() < 4) {
86         eventsCount.wait();
87       }
88       Assert.assertEquals(1, ((Integer JavaDoc)eventsCount.get("com.tc.cluster.event.nodeDisconnected")).intValue());
89       Assert.assertEquals(1, ((Integer JavaDoc)eventsCount.get("com.tc.cluster.event.nodeConnected")).intValue());
90       Assert.assertEquals(1, ((Integer JavaDoc)eventsCount.get("com.tc.cluster.event.thisNodeDisconnected")).intValue());
91       Assert.assertEquals(1, ((Integer JavaDoc)eventsCount.get("com.tc.cluster.event.thisNodeConnected")).intValue());
92     }
93   }
94
95   private void registerMBeanListener() throws Exception JavaDoc {
96     List JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
97     if (servers.size() == 0) { throw new RuntimeException JavaDoc("No bean server found!"); }
98     echo("Servers found: " + servers.size());
99     Assert.assertEquals(1, servers.size());
100     server = (MBeanServer JavaDoc) servers.get(0);
101
102     // our *star* bean for memebership events
103
clusterBean = new ObjectName JavaDoc("org.terracotta:type=Terracotta Cluster,name=Terracotta Cluster Bean");
104
105     // The MBeanServerDelegate emits notifications about
106
// registration/unregistration of MBeans
107
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
108
109     // listener for newly registered MBeans
110
NotificationListener JavaDoc listener = new NotificationListener JavaDoc() {
111       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback) {
112         synchronized (clusterBeanBag) {
113           clusterBeanBag.add(handback);
114           clusterBeanBag.notifyAll();
115         }
116       }
117     };
118
119     // filter to let only clusterBean passed through
120
NotificationFilter JavaDoc filter = new NotificationFilter JavaDoc() {
121       public boolean isNotificationEnabled(Notification JavaDoc notification) {
122         if (notification.getType().equals("JMX.mbean.registered")
123             && ((MBeanServerNotification JavaDoc) notification).getMBeanName().equals(clusterBean)) return true;
124         return false;
125       }
126     };
127
128     // add our listener for clusterBean's registration
129
server.addNotificationListener(delegateName, listener, filter, clusterBean);
130
131     // because of race condition, clusterBean might already have registered
132
// before we registered the listener
133
Set JavaDoc allObjectNames = server.queryNames(null, null);
134
135     if (!allObjectNames.contains(clusterBean)) {
136       synchronized (clusterBeanBag) {
137         while (clusterBeanBag.isEmpty()) {
138           clusterBeanBag.wait();
139         }
140       }
141     }
142
143     // clusterBean is now registered, no need to listen for it
144
server.removeNotificationListener(delegateName, listener);
145
146     // now that we have the clusterBean, add listener for membership events
147
server.addNotificationListener(clusterBean, this, null, clusterBean);
148   }
149
150   public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback) {
151     synchronized (eventsCount) {
152       String JavaDoc msgType = notification.getType();
153       Integer JavaDoc count = (Integer JavaDoc) eventsCount.get(msgType);
154       if (count != null) {
155         eventsCount.put(msgType, new Integer JavaDoc(count.intValue() + 1));
156       } else {
157         eventsCount.put(msgType, new Integer JavaDoc(1));
158       }
159       echo("type=" + notification.getType() + ", message=" + notification.getMessage());
160       eventsCount.notifyAll();
161     }
162   }
163
164   private static void echo(String JavaDoc msg) {
165     System.out.println(msg);
166   }
167
168   public static class L1Client {
169     public static void main(String JavaDoc args[]) {
170       try {
171         Thread.sleep(2000);
172       } catch (InterruptedException JavaDoc e) {
173         //ignored
174
} finally {
175         System.out.println("L1Client exiting.");
176       }
177     }
178   }
179
180   private ExtraL1ProcessControl spawnNewClient() throws Exception JavaDoc {
181     final String JavaDoc hostName = config.getAttribute(HOST_NAME);
182     final int port = Integer.parseInt(config.getAttribute(PORT_NUMBER));
183     final File JavaDoc configFile = new File JavaDoc(config.getAttribute(CONFIG_FILE));
184     File JavaDoc workingDir = new File JavaDoc(configFile.getParentFile(), "client-0");
185     FileUtils.forceMkdir(workingDir);
186
187     ExtraL1ProcessControl client = new ExtraL1ProcessControl(hostName, port, L1Client.class, configFile
188         .getAbsolutePath(), new String JavaDoc[0], workingDir);
189     client.start(20000);
190     client.mergeSTDERR();
191     client.mergeSTDOUT();
192     client.waitFor();
193     System.err.println("\n### Started New Client");
194     return client;
195   }
196
197 }
198
Popular Tags