Remote
Method Invocation (RMI)
Remote Method Invocation
(RMI) allows a Java object that executes on one machine to invoke a method of a
Java object that executes on another machine. This is an important feature,
because it allows you to build distributed applications. While a complete
discussion of RMI is outside the scope of this book, the following simplified
example describes the basic principles involved.
A
Simple Client/Server Application Using RMI
This section provides
step-by-step directions for building a simple client/server application by
using RMI. The server receives a request from a client, processes it, and
returns a result. In this example, the request specifies two numbers. The
server adds these together and returns the sum.
Step
One: Enter and Compile the Source Code
This application uses four
source files. The first file, AddServerIntf.java,
defines the remote interface that is provided by the server. It contains one
method that accepts two double
arguments and returns their sum. All remote interfaces must extend the Remote interface, which is part of java.rmi. Remote defines no members. Its purpose is simply to indicate that
an interface uses remote methods. All remote methods can throw a RemoteException.
import java.rmi.*;
public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws
RemoteException;
}
The second source file, AddServerImpl.java, implements the
remote interface. The implementation of the add( ) method is straightforward. Remote objects typically extend UnicastRemoteObject, which provides
functionality that is needed to make objects available from remote machines.
import java.rmi.*; import java.rmi.server.*;
public class AddServerImpl extends
UnicastRemoteObject implements AddServerIntf {
public AddServerImpl() throws RemoteException {
}
public double add(double d1, double d2) throws
RemoteException { return d1 + d2;
}
}
The third source file, AddServer.java, contains the main
program for the server machine. Its primary function is to update the RMI
registry on that machine. This is done by using the rebind( ) method of the Naming
class (found in java.rmi). That
method associates a name with an object reference. The first argument to the rebind( ) method is a string that names
the server as "AddServer". Its second argument is a reference to an
instance of AddServerImpl.
import java.net.*; import java.rmi.*;
public class AddServer {
public static void main(String args[]) {
try {
AddServerImpl addServerImpl = new
AddServerImpl(); Naming.rebind("AddServer", addServerImpl);
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
The fourth source file, AddClient.java, implements the client
side of this distributed application. AddClient.java
requires three command-line arguments. The first is the IP address or name of
the server machine. The second and third arguments are the two numbers that are
to be summed.
The application begins by
forming a string that follows the URL syntax. This URL uses the rmi protocol. The string includes the
IP address or name of the server and the string "AddServer". The
program then invokes the lookup( )
method of the Naming class. This
method accepts one argument, the rmi
URL, and returns a reference to an object of type AddServerIntf. All remote method invocations can then be directed
to this object.
The program continues by displaying
its arguments and then invokes the remote add(
) method. The sum is returned from this method and is then printed.
import java.rmi.*;
public class AddClient {
public static void main(String args[]) { try {
String addServerURL = "rmi://" + args[0]
+ "/AddServer"; AddServerIntf addServerIntf =
(AddServerIntf)Naming.lookup(addServerURL);
System.out.println("The first number is: " + args[1]); double d1 =
Double.valueOf(args[1]).doubleValue(); System.out.println("The second
number is: " + args[2]);
double d2 =
Double.valueOf(args[2]).doubleValue(); System.out.println("The sum is:
" + addServerIntf.add(d1, d2));
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
After you enter all the code,
use javac to compile the four source
files that you created.
Step
Two: Manually Generate a Stub if Required
In the context of RMI, a stub is a Java object that resides on
the client machine. Its function is to present the same interfaces as the
remote server. Remote method calls initiated by the client are actually
directed to the stub. The stub works with the other parts of the RMI system to
formulate a request that is sent to the remote machine.
A remote method may accept arguments that are
simple types or objects. In the latter case, the object may have references to
other objects. All of this information must be sent to the remote machine. That is,
an object passed as an argument to a remote method call must be serialized and
sent to the remote machine. Recall from Chapter 20 that the serialization
facilities also recursively process all referenced objects.
If a response must be
returned to the client, the process works in reverse. Note that the
serialization and deserialization facilities are also used if objects are
returned to a client.
Prior to Java 5, stubs needed
to be built manually by using rmic.
This step is not required for modern versions of Java. However, if you are
working in a legacy environment, then you can use the rmic compiler, as shown here, to build a stub:
rmic AddServerImpl
This command generates the
file AddServerImpl_Stub.class. When
using rmic, be sure that CLASSPATH is set to include the current
directory.
Step
Three: Install Files on the Client and Server Machines
Copy AddClient.class, AddServerImpl_Stub.class
(if needed), and AddServerIntf.class
to a directory on the client machine. Copy AddServerIntf.class,
AddServerImpl.class, AddServerImpl_Stub.class (if needed),
and AddServer.class to a directory
on the server machine.
Step
Four: Start the RMI Registry on the Server Machine
The JDK provides a program
called rmiregistry, which executes
on the server machine. It maps names to object references. First, check that
the CLASSPATH environment variable
includes the directory in which your files are located. Then, start the RMI
Registry from the command line, as shown here:
start rmiregistry
When this command returns,
you should see that a new window has been created. You need to leave this
window open until you are done experimenting with the RMI example.
Step
Five: Start the Server
The server code is started
from the command line, as shown here: java AddServer
Recall that the AddServer code instantiates AddServerImpl and registers that object
with the name "AddServer".
Step
Six: Start the Client
The AddClient software requires three arguments: the name or IP address
of the server machine and the two numbers that are to be summed together. You
may invoke it from the command line by using one of the two formats shown here:
java AddClient server1 8 9
java AddClient 11.12.13.14 8 9
In the first line, the name
of the server is provided. The second line uses its IP address (11.12.13.14).
You can try this example
without actually having a remote server. To do so, simply install all of the
programs on the same machine, start rmiregistry,
start AddServer, and then execute AddClient using this command line:
java AddClient 127.0.0.1 8 9
Here, the address 127.0.0.1
is the “loop back” address for the local machine. Using this address allows you
to exercise the entire RMI mechanism without actually having to install the
server on a remote computer. (If you are using a firewall, then this approach
may not work.)
In either case, sample output
from this program is shown here:
The first number is: 8
The second number is: 9
The sum is: 17.0
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.