Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.remoting.samples.detection.jndi;
  
  
 import java.util.Map;
Simple remoting client that uses detection to discover the remoting server to make invocation on. Note that this class is a standard JMX NotificationListener so it can listen for discovery JMX notifications coming from the NetworkRegistry. This is how the NetworkRegistry tells us when new servers have come online and when dead servers go offline.

Author(s):
Tom Elrod
John Mazzitelli
 
 public class SimpleDetectorClient
       implements NotificationListener
 {
    private int detectorPort = 1099;
    private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
    private String urlPackage = "org.jboss.naming:org.jnp.interfaces";


   
Sets up NetworkRegistry and JNDIDetector so we can listen for any additions or removals of remoting servers on the network.

 
    public void setupDetector()
          throws Exception
    {
       // we need an MBeanServer to store our network registry and jndi detector services
       MBeanServer server = MBeanServerFactory.createMBeanServer();
 
       // the registry will house all remoting servers discovered
       NetworkRegistry registry = NetworkRegistry.getInstance();
       server.registerMBean(registrynew ObjectName("remoting:type=NetworkRegistry"));
       println("NetworkRegistry has been created");
 
       // register class as listener, so know when new server found
       registry.addNotificationListener(thisnullnull);
       println("NetworkRegistry has added the client as a listener");
 
       String detectorHost = InetAddress.getLocalHost().getHostName();
       
       // jndi detector will detect new network registries that come online
       JNDIDetector detector = new JNDIDetector(getConfiguration());
       // set config info for detector and start it.
       detector.setPort();
       detector.setHost(detectorHost);
       detector.setContextFactory();
       detector.setURLPackage();
 
       server.registerMBean(detectornew ObjectName("remoting:type=JNDIDetector"));
       detector.start();
       println("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
 
       return;
    }

   
Callback method from the broadcaster MBean this listener implementation is registered to. When a new server is detected, a welcome message will immediately be sent to the newly discovered server.

Parameters:
notification the notification object
handback the handback object given to the broadcaster upon listener registration
 
    public void handleNotification(Notification notification,
                                   Object handback)
    {
       // check to see if network notification
       if(notification instanceof NetworkNotification)
       {
          println("GOT A NETWORK-REGISTRY NOTIFICATION: " + notification.getType());
 
          NetworkNotification networkNotification = (NetworkNotificationnotification;
 
          if(..equals(networkNotification.getType()))
          { // notification is for new servers being added
             println("New server(s) have been detected - getting locators and sending welcome messages");
             InvokerLocator[] locators = networkNotification.getLocator();
             for(int x = 0; x < locators.lengthx++)
             {
                try
                {
                   // get the new found server's locator and invoke a call
                   InvokerLocator newServerLocator = locators[x];
                  makeInvocation(newServerLocator.getLocatorURI());
               }
               catch(Throwable throwable)
               {
                  throwable.printStackTrace();
               }
            }
         }
         else if(..equals(networkNotification.getType()))
         { // notification is for old servers that have gone down
            InvokerLocator[] locators = networkNotification.getLocator();
            for(int x = 0; x < locators.lengthx++)
            {
               println("It has been detected that a server has gone down with a locator of: " + locators[x]);
            }
         }
      }
      return;
   }

   
Make call on remoting server based on locator uri provided.

Parameters:
locatorURI the URI of the remote server we want to send the message to
Throws:
java.lang.Throwable
   public void makeInvocation(String locatorURI)
         throws Throwable
   {
      InvokerLocator locator = new InvokerLocator(locatorURI);
      println("Sending welcome message to remoting server with locator uri of: " + locatorURI);
      Client remotingClient = new Client(locatorgetConfiguration());
      remotingClient.connect();
      Object response = remotingClient.invoke("Welcome Aboard!"null);
      println("The newly discovered server sent this response to our welcome message: " + response);
      remotingClient.disconnect();
      return;
   }

   
Starts the JBoss/Remoting client.

Parameters:
args unused
   public static void main(String[] args)
   {
      println("Starting JBoss/Remoting client... to stop this client, kill it manually via Control-C");
      SimpleDetectorClient client = new SimpleDetectorClient();
      try
      {
         client.setupDetector();
         // let this client run forever - welcoming new servers when then come online
         while(true)
         {
            Thread.sleep(1000);
         }
      }
      catch(Throwable e)
      {
         e.printStackTrace();
      }
      println("Stopping JBoss/Remoting client");
   }

   
Outputs a message to stdout.

Parameters:
msg the message to output
   public static void println(String msg)
   {
      ..println(new java.util.Date() + ": [CLIENT]: " + msg);
   }
   
   

Returns:
configuration map for JNDIDetector
   protected Map getConfiguration()
   {
      return new HashMap();
   }
New to GrepCode? Check out our FAQ X