5.Writing RMI Client

Writing a RMI client

What good is a service, if you don't write a client that uses it? Writing clients is the easy part - all a client has to do is call the registry to obtain a reference to the remote object, and call its methods. All the underlying network communication is hidden from view, which makes RMI clients simple.

Our client must first assign a security manager, and then obtain a reference to the service. Note that the client receives an instance of the interface we defined earlier, and not the actual implementation. Some behind-the-scenes work is going on, but this is completely transparent to the client.

// Assign security manager
    if (System.getSecurityManager() == null)
    {
        System.setSecurityManager   (
new RMISecurityManager());
    }

    // Call registry for PowerService
    PowerService service = (PowerService) Naming.lookup
        ("rmi://" + args[0] + "/PowerService");

To identify a service, we specify an RMI URL. The URL contains the hostname on which the service is located, and the logical name of the service. This returns a PowerService instance, which can then be used just like a local object reference. We can call the methods just as if we'd created an instance of the remote PowerServiceServer ourselves.

// Call remote method
System.out.println    ("Answer : " + service.square(value));

        // Call remote method
        System.out.println    ("Answer : " + service.power(value,power));

Writing RMI clients is the easiest part of building distributed services. In fact, there's more code for the user interface menu in the client than there is for the RMI components! To keep things simple, there's no data validation, so be careful when entering numbers. Here's the full source code for the RMI client.

import java.rmi.*;
import java.rmi.Naming;
import java.io.*;

//
//
// PowerServiceClient
//
//
public class PowerServiceClient
{
public static void main(String args[]) throws Exception
{
// Check for hostname argument if (args.length != 1)
{
System.out.println
("Syntax - PowerServiceClient host");
System.exit(1);
}

// Assign security manager if (System.getSecurityManager() == null)
{
System.setSecurityManager
(
new RMISecurityManager());
}

// Call registry for PowerService PowerService service = (PowerService) Naming.lookup ("rmi://" + args[0] + "/PowerService"); DataInputStream din = new
DataInputStream (System.in);

for (;;)
{
System.out.println
("1 - Calculate square");
System.out.println
("2 - Calculate power");
System.out.println
("3 - Exit"); System.out.println();
System.out.print ("Choice : ");

String line = din.readLine();
Integer choice =
new Integer(line);

int value = choice.intValue();

switch (value)
{
case 1:
System.out.print ("Number : ");
line = din.readLine();System.out.println();
choice =
new Integer (line);
value = choice.intValue();

// Call remote method System.out.println ("Answer : " + service.square(value)); break;
case 2:
System.out.print ("Number : ");
line = din.readLine();
choice =
new Integer (line);
value = choice.intValue();

System.out.print ("Power : ");
line = din.readLine();
choice =
new Integer (line);
int power = choice.intValue();

// Call remote method System.out.println ("Answer : " + service.power(value, power)); break;
case 3:
System.exit(0);
default :
System.out.println ("Invalid option");
break;
}
}
}

}