1 22 package org.jboss.ha.framework.server.util; 23 24 import java.net.InetAddress ; 25 import java.util.ArrayList ; 26 import java.util.Vector ; 27 import java.io.IOException ; 28 import javax.naming.InitialContext ; 29 import javax.management.ObjectName ; 30 31 import org.jboss.ha.framework.interfaces.HAPartition; 32 import org.jboss.ha.framework.interfaces.HAPartition.AsynchHAMembershipListener; 33 import org.jboss.system.ServiceMBeanSupport; 34 import org.jboss.system.server.ServerConfigUtil; 35 import org.jboss.logging.Logger; 36 import org.apache.log4j.MDC; 37 38 81 public class TopologyMonitorService extends ServiceMBeanSupport 82 implements TopologyMonitorServiceMBean, AsynchHAMembershipListener 83 { 84 private static final String CHANGE_NAME = TopologyMonitorService.class.getName() 85 + ".membershipChanged"; 86 private static Logger changeLog = Logger.getLogger(CHANGE_NAME); 87 private String partitionName = ServerConfigUtil.getDefaultPartitionName(); 88 private HAPartition partition; 89 private String hostname; 90 private ObjectName triggerServiceName; 91 92 public TopologyMonitorService() 93 { 94 } 95 96 protected void startService() throws Exception 98 { 99 InitialContext ctx = new InitialContext (); 100 String partitionJndiName = "/HAPartition/" + partitionName; 101 partition = (HAPartition) ctx.lookup(partitionJndiName); 102 partition.registerMembershipListener(this); 104 log.info("Registered as MembershipListener"); 105 try 106 { 107 hostname = InetAddress.getLocalHost().getHostName(); 108 } 109 catch(IOException e) 110 { 111 log.warn("Failed to lookup local hostname", e); 112 hostname = "<unknown>"; 113 } 114 } 115 116 protected void stopService() throws Exception 117 { 118 partition.unregisterMembershipListener(this); 119 } 120 122 public String getPartitionName() 124 { 125 return partitionName; 126 } 127 public void setPartitionName(String name) 128 { 129 this.partitionName = name; 130 } 131 132 public ObjectName getTriggerServiceName() 133 { 134 return triggerServiceName; 135 } 136 public void setTriggerServiceName(ObjectName triggerServiceName) 137 { 138 this.triggerServiceName = triggerServiceName; 139 } 140 141 public Vector getClusterNodes() 142 { 143 Vector view = null; 144 try 145 { 146 InitialContext ctx = new InitialContext (); 147 String jndiName = "/HAPartition/" + partitionName; 148 HAPartition partition = (HAPartition) ctx.lookup(jndiName); 149 view = partition.getCurrentView(); 150 } 151 catch(Exception e) 152 { 153 log.error("Failed to access HAPartition state", e); 154 } 155 return view; 156 } 157 158 160 167 public void membershipChanged(final Vector deadMembers, final Vector newMembers, 168 final Vector allMembers) 169 { 170 MDC.put("RegexEventEvaluator", "End membershipChange.*"); 171 ArrayList removed = new ArrayList (); 172 ArrayList added = new ArrayList (); 173 ArrayList members = new ArrayList (); 174 changeLog.info("Begin membershipChanged info, hostname="+hostname); 175 changeLog.info("DeadMembers: size="+deadMembers.size()); 176 for(int m = 0; m < deadMembers.size(); m ++) 177 { 178 AddressPort addrInfo = getMemberAddress(deadMembers.get(m)); 179 removed.add(addrInfo); 180 changeLog.info(addrInfo); 181 } 182 changeLog.info("NewMembers: size="+newMembers.size()); 183 for(int m = 0; m < newMembers.size(); m ++) 184 { 185 AddressPort addrInfo = getMemberAddress(newMembers.get(m)); 186 added.add(addrInfo); 187 changeLog.info(addrInfo); 188 } 189 changeLog.info("AllMembers: size="+allMembers.size()); 190 for(int m = 0; m < allMembers.size(); m ++) 191 { 192 AddressPort addrInfo = getMemberAddress(allMembers.get(m)); 193 members.add(addrInfo); 194 changeLog.info(addrInfo); 195 } 196 if( triggerServiceName != null ) 198 { 199 changeLog.info("Invoking trigger service: "+triggerServiceName); 200 try 201 { 202 Object [] params = {removed, added, members, CHANGE_NAME}; 203 String [] sig = {"java.util.ArrayList", "java.util.ArrayList", 204 "java.util.ArrayList", "java.lang.String"}; 205 server.invoke(triggerServiceName, "membershipChanged", params, sig); 206 } 207 catch(Throwable t) 208 { 209 changeLog.error("Failed to notify trigger service: "+triggerServiceName, t); 210 log.debug("Failed to notify trigger service: "+triggerServiceName, t); 211 } 212 } 213 changeLog.info("End membershipChanged info, hostname="+hostname); 214 MDC.remove("RegexEventEvaluator"); 215 } 216 218 221 private AddressPort getMemberAddress(Object addr) 222 { 223 AddressPort info = null; 224 try 225 { 226 org.jboss.ha.framework.interfaces.ClusterNode node = 227 (org.jboss.ha.framework.interfaces.ClusterNode)addr; 228 229 InetAddress inetAddr = node.getOriginalJGAddress().getIpAddress(); 230 Integer port = new Integer (node.getOriginalJGAddress().getPort()); 231 info = new AddressPort(inetAddr, port); 232 } 233 catch(Exception e) 234 { 235 log.warn("Failed to obtain InetAddress/port from addr: "+addr, e); 236 } 237 return info; 238 } 239 240 public static class AddressPort 241 { 242 InetAddress addr; 243 Integer port; 244 AddressPort(InetAddress addr, Integer port) 245 { 246 this.addr = addr; 247 this.port = port; 248 } 249 250 public Integer getPort() 251 { 252 return port; 253 } 254 public InetAddress getInetAddress() 255 { 256 return addr; 257 } 258 public String getHostAddress() 259 { 260 return addr.getHostAddress(); 261 } 262 public String getHostName() 263 { 264 return addr.getHostName(); 265 } 266 public String toString() 267 { 268 return "{host("+addr+"), port("+port+")}"; 269 } 270 } 271 } 272 | Popular Tags |