1 9 package org.jboss.test.mx.remoting.pingpong; 10 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.Timer ; 15 import java.util.TimerTask ; 16 17 import javax.management.AttributeChangeNotification ; 18 import javax.management.ListenerNotFoundException ; 19 import javax.management.MBeanException ; 20 import javax.management.MBeanNotificationInfo ; 21 import javax.management.MBeanRegistration ; 22 import javax.management.MBeanServer ; 23 import javax.management.Notification ; 24 import javax.management.NotificationFilter ; 25 import javax.management.NotificationListener ; 26 import javax.management.ObjectName ; 27 28 import org.jboss.logging.Logger; 29 import org.jboss.mx.remoting.MBeanLocator; 30 import org.jboss.mx.remoting.tracker.MBeanTracker; 31 import org.jboss.mx.remoting.tracker.MBeanTrackerAction; 32 import org.jboss.mx.util.JBossNotificationBroadcasterSupport; 33 import org.jboss.remoting.ConnectionFailedException; 34 import org.jboss.remoting.ident.Identity; 35 36 44 public class PingPong implements PingPongMBean, MBeanTrackerAction, MBeanRegistration 45 { 46 private static final transient Logger log = Logger.getLogger(PingPong.class.getName()); 47 private MBeanTracker tracker; 48 private Timer timer=new Timer (false); 49 private Map friends=new HashMap (); 50 private MBeanServer server; 51 private ObjectName objectName; 52 private JBossNotificationBroadcasterSupport broadcaster=new JBossNotificationBroadcasterSupport(); 53 54 public Object ping (Object pong) 55 { 56 Notification notification = new Notification ("pong",objectName,System.currentTimeMillis()); 57 broadcaster.sendNotification(notification); 58 log.debug("ping called: "+pong+", sending notification: "+notification+" for objectName: "+objectName); 59 Notification stateChange = new AttributeChangeNotification (objectName,System.currentTimeMillis(),System.currentTimeMillis(),"State Changed","State",Integer .class.getName(),new Integer (1),new Integer (2)); 60 broadcaster.sendNotification(stateChange); 61 return pong; 62 } 63 64 75 public void addNotificationListener ( NotificationListener listener, 76 NotificationFilter filter, 77 Object handback ) 78 throws IllegalArgumentException 79 { 80 log.debug("addNotificationListener - listener: "+listener); 81 broadcaster.addNotificationListener(listener,filter,handback); 82 } 83 84 91 public void removeNotificationListener ( NotificationListener listener ) 92 throws ListenerNotFoundException 93 { 94 log.debug("removeNotificationListener - listener: "+listener); 95 broadcaster.removeNotificationListener(listener); 96 } 97 98 105 public MBeanNotificationInfo [] getNotificationInfo () 106 { 107 return new MBeanNotificationInfo [0]; 108 } 109 110 114 public void postDeregister () 115 { 116 } 117 118 125 public void postRegister (Boolean registrationDone) 126 { 127 } 128 129 137 public void preDeregister () 138 throws Exception 139 { 140 stop(); 141 } 142 143 161 public ObjectName preRegister (MBeanServer server, ObjectName name) 162 throws Exception 163 { 164 this.server = server; 165 this.objectName = name; 166 start(); 167 return name; 168 } 169 170 171 177 public void start () throws Exception 178 { 179 tracker = new MBeanTracker(server,new Class []{PingPongMBean.class},null,false,null,true,this); 180 timer.scheduleAtFixedRate(new Pinger(),5000L,5000L); 181 } 182 183 189 public void stop () 190 { 191 if (tracker!=null) 192 { 193 tracker.destroy(); 194 tracker=null; 195 } 196 } 197 198 205 public void mbeanNotification (MBeanLocator locator, Notification notification, Object handback) 206 { 207 log.info("received notification from: "+locator+", notification: "+notification); 208 } 209 210 215 public void mbeanRegistered (MBeanLocator locator) 216 { 217 if (Identity.get(server).equals(locator.getIdentity())) 218 { 219 return; 221 } 222 log.info("found a new friend to play ping pong with: "+locator); 223 PingPongMBean friend=null; 224 try 225 { 226 friend=(PingPongMBean)locator.narrow(PingPongMBean.class); 227 } 228 catch (Exception e) 229 { 230 log.error("error casting my friend to PingPongMBean - his locator is: "+locator,e); 231 return; 232 } 233 synchronized(friends) 234 { 235 friends.put(locator,friend); 236 } 237 } 238 239 247 public void mbeanStateChanged (MBeanLocator locator, int oldState, int newState) 248 { 249 log.info("one of my partners ("+locator+") changed its state from: "+oldState+" to: "+newState); 250 } 251 252 257 public void mbeanUnregistered (MBeanLocator locator) 258 { 259 log.info("I lost a friend, "+locator); 260 friends.remove(locator); 261 } 262 263 final class Pinger extends TimerTask 264 { 265 268 public void run () 269 { 270 Map copy = null; 271 synchronized (friends) 272 { 273 copy = new HashMap (friends); 274 } 275 if (copy.isEmpty()) 276 { 277 log.info("I don't have any friends on the network, how boring..."); 278 return; 279 } 280 Iterator iter = copy.keySet().iterator(); 281 Integer myhash=new Integer (hashCode()); 282 while(iter.hasNext()) 283 { 284 MBeanLocator l=(MBeanLocator)iter.next(); 285 try 286 { 287 Object obj = l.getServerLocator().getMBeanServer().invoke(l.getObjectName(),"ping",new Object []{myhash},new String []{Object .class.getName()}); 288 log.info("pinging my friend at: "+l+", with: "+myhash); 290 if (!obj.equals(myhash)) 293 { 294 log.warn("My friend passed me back something I don't understand?! I passed him: "+myhash+", I received: "+obj); 295 } 296 } 297 catch (Throwable ex) 298 { 299 if (ex instanceof MBeanException ) 300 { 301 MBeanException mbe=(MBeanException )ex; 302 if (mbe.getTargetException() instanceof ConnectionFailedException) 303 { 304 log.info("my friend died during a ping, "+l); 305 return; 306 } 307 } 308 log.warn("My friend doesn't like me, he gave me an exception back",ex); 309 } 310 } 311 copy=null; 312 } 313 } 314 } 315 316 | Popular Tags |