Remote Method Registry Service Stub Skeleton: Terminology Related To RMI
Remote Method Registry Service Stub Skeleton: Terminology Related To RMI
Java. RMI facilitate invocation of methods on remote object. A remote object is an object which is
residing in a different JRE.
RMI Notes 1
Steps to create a RMI based distributed application.
Step -1 Define a Remote interface that declares methods which can be remotely invoked. This interface
must extend java.rmi.Remote interface and all its methods must extend
java.rmi.RemoteException.
For example-
import java.rmi.*;
Step 2
Define a class which implements Remote interface defined in the last step & provides Exporting
Logic.
Exporting logic represents the functionality of creating proxy objects for a remote object.
o java.rmi.server.UnicastRemoteObject &
java.rmi.PortableRemoteObject classes provides Exporting Logic.
o java.rmi.server.UnicastRemoteObject is used when client & server use same
type of JRE.
o java.rmi.PortableRemoteObject is used when client & server are executed
in different type of JRE.
Program 1 (AdderSubtractor.java)
import java.rmi.*;
Program 2 (MyRemoteObject.java)
import java.rmi.*;
import java.rmi.server.*;
Step 3
Obtain stub and skeleton classes for Remote Object class. rmic tool provided with JDK is
used to generate stub & skeleton classes for Remote objects.
Syntax-
rmic ClassNameOfRemoteObject
Note : prior to jdk 1.5 both stub & skeleton used to be static proxy. From jdk 1.5
onwards skeleton is changed to be a dynamic proxy.
Step - 4
Start a Registry service.
rmiregistry tool provided with jdk act as a registry service for remoter objects i.e. it the
facility of registering & looking up remote stubs.
java.rmi.Naming class provide methods for registering and obtaining remote stubs from
RMI registry.
bind() method is used to register a remote stub.
RMI Notes 3
rebind() same as bind() method except it doesnt throws AlreadyBindException.
Program 3 (MyServer.java)
import java.rmi.*;
import java.rmi.server.*;
class MyServer
{
public static void main(String args[])
{
try
{
System.out.println("Creating remote objects.....");
MyRemoteObject o = new MyRemoteObject();
System.out.println("Registering stub.....");
Naming.bind("adderSubtractor", o);
System.out.println("Stub registered, server is ready.....");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Step - 5
Define a class for the client which performs a lookup for the stub & invokes remote methods.
Program 4 (MyClient.java)
import java.rmi.*;
import java.rmi.server.*;
import java.util.Scanner;
class MyClient
{
public static void main(String args[])
{
try
{
System.out.println("Client started, obtaining stub.....");
AdderSubtractor stub =
(AdderSubtractor)Naming.lookup("adderSubtractor");
System.out.println("Stub obtained, invoking remote methods....");
RMI Notes 4
Scanner in = new Scanner(System.in);
System.out.println("Enter first number: ");
int a=in.nextInt();
System.out.println("Enter second number: ");
int b=in.nextInt();
int c=stub.add(a, b);
int d=stub.subtract(a, b);
System.out.println("Sum is: "+c);
System.out.println("Difference is : "+ d);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
RMI Notes 5
RMI Notes 6
RMI Notes 7
If RMI registry is running on a different host on non default port then
java.rmi.registry.Registry class is used to register remote stubs. A Registry
object is obtained using factory methods provided by
java.rmi.registry.LocateRegistry class.
class MyServer
{
public static void main(String args[])
{
try
{
System.out.println("Registering stub.....");
Registry r = LocateRegistry.getRegistry("localhost",5000);
r.bind("adderSubtractor",o);
System.out.println("Stub registered, server is ready.....");
}
catch(Exception e)
{
System.out.println(e);
}
RMI Notes 8
}
}
If RMI registry is running on a different port or on a non default port then at the time of
lookup, lookup String in the following format is used
protocol:\\hostAddress or hostName:portNo\registeredName
default protocol_rmi
default host_localhost
default port_1099
class MyClient
{
public static void main(String args[])
{
try
{
System.out.println("Client started, obtaining stub.....");
AdderSubtractor stub =
(AdderSubtractor)Naming.lookup("rmi://localhost:5000/adderSubtractor");
System.out.println("Stub obtained, invoking remote methods....");
Scanner in = new Scanner(System.in);
System.out.println("Enter first number: ");
int a=in.nextInt();
System.out.println("Enter second number: ");
int b=in.nextInt();
int c=stub.add(a, b);
int d=stub.subtract(a, b);
System.out.println("Sum is: "+c);
System.out.println("Difference is : "+ d);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
RMI Notes 9