Lecture 27 Remote Method Invocation
Lecture 27 Remote Method Invocation
Invocation
UNIT IV
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 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.
• rmic AddServerImpl
• This command generates two new files:
• AddServerImpl_Skel.class (skeleton)
• AddServerImpl_Stub.class (stub).
• When using rmic, be sure that CLASSPATH is set to include the current directory.
• As you can see, by default, rmic generates both a stub and a skeleton file. If you do not
need the skeleton, you have the option to suppress it.
Step Three: Install Files on the
Client and Server Machines
• Copy AddClient.class, AddServerImpl_Stub.class, and
AddServerIntf.class to a directory on the client machine.
• Copy AddServerIntf.class, AddServerImpl.class,
AddServerImpl_Skel.class, AddServerImpl_Stub.class, and
AddServer.class to a directory on the server machine.
Step Four: Start the RMI
Registry on the Server Machine
• The Java 2 SDK 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.
• 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 AddSever,
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.
• 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
RMI Exercise for students
FILE SERVER IMPLEMENTATION USING RMI
That means that a client requesting the service(invoking the service openFile remotely) has to pass to the
server a string representing the name of the file to be opened.
The server will execute the service and then return to the client an int representing either success or the
code for an error message.
Registry
2
1
server
client 3
4
1. Server implements the method and registers an object that can be used by a client to a registry ( a database that
keeps track of all services and their locations). The client is aware of the remote object. It contacts the registry
to obtain information about the location of the server node, the port number where the process resides and the
object id number (Many clients can request the service simultaneously).
2. The registry responds with information about the server’ s IP address , port number for the service, object
id number etc.
3. The client sends a request to invoke the service on the server.
4. The server executes the service and returns the results to the client.
IMPLEMENTATION USING RMI
• Most of the communications discussed thus far are created by providing a single line of code in our coding of
the corresponding processes. The RMI system and the local O.S. handle the rest.
• SERVER CODING:
• Create an interface that represents the service:
• public interface MyRemoteFileSystem exetnds Remote {
public int openFile ( String name) throws RemoteException;
}
• Create a class that implements the remote service (method).
• public class MyRemoteFileSystemImpl extends UnicastRemoteObject implements MyRemoteFileSystem {
//constructor that calls super(); . Super is a call to the superclasse’ s constructor. In the case the super class creates network
connections. Nothing else is needed on the part of the developer.
IMPLEMENTATION USING RMI
// implement the code for the remotely invokable method
public int openFile(name) { //code to open the file
return int; }
//main method for the class that starts the server
public static void main ( String [ ] args) {
// start the server by instantiating an instance of the class
MyRemoteFileSystemImpl rfs = new MyRemoteFileSystemImpl ( ) ;
//create a string that represents the location of the registry and the name of a remote
object that can be used by a client to invoke the service
String serverobjectname = “//localhost/remoteobject;
// we assume that the registry resides localhost (or it can be the ip address of another
node
//proceed to register the remote object with the registry Naming.rebind
( serverobjectname, rfs ); }//end of main
}// end of the class THTAT’S ALL IS NEEDED FOR THE
SERVER!
IMPLEMENTATION USING RMI
• CLIENT CODING
• Create a class that represents the client process.
public class Client {
// global declarations as needed
//constructor
//other methods as needed 9including a main method for the client
// method that needs to open a file from the File Server node
public void getRemoteFile ( ) {
String serverobjectname = “//localhost/remoteobject;
// lookup remote object on the registry. Notice that the name of the interface is used below
MyRemoteFileSystem myfile = mew (MyRemoteFileSystem) Naming.lookup ( serverobjectname) ;
//proceed with remote invocation
int c = myfile. openFile(filename) ;
// where filename is a string representing the name of the file. It is passed to the server. The server returns the result which is capured by the int c.
} //end of the method
}//end of the client class
IMPLEMENTATION USING RMI
• As you can see the implementation is simple. Most of the work is handled by the system
under the hood.
• COMPILING
• Compile the server interface class first by using javac.
• Compile the server class that implemets the interface next by using the javac
compiler
• Compile the server class that implements the interface one more time by using the
rmic compiler (it comes with the jdk). This produces an extra class called the stub
> rmic –v1.2 MyRemoteFileSystemImpl //notice that no extension is used after
the file name
The file MyRemoteFile SystemImpl_stub.class is produced
• Compile the client class by using javac compilker
IMPLEMENTATION USING RMI
• MAKING IT WORK
• Place the compiled interface file ( MyRemoteInterface.class) in the client node (in addition to the server node)
• Place the stub (MyRemoteFileSystemImpl_stub.class) in the client node also (in addition to the server node).
• Place the server MyRemoteFielSystem.class on the server node
• Place the client code on the client node.
• Start the registry. RMI provides registry automatically > start rmiregistry
• Start the server >java MyRemoteFileSystemImpl
• Start the client >java Client
• Note: In windows open a Command Prompt window for each process.