1 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 ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import javax.management.MBeanServer ; 26 import javax.management.MBeanServerFactory ; 27 import javax.management.MBeanServerNotification ; 28 import javax.management.Notification ; 29 import javax.management.NotificationFilter ; 30 import javax.management.NotificationListener ; 31 import javax.management.ObjectName ; 32 33 public class ClusterMembershipEventJMXTestApp extends AbstractTransparentApp implements NotificationListener { 34 35 public static final String CONFIG_FILE = "config-file"; 36 public static final String PORT_NUMBER = "port-number"; 37 public static final String HOST_NAME = "host-name"; 38 39 private final ApplicationConfig config; 40 41 private MBeanServer server = null; 42 private ObjectName clusterBean = null; 43 private List clusterBeanBag = new ArrayList (); 44 private Map eventsCount = new HashMap (); 45 46 public ClusterMembershipEventJMXTestApp(String appId, ApplicationConfig config, ListenerProvider listenerProvider) { 47 super(appId, config, listenerProvider); 48 this.config = config; 49 50 try { 51 registerMBeanListener(); 52 } catch (Exception ex) { 53 ex.printStackTrace(); 54 } 55 } 56 57 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 58 String testClass = ClusterMembershipEventJMXTestApp.class.getName(); 59 String 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 t) { 69 notifyError(t); 70 } 71 } 72 73 private void runTest() throws Throwable { 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 )eventsCount.get("com.tc.cluster.event.nodeDisconnected")).intValue()); 89 Assert.assertEquals(1, ((Integer )eventsCount.get("com.tc.cluster.event.nodeConnected")).intValue()); 90 Assert.assertEquals(1, ((Integer )eventsCount.get("com.tc.cluster.event.thisNodeDisconnected")).intValue()); 91 Assert.assertEquals(1, ((Integer )eventsCount.get("com.tc.cluster.event.thisNodeConnected")).intValue()); 92 } 93 } 94 95 private void registerMBeanListener() throws Exception { 96 List servers = MBeanServerFactory.findMBeanServer(null); 97 if (servers.size() == 0) { throw new RuntimeException ("No bean server found!"); } 98 echo("Servers found: " + servers.size()); 99 Assert.assertEquals(1, servers.size()); 100 server = (MBeanServer ) servers.get(0); 101 102 clusterBean = new ObjectName ("org.terracotta:type=Terracotta Cluster,name=Terracotta Cluster Bean"); 104 105 ObjectName delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"); 108 109 NotificationListener listener = new NotificationListener () { 111 public void handleNotification(Notification notification, Object handback) { 112 synchronized (clusterBeanBag) { 113 clusterBeanBag.add(handback); 114 clusterBeanBag.notifyAll(); 115 } 116 } 117 }; 118 119 NotificationFilter filter = new NotificationFilter () { 121 public boolean isNotificationEnabled(Notification notification) { 122 if (notification.getType().equals("JMX.mbean.registered") 123 && ((MBeanServerNotification ) notification).getMBeanName().equals(clusterBean)) return true; 124 return false; 125 } 126 }; 127 128 server.addNotificationListener(delegateName, listener, filter, clusterBean); 130 131 Set 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 server.removeNotificationListener(delegateName, listener); 145 146 server.addNotificationListener(clusterBean, this, null, clusterBean); 148 } 149 150 public void handleNotification(Notification notification, Object handback) { 151 synchronized (eventsCount) { 152 String msgType = notification.getType(); 153 Integer count = (Integer ) eventsCount.get(msgType); 154 if (count != null) { 155 eventsCount.put(msgType, new Integer (count.intValue() + 1)); 156 } else { 157 eventsCount.put(msgType, new Integer (1)); 158 } 159 echo("type=" + notification.getType() + ", message=" + notification.getMessage()); 160 eventsCount.notifyAll(); 161 } 162 } 163 164 private static void echo(String msg) { 165 System.out.println(msg); 166 } 167 168 public static class L1Client { 169 public static void main(String args[]) { 170 try { 171 Thread.sleep(2000); 172 } catch (InterruptedException e) { 173 } finally { 175 System.out.println("L1Client exiting."); 176 } 177 } 178 } 179 180 private ExtraL1ProcessControl spawnNewClient() throws Exception { 181 final String hostName = config.getAttribute(HOST_NAME); 182 final int port = Integer.parseInt(config.getAttribute(PORT_NUMBER)); 183 final File configFile = new File (config.getAttribute(CONFIG_FILE)); 184 File workingDir = new File (configFile.getParentFile(), "client-0"); 185 FileUtils.forceMkdir(workingDir); 186 187 ExtraL1ProcessControl client = new ExtraL1ProcessControl(hostName, port, L1Client.class, configFile 188 .getAbsolutePath(), new String [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 |