KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > samples > detection > SimpleDetectorClient


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import javax.management.Notification JavaDoc;
18 import javax.management.NotificationListener JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20
21 /**
22  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
23  */

24 public class SimpleDetectorClient implements NotificationListener JavaDoc
25 {
26    public void setupDetector() throws Exception JavaDoc
27    {
28       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
29
30       NetworkRegistry registry = NetworkRegistry.getInstance();
31       server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
32
33       // register class as listener, so know when new server found
34
registry.addNotificationListener(this, null, null);
35
36       MulticastDetector detector = new MulticastDetector();
37       server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=MulticastDetector"));
38       detector.start();
39    }
40
41    /**
42     * Callback method from the broadcaster MBean this listener implementation
43     * is registered to.
44     *
45     * @param notification the notification object
46     * @param handback the handback object given to the broadcaster
47     * upon listener registration
48     */

49    public void handleNotification(Notification notification, Object JavaDoc 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 JavaDoc throwable)
62             {
63                throwable.printStackTrace();
64             }
65          }
66       }
67    }
68
69    public void makeInvocation(String JavaDoc locatorURI) throws Throwable JavaDoc
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 JavaDoc response = remotingClient.invoke("Do something", null);
76
77       System.out.println("Invocation response: " + response);
78    }
79
80    public static void main(String JavaDoc[] args)
81    {
82       SimpleDetectorClient client = new SimpleDetectorClient();
83       try
84       {
85          client.setupDetector();
86
87          // sleep the thread for 10 seconds while waiting detection
88
Thread.sleep(10000);
89
90       }
91       catch(Throwable JavaDoc e)
92       {
93          e.printStackTrace();
94       }
95    }
96
97
98 }
Popular Tags