0% found this document useful (0 votes)
3 views

Lecture 27 Remote Method Invocation

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 27 Remote Method Invocation

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Remote Method

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.

This application uses four source files.


AddServerIntf.java
• AddServerIntf.java
• It 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 purposeis simply to indicate that an interface uses remote
methods. All remote methods can throw a RemoteException.
AddServerIntf.java
import java.rmi.*;
public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws
RemoteException;
}
AddServerImpl.java
• It implements the remote interface.
• The implementation of the add( ) method is straightforward.
• All remote objects must extend UnicastRemoteObject, which
provides functionality that is needed to make objects available from
remote machines.
AddServerImpl.java
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;
}
}
AddServer.java
• It 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.
AddServer.java
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);
}
}
}
AddClient.java
• It implements the client side of this distributed application.
• AddClient.java requires three command line arguments.
• Thefirst 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.
AddClient.java
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]);
AddClient.java
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: " +
ddServerIntf.add(d1, d2));
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
STEP TWO : Generate Stubs and
Skeletons
• Before you can use the client and server, you must generate the
necessary stub.
• You may also need to generate a skeleton.
• 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.
STEP TWO : Generate Stubs and
Skeletons
• 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.
• An object passed as an argument to a remote method call must be
serialized and sent to the remote machine.
• Serialization facilities to recursively process all referenced objects.
STEP TWO : Generate Stubs and
Skeletons
• Skeletons are not required by Java 2.
• However, they are required for the Java 1.1 RMI model. Because of
this, skeletons are still required for compatibility between Java 1.1
and Java 2.
• A skeleton is a Java object that resides on the server machine.
• It works with the other parts of the 1.1 RMI system to receive
requests, perform deserialization, and invoke the appropriate code on
the server.
• Again, the skeleton mechanism is not required for Java 2 code that
does not require compatibility with 1.1.
STEP TWO : Generate Stubs and
Skeletons
• 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.
• To generate stubs and skeletons, you use a tool called the RMI compiler, which is invoked
from the command line

• 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

SUPPOSE A SERVER HAS THE FILE SERVICE

int openFile(String name).

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.

The following communications take place:


IMPLEMENTATION USING RMI

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.

You might also like