Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

CHAPTER SIX

RMI (Remote Method Invocation)

1
Outline
 Remote Method Invocation (RMI)
 Understanding stub and skeleton
 Steps to write the RMI program
 RMI Example

2
Remote Method Invocation (RMI)
 The RMI is an API that provides a mechanism to create distributed
application in java.
 The RMI allows an object to invoke methods on an object running in
another JVM.
 The RMI provides remote communication between the applications
using two objects stub and skeleton.

3
Remote Procedure Calls
 Remote procedure call (RPC) abstracts procedure calls between
processes on networked systems.
 Stub – client-side proxy for the actual procedure on the server. Server
has a similar stub as well.
 The client-side stub locates the server and marshals the parameters.
 The server-side stub receives this message, unpacks the marshaled
parameters, and performs the procedure on the server.

4
Goal of RMI
 Implement distributed objects.
oHave a program running on one machine invoke a method belonging to
an object whose execution is performed on another machine.
 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,
o For example, database access.
Simpler than programming our own TCP sockets and protocols.
What is the difference between RMI and Socket(TCP/UDP)?

5
Understanding stub and skeleton
 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.

6
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.
 When the caller invokes method on the stub object, it does the
following tasks:
initiates a connection with remote Virtual Machine (JVM),
writes and transmits (marshals) the parameters to the remote Virtual Machine
(JVM),
waits for the result
reads (unmarshals) the return value or exception, and
finally, returns the value to the caller.

7
skeleton
 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:
It reads the parameter for the remote method
It invokes the method on the actual remote object, and
It writes and transmits (marshals) the result to the caller.
call

skeleton
Stub

RMI Client RMI Server


return

8
RMI Registry
 The RMI registry is a simple server-side bootstrap naming facility that
allows remote clients to get a reference to a remote object
 Servers name and register their objects to be accessed remotely with
the RMI Registry.
 Clients use the name to find server objects and obtain a remote
reference to those objects from the RMI Registry.
 A registry (using the rmiregistry command) is a separate process
running on the server machine.

9
Steps for Developing an RMI System
 There are 6 steps to write the RMI program
1. defining the remote interface
2. Provide the implementation of the remote interface
3. Compile the implementation class and create the stub and skeleton objects
using the rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application

10
1. create the remote interface
 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;
}
11
2. Provide the implementation of the remote
interface
 For providing the implementation of the Remote interface, we need to
Either extend the UnicastRemoteObject class,
or use the exportObject() method of the UnicastRemoteObject class
/* SampleServerImpl.java */
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SampleServerImpl extends UnicastRemoteObject
implements SampleServer{
SampleServerImpl() throws RemoteException
{
super();
}
public int sum(int a,int b) throws RemoteException
{
return a + b;
}}
12
3. create the stub and skeleton objects using the
rmic tool.
 Next step is to create stub and skeleton objects using the rmi compiler.
 The rmic tool invokes the RMI compiler and creates stub and skeleton
objects.
 rmic SampleServer

13
4. Start the registry service by the rmiregistry
tool
 start the registry service by using the rmiregistry tool.
 If you don't specify the port number, it uses a default port number.
 rmiregistry 1099

14
5. Create and run the server application
 rmi services need to be hosted in a server process.
 The Naming class provides methods to get and store the remote object.
 The Naming class provides 5 methods.
1. public static java.rmi.Remote lookup(java.lang.String) throws
java.rmi.NotBoundException,java.net.MalformedURLException,
java.rmi.RemoteException;
oit returns the reference of the remote object.
2. public static void bind(java.lang.String, java.rmi.Remote) throws
java.rmi.AlreadyBoundException, java.net.MalformedURLException,
java.rmi.RemoteException;
oit binds the remote object with the given name.
3. Public static void unbind(java.lang.String) throws
java.rmi.RemoteException,java.rmi.NotBoundException,java.net.Malf
ormedURLException;
oit destroys the remote object which is bound with the given name. 15
…Create and run the server application
4.public static void rebind(java.lang.String, java.rmi.Remote) throws
java.rmi.RemoteException, java.net.MalformedURLException;
oit binds the remote object to the new name.
5.public static java.lang.String[] list(java.lang.String) throws
java.rmi.RemoteException, java.net.MalformedURLException;
oit returns an array of the names of the remote objects bound in the
registry.

16
…Create and run the server application
import java.rmi.*;
import java.rmi.registry.*;
public class MyServer{
public static void main(String args[]){
try{
//create a local instance of the object
SampleServer server = new SampleServerImpl();
//put the local instance in the registry
Naming.rebind("rmi://localhost:1099/SAMPLE-SERVER ", server);
}catch(Exception e){System.out.println(e);}
}
}
17
6.Create and run the client application
 In order for the client object to invoke methods on the server, it must
first 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 (
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 RMI is programmed using the remote interface name
(remoteObject) as prefix and the remote method name (sum) as suffix.

18
6.Create and run the client application
import java.rmi.*;
public class MyClient{
public static void main(String args[]){
//get the remote object from the registry
try
{
String url = “rmi://localhost/SAMPLE-SERVER";
SampleServer remoteObject = (SampleServer)Naming.lookup(url);
System.out.println("Got remote object");
System.out.println(" 15 + 5 = " + remoteObject.sum(15,5) );
}catch(Exception e){}
}
}
19
Running rmi example
1) compile all the java files
javac *.java
2)create stub and skeleton object by rmic tool
rmic SampleServe
3)start rmi registry in one command prompt
rmiregistry 1099
4)start the server in another command prompt
java MyServer
5)start the client application in another command prompt
java MyClient
20
Output of this RMI example

21
22

You might also like