0% found this document useful (0 votes)
52 views9 pages

Remote Method Registry Service Stub Skeleton: Terminology Related To RMI

RMI allows developing distributed Java applications by facilitating remote method invocation on objects located in a different JVM. It uses stubs as client-side proxies and skeletons as server-side proxies to invoke methods remotely in a transparent manner. Developing an RMI application involves defining a remote interface, implementing it to export the remote object, generating stubs and skeletons, starting the RMI registry, binding the remote object, and looking it up from the client to invoke methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views9 pages

Remote Method Registry Service Stub Skeleton: Terminology Related To RMI

RMI allows developing distributed Java applications by facilitating remote method invocation on objects located in a different JVM. It uses stubs as client-side proxies and skeletons as server-side proxies to invoke methods remotely in a transparent manner. Developing an RMI application involves defining a remote interface, implementing it to export the remote object, generating stubs and skeletons, starting the RMI registry, binding the remote object, and looking it up from the client to invoke methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

RMI is Java API that provides basic infrastructure for developing Distributed Applications in

Java. RMI facilitate invocation of methods on remote object. A remote object is an object which is
residing in a different JRE.

Terminology related to RMI-


o Remote Method A method that is invoked on a remote object using RMI.
o Registry Service is a binding and lookup service that is used to provide location
transparency to remote objects.
o Stub-is a proxy of remote object that is made available to the client using registry service
and hides the complexity of Remote Method Invocation from client.
o Skeleton- is a proxy of client that invokes remote method on the remote object on
behalf of the client.

1.1 Stub & Skeleton are created for remote object.


1.2 Stub is registered.
2.0 Stub is looked up.
2.1 Stub is returned.
2.2 Remote method is invoked.
2.3 Connection to the skeleton is made & method call is marshaled.
2.4 Method call is unmarshalled and method is invoked on remote object.
2.5 Method is completed & result is returned.
2.6 Result is marshalled back to stub.
2.7 Result is unmarshalled & provided to client.

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.*;

public interface AdderSubractor extends Remote


{
public int add(int x, int y) throws RemoteException;
public int subtract(int x, int y) throws RemoteException;

Note : Remote is a marker interface.


public interface Remote
{}

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.*;

public interface AdderSubractor extends Remote


{
public int add(int x, int y) throws RemoteException;
public int subtract(int x, int y) throws RemoteException;

Program 2 (MyRemoteObject.java)
import java.rmi.*;
import java.rmi.server.*;

class MyRemoteObject extends UnicastRemoteObject implements AdderSubtractor


{
RMI Notes 2
public MyRemoteObject() throws RemoteException
{
super();
}

public int add(int x, int y)


{
System.out.println("add() method of MyRemoteObject is invoked.");
return x+y;
}

public int subtract(int x, int y)


{
System.out.println("subtract() method of MyRemoteObject is invoked.");
return x-y;
}
}

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

Proxy are of two types-


a. static
b. dynamic

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.

Syntax of starting registry service-


1. on default port (1099):-
rmiregistry
2. on non default port :-
rmiregistry portNo.

java.rmi.Naming class provide methods for registering and obtaining remote stubs from
RMI registry.
bind() method is used to register a remote stub.

public static void bind(String name, Remote Object) throws RemoteException,


AlreadyBindException;

RMI Notes 3
rebind() same as bind() method except it doesnt throws AlreadyBindException.

public static rebind(String name, Remote Object) throws RemoteException;

lookup() method is used to obtain a stub.

public static Remote lookup(String name) throws RemoteException;

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);
}
}
}

Screenshots of above example -

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.

public static Registry getRegisty(int port);


public static Registry getRegisty(String hostName);
public static Registry getRegisty(String hostName, int port);

Changes need to be made in Program 3 (MyServer.java)


import java.rmi.*;
import java.rmi.server.*;

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

Changes need to be made in 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("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

You might also like