KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
3  */

4 package org.jboss.remoting.samples.detection;
5
6 import javax.management.MBeanServer JavaDoc;
7 import javax.management.MBeanServerFactory JavaDoc;
8 import javax.management.Notification JavaDoc;
9 import javax.management.NotificationListener JavaDoc;
10 import javax.management.ObjectName JavaDoc;
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 /**
18  * Simple remoting client that uses detection to discover the remoting server to make invocation on. Note that this
19  * class is a standard JMX NotificationListener so it can listen for discovery JMX notifications coming from the
20  * NetworkRegistry. This is how the NetworkRegistry tells us when new servers have come online and when dead
21  * servers go offline.
22  *
23  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
24  * @author <a HREF="mailto:mazz@jboss.com">John Mazzitelli</a>
25  */

26 public class SimpleDetectorClient
27       implements NotificationListener JavaDoc
28 {
29    /**
30     * Sets up NetworkRegistry and MulticastDetector so we can listen for any additions or removals of remoting
31     * servers on the network.
32     *
33     * @throws Exception
34     */

35    public void setupDetector()
36          throws Exception JavaDoc
37    {
38       // we need an MBeanServer to store our network registry and multicast detector services
39
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
40
41       // the registry will house all remoting servers discovered
42
NetworkRegistry registry = NetworkRegistry.getInstance();
43       server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
44       println("NetworkRegistry has been created");
45
46       // register class as listener, so know when new server found
47
registry.addNotificationListener(this, null, null);
48       println("NetworkRegistry has added the client as a listener");
49
50       // multicast detector will detect new network registries that come online
51
MulticastDetector detector = new MulticastDetector();
52       server.registerMBean(detector, new ObjectName JavaDoc("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    /**
60     * Callback method from the broadcaster MBean this listener implementation is registered to. When a new server
61     * is detected, a welcome message will immediately be sent to the newly discovered server.
62     *
63     * @param notification the notification object
64     * @param handback the handback object given to the broadcaster upon listener registration
65     */

66    public void handleNotification(Notification JavaDoc notification,
67                                   Object JavaDoc handback)
68    {
69       // check to see if network notification
70
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          { // notification is for new servers being added
78
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                   // get the new found server's locator and invoke a call
85
InvokerLocator newServerLocator = locators[x];
86                   makeInvocation(newServerLocator.getLocatorURI());
87                }
88                catch(Throwable JavaDoc throwable)
89                {
90                   throwable.printStackTrace();
91                }
92             }
93          }
94          else if(NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType()))
95          { // notification is for old servers that have gone down
96
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    /**
108     * Make call on remoting server based on locator uri provided.
109     *
110     * @param locatorURI the URI of the remote server we want to send the message to
111     * @throws Throwable
112     */

113    public void makeInvocation(String JavaDoc locatorURI)
114          throws Throwable JavaDoc
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 JavaDoc 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    /**
128     * Starts the JBoss/Remoting client.
129     *
130     * @param args unused
131     */

132    public static void main(String JavaDoc[] 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          // let this client run forever - welcoming new servers when then come online
141
while(true)
142          {
143             Thread.sleep(1000);
144          }
145       }
146       catch(Throwable JavaDoc e)
147       {
148          e.printStackTrace();
149       }
150
151       println("Stopping JBoss/Remoting client");
152    }
153
154    /**
155     * Outputs a message to stdout.
156     *
157     * @param msg the message to output
158     */

159    public static void println(String JavaDoc msg)
160    {
161       System.out.println((++MSG_COUNT) + ". [CLIENT]: " + msg);
162    }
163
164    // just to show the ordering of the messages that come out
165
private static int MSG_COUNT = 0;
166 }
Popular Tags