1
Chapter Six: Remote Method Invocation (RMI)
Chapter 6: Remote Method Invocation
IMPORT JAVA.RMI.*;
Remote Method Invocation 2
► It is a mechanism that allows an object residing in one system (JVM) to
Chapter Six: Remote Method Invocation (RMI)
access/invoke an object running on another JVM.
► The RMI -API enables client and server communications over the net. Typically,
client programs send requests to a server program, and the server program
responds to those requests.
“A common example is sharing a word processing program over a network. The
word processor is installed on a server, and anyone who wants to use it starts it
from his or her machine by double clicking an icon on the desktop or typing at
the command line. The invocation sends a request to a server program for
access to the software, and the server program responds by making the software
available to the requestor.”
Remote Method Invocation 3
► The RMI API lets you create a publicly accessible remote server object that
Chapter Six: Remote Method Invocation (RMI)
enables client and server communications through simple method calls on the
server object.
❑ Clients can easily communicate directly with the server object and indirectly
with each other through the server object using Uniform Resource Locators
(URLs) and HyperText Transfer Protocol (HTTP).
► RMI is used to build distributed applications; it provides remote communication
between Java programs. It is provided in the package java.rmi.
► The RMI provides remote communication between the applications using two
objects stub and skeleton.
Goal of RMI 4
Chapter Six: Remote Method Invocation (RMI)
Reasons for Using RMI
❑ Some operations can be executed significantly faster on the remote system
than the local client computer.
❑ Specialized data or operations are available on the remote system that
cannot be replicated easily on the local system, for example, database
access.
❑ Simpler than programming our own TCP sockets and protocols.
► Implement distributed objects.
► Have a program running on one machine invoke a method belonging to an
object whose execution is performed on another machine.
Understanding stub and skeleton 5
Chapter Six: Remote Method Invocation (RMI)
RMI uses stub and skeleton object for communication with the
remote object. A remote object is an object whose method can be
invoked from another JVM.
Stub
The stub is an object, acts as a gateway for the client side. All the
outgoing requests are routed through it.
It resides at the client side and represents the remote object.
Understanding stub and skeleton 6
Chapter Six: Remote Method Invocation (RMI)
Stub
When the caller invokes method on the stub object, it does
the following tasks:
1.It initiates a connection with remote Virtual Machine (JVM),
2.It writes and transmits (marshals) the parameters to the remote Virtual
Machine (JVM),
3.It waits for the result
4.It reads (unmarshals) the return value or exception, and
5.It finally, returns the value to the caller.
Understanding stub and skeleton 7
skeleton
Chapter Six: Remote Method Invocation (RMI)
The skeleton is an object, acts as a gateway for the server side
object. All the incoming requests are routed through it.
▪ When the skeleton receives the incoming request, it does the following tasks:
1. It reads the parameter for the remote method
2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.
Architecture of an RMI Application 8
Chapter Six: Remote Method Invocation (RMI)
In an RMI application, we write two programs, a server program
(resides on the server) and a client program (resides on the client).
✔ Inside the server program, a remote object is created and
reference of that object is made available for the client (using
the registry).
✔ The client program requests the remote objects on the server and
tries to invoke its methods.
Architecture of an RMI Application 9
Chapter Six: Remote Method Invocation (RMI)
The following diagram shows the architecture of an RMI application.
Architecture of an RMI Application 10
Chapter Six: Remote Method Invocation (RMI)
Transport Layer − This layer connects the client and the server. It
manages the existing connection and also sets up new connections.
Stub − A stub is a representation (proxy) of the remote object at client.
It resides in the client system; it acts as a gateway for the client program.
Skeleton − This is the object which resides on the server side. stub
communicates with this skeleton to pass request to the remote object.
RRL(Remote Reference Layer) − It is the layer which manages the
references made by the client to the remote object.
Working of an RMI Application 11
Chapter Six: Remote Method Invocation (RMI)
The following points summarize how an RMI application works −
✔ When the client makes a call to the remote object, it is received by the
stub which eventually passes this request to the RRL.
✔ When the client-side RRL receives the request, it invokes a method called
invoke() of the object remoteRef. It passes the request to the RRL on the
server side.
✔ The RRL on the server side passes the request to the Skeleton (proxy on
the server) which finally invokes the required object on the server.
✔ The result is passed all the way back to the client.
The General RMI Architecture 12
Chapter Six: Remote Method Invocation (RMI)
The server must first bind its name to
the registry
► The client lookup the server name in
the registry to establish remote
references.
► The Stub serializing the parameters
to skeleton, the skeleton invoking
the remote method and serializing
the result back to the stub.
RMI Registry
► RMI registry is a namespace on which all server objects are placed. Each 13
time the server creates an object, it registers this object with the
Chapter Six: Remote Method Invocation (RMI)
RMIregistry (using bind() or reBind() methods). These are registered using
a unique name known as bind name.
► To invoke a remote object, the client needs a reference of that object.
At that time, the client fetches the object from the registry using its bind
name (using lookup() method).
Steps for Developing an RMI 14
System
Chapter Six: Remote Method Invocation (RMI)
1. Define the remote interface.
2. Implement Server program (by implementing the interface).
3. Develop the client program.
4. Compile the Java source files.
5. Generate the client stubs and server skeletons.
6. Start the RMI registry.
7. Start the remote server objects.
8. Run the client
Step 1: Defining the Remote Interface 15
Chapter Six: Remote Method Invocation (RMI)
► To create an RMI application, the first step is the defining of a remote
interface between the client and server objects.
► The client need to access methods in the remote objects on the server.
Therefore, an interface needs to be defined for both client and server.
/* SampleServer.java */
import java.rmi.*;
public interface SampleServer extends Remote
{
public int sum(int a,int b) throws RemoteException;
}
► In this interface which allows clients to access. Please be careful with
throws java.rmi.RemoteException because it has to be added in RMI
file.
Step 2: Develop the Server program 16
► Develop the remote object and its interface. The server is a simple
Chapter Six: Remote Method Invocation (RMI)
unicast remote server.
► Create server by extending java.rmi.server.UnicastRemoteObject. The server
uses the RMISecurityManager to protect its resources while
engaging in remote communication.
/* SampleServerImpl.java */
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SampleServerImpl extends UnicastRemoteObject
implements SampleServer
{
SampleServerImpl() throws RemoteException
{
super();
}
Step 2: Develop the Server program 17
Chapter Six: Remote Method Invocation (RMI)
Implement the remote methods
/* SampleServerImpl.java */
public int sum(int a,int b) throws RemoteException
{
return a + b;
}
}
► The server must bind its name to the registry, the client will look up the
server name.
► Use java.rmi.Naming class to bind the server name to registry. In this
example the name is called “SAMPLE-SERVER”.
► In the main method of your server object, the RMI security manager is
created and installed.
Step 2: Develop the Server program 18
/* SampleServerImpl.java */
public static void main(String args[])
Chapter Six: Remote Method Invocation (RMI)
{
try
{
//create a local instance of the object
SampleServerImpl Server = new SampleServerImpl();
//put the local instance in the registry
Naming.rebind("SAMPLE-SERVER" , Server);
System.out.println("Server waiting.....");
}
catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString()); }
catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString()); }
}
Step 3: Develop the client program
19
► In order for the client object to invoke methods on the server, it must first
Chapter Six: Remote Method Invocation (RMI)
look up the name of server in the registry. You use the java.rmi.Naming
class to lookup the server name.
► The server name is specified as URL in the form of( rmi://host:port/name )
❑ Default RMI port is 1099.
❑ The name specified in the URL must exactly match the name that the
server has bound to the registry. In this example, the name is
“SAMPLE-SERVER”
► The remote method invocation is programmed using the remote
interface name (remoteObject) as prefix and the remote method name
(sum) as suffix.
Step 3: Develop the client program 20
try
import java.rmi.*;
Chapter Six: Remote Method Invocation (RMI)
{
import java.rmi.server.*; System.out.println("Security Manager loaded");
public class SampleClient String url = "//localhost/SAMPLE-SERVER";
SampleServer remoteObject =
{ (SampleServer)Naming.lookup(url);
public static void main(String[] args) System.out.println("Got remote object");
System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) );
{
}
//get the remote object from the catch (RemoteException exc) {
registry System.out.println("Error in lookup: " + exc.toString()); }
catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString()); }
catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
}
Step 4 & 5: Compile the Java source files & 21
Generate the client stubs and server skeletons
Chapter Six: Remote Method Invocation (RMI)
► Once the interface is completed, you need to generate stubs and skeleton code.
► The RMI system provides an RMI compiler (rmic) that takes your generated
interface class and procedures stub code on its self.
► Stub act as a gateway for Client program. It resides on Client side and
communicate with Skeleton object. It establish the connection between remote
object and transmit request to it.
Step 6: Start the RMI registry 22
Chapter Six: Remote Method Invocation (RMI)
The RMI applications need install to Registry. And the Registry must
start manual by call rmiregisty.
► The rmiregistry us uses port 1099 by default. You can also bind
rmiregistry to a different port by indicating the new port number as :
rmiregistry <new port>
-> start rmiregistry
Steps 7 & 8: Start the remote server objects & 23
Run the client
Chapter Six: Remote Method Invocation (RMI)
► Once the Registry is started, the server can be started and will be
able to store itself in the Registry.
Required Files Summary 24
Chapter Six: Remote Method Invocation (RMI)
The files needed for creating a Java RMI application are:
▪ A remote interface defines the remote interface provided by the
service. Usually, it is a single line statement specifies the service function
(SampleServer.java). (An interface is the skeleton for a public class.)
▪ A remote object implements the remote service. It contains a
constructor and required functions. (SampleServerImpl.java)
▪ A client that invokes the remote method. (SampleClient.java)
▪ The server offers the remote service, installs a security manager and
contacts rmiregistry with an instance of the service under the name of
the remote object. (SampleServer.java)
Practical Reading Assignment 25
Chapter Six: Remote Method Invocation (RMI)
► Java RMI - Database Application
► Java RMI - GUI Application
Chapter Six: Remote Method Invocation (RMI)
26
End of Chapter 6