1 4 package org.jboss.remoting.samples.detection; 5 6 import javax.management.MBeanServer ; 7 import javax.management.MBeanServerFactory ; 8 import javax.management.Notification ; 9 import javax.management.NotificationListener ; 10 import javax.management.ObjectName ; 11 import org.jboss.remoting.Client; 12 import org.jboss.remoting.InvokerLocator; 13 import org.jboss.remoting.detection.multicast.MulticastDetector; 14 import org.jboss.remoting.network.NetworkNotification; 15 import org.jboss.remoting.network.NetworkRegistry; 16 17 26 public class SimpleDetectorClient 27 implements NotificationListener 28 { 29 35 public void setupDetector() 36 throws Exception 37 { 38 MBeanServer server = MBeanServerFactory.createMBeanServer(); 40 41 NetworkRegistry registry = NetworkRegistry.getInstance(); 43 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 44 println("NetworkRegistry has been created"); 45 46 registry.addNotificationListener(this, null, null); 48 println("NetworkRegistry has added the client as a listener"); 49 50 MulticastDetector detector = new MulticastDetector(); 52 server.registerMBean(detector, new ObjectName ("remoting:type=MulticastDetector")); 53 detector.start(); 54 println("MulticastDetector has been created and is listening for new NetworkRegistries to come online"); 55 56 return; 57 } 58 59 66 public void handleNotification(Notification notification, 67 Object handback) 68 { 69 if(notification instanceof NetworkNotification) 71 { 72 println("GOT A NETWORK-REGISTRY NOTIFICATION: " + notification.getType()); 73 74 NetworkNotification networkNotification = (NetworkNotification) notification; 75 76 if(NetworkNotification.SERVER_ADDED.equals(networkNotification.getType())) 77 { println("New server(s) have been detected - getting locators and sending welcome messages"); 79 InvokerLocator[] locators = networkNotification.getLocator(); 80 for(int x = 0; x < locators.length; x++) 81 { 82 try 83 { 84 InvokerLocator newServerLocator = locators[x]; 86 makeInvocation(newServerLocator.getLocatorURI()); 87 } 88 catch(Throwable throwable) 89 { 90 throwable.printStackTrace(); 91 } 92 } 93 } 94 else if(NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType())) 95 { InvokerLocator[] locators = networkNotification.getLocator(); 97 for(int x = 0; x < locators.length; x++) 98 { 99 println("It has been detected that a server has gone down with a locator of: " + locators[x]); 100 } 101 } 102 } 103 104 return; 105 } 106 107 113 public void makeInvocation(String locatorURI) 114 throws Throwable 115 { 116 InvokerLocator locator = new InvokerLocator(locatorURI); 117 println("Sending welcome message to remoting server with locator uri of: " + locatorURI); 118 119 Client remotingClient = new Client(locator); 120 Object response = remotingClient.invoke("Welcome Aboard!", null); 121 122 println("The newly discovered server sent this response to our welcome message: " + response); 123 124 return; 125 } 126 127 132 public static void main(String [] args) 133 { 134 println("Starting JBoss/Remoting client... to stop this client, kill it manually via Control-C"); 135 SimpleDetectorClient client = new SimpleDetectorClient(); 136 try 137 { 138 client.setupDetector(); 139 140 while(true) 142 { 143 Thread.sleep(1000); 144 } 145 } 146 catch(Throwable e) 147 { 148 e.printStackTrace(); 149 } 150 151 println("Stopping JBoss/Remoting client"); 152 } 153 154 159 public static void println(String msg) 160 { 161 System.out.println((++MSG_COUNT) + ". [CLIENT]: " + msg); 162 } 163 164 private static int MSG_COUNT = 0; 166 } | Popular Tags |