1 7 package org.jboss.samples.detection; 8 9 import org.jboss.remoting.Client; 10 import org.jboss.remoting.InvokerLocator; 11 import org.jboss.remoting.detection.multicast.MulticastDetector; 12 import org.jboss.remoting.network.NetworkNotification; 13 import org.jboss.remoting.network.NetworkRegistry; 14 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerFactory ; 17 import javax.management.Notification ; 18 import javax.management.NotificationListener ; 19 import javax.management.ObjectName ; 20 21 24 public class SimpleDetectorClient implements NotificationListener 25 { 26 public void setupDetector() throws Exception 27 { 28 MBeanServer server = MBeanServerFactory.createMBeanServer(); 29 30 NetworkRegistry registry = NetworkRegistry.getInstance(); 31 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 32 33 registry.addNotificationListener(this, null, null); 35 36 MulticastDetector detector = new MulticastDetector(); 37 server.registerMBean(detector, new ObjectName ("remoting:type=MulticastDetector")); 38 detector.start(); 39 } 40 41 49 public void handleNotification(Notification notification, Object handback) 50 { 51 if(notification instanceof NetworkNotification) 52 { 53 NetworkNotification networkNotification = (NetworkNotification) notification; 54 InvokerLocator[] locators = networkNotification.getLocator(); 55 for(int x = 0; x < locators.length; x++) 56 { 57 try 58 { 59 makeInvocation(locators[x].getLocatorURI()); 60 } 61 catch(Throwable throwable) 62 { 63 throwable.printStackTrace(); 64 } 65 } 66 } 67 } 68 69 public void makeInvocation(String locatorURI) throws Throwable 70 { 71 InvokerLocator locator = new InvokerLocator(locatorURI); 72 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 73 74 Client remotingClient = new Client(locator); 75 Object response = remotingClient.invoke("Do something", null); 76 77 System.out.println("Invocation response: " + response); 78 } 79 80 public static void main(String [] args) 81 { 82 SimpleDetectorClient client = new SimpleDetectorClient(); 83 try 84 { 85 client.setupDetector(); 86 87 Thread.sleep(10000); 89 90 } 91 catch(Throwable e) 92 { 93 e.printStackTrace(); 94 } 95 } 96 97 98 } | Popular Tags |