ComputeEngineClient
Home ] Up ] ComputeEngineImpl ] Security Policies ] ComputePi ] [ ComputeEngineClient ] Running the Example ]

 

 

Here's the ComputeEngineClient class.

Note that the original version on the Java RMI Tutorial web site also set a security manager for the client.  However, I found that it did not appear to be necessary for the application to work.

package rmiClient;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import rmi.ComputeEngine;

/**
 * A class to act as a compute engine client
 * for a remote RMI compute engine object.
 *
 * @author Bryan Higgs
 * @version 1.0
 */
public class ComputeEngineClient
{
  /**
   * Main entry point for client
   * @param args the command line arguments
   *             args[0] contains the server hostname
   *             args[1] contains the port number for the registry
   */
  public static void main(String[] args)
  {
    // Determine server hostname
    String host = "localhost";
    if (args.length > 0)
      host = args[0];
    
    // Determine the port number
    int port = 1099;  // the default port number for the registry
    if (args.length > 1)
    {
      try
      {
        port = Integer.parseInt(args[1]);
      }
      catch (NumberFormatException nfe)
      {
        System.err.println("Port number " + args[1] + " invalid");
        return;
      }
    }
    
    System.out.println("Using port " + port);
    
    // Construct the base URL for the lookups.
    String url = "rmi://" + host + ":" + port + "/ComputeEngine";
    
    // Obtain the initial naming context for later...
    try
    {
      // Locate the compute engine in the registry
      System.out.println("Locating remote ComputeEngine...");
      ComputeEngine engine = (ComputeEngine) Naming.lookup(url);
      
      System.err.println("Creating ComputePi instance...");
      ComputePi task = new ComputePi(40); // Compute PI to 40 places
      
      System.err.println("Requesting remote computation...");
      BigDecimal pi = engine.compute(task);
      
      System.err.println("Result: PI = " + pi);    
    }
    catch (NotBoundException nbe)
    {
      nbe.printStackTrace();
    }
    catch (MalformedURLException ex)
    {
      ex.printStackTrace();
    }
    catch (RemoteException ex)
    {
      ex.printStackTrace();
    }
  }
}

Note that it:

  • Uses java.rmi.Naming to perform the look up.
  • Creates an instance of ComputePi, specifying 40 places.
  • Calls the ComputeEngine's compute() method, passing in a reference to the ComputePi instance.
  • Prints out the result.
 
The page was last updated February 19, 2008