0% found this document useful (0 votes)
4 views25 pages

RMI in Distributed Programming Guide

This document provides detailed lesson notes on Remote Method Invocation (RMI) in distributed programming, including objectives, architecture, and practical exercises. It explains the roles of stubs and skeletons in RMI, how to create RMI applications, and the advantages of using RMI for distributed computing. The content is structured to guide learners through understanding and implementing RMI in Java applications.

Uploaded by

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

RMI in Distributed Programming Guide

This document provides detailed lesson notes on Remote Method Invocation (RMI) in distributed programming, including objectives, architecture, and practical exercises. It explains the roles of stubs and skeletons in RMI, how to create RMI applications, and the advantages of using RMI for distributed computing. The content is structured to guide learners through understanding and implementing RMI in Java applications.

Uploaded by

Big Fabiano
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

One

Step
At A
Time

DISTRIBUTED PROGRAMMING
Detailed Lesson notes with practical exercises and applications in Distributed Programming

MODULE 3
REMOTE METHOD INVOCATION (RMI)

By Mr. NYAMBI BLAISE


PhD Student/PLET in Computer Science
Masters in Industrial Computing & Electronics
Software Engineer
Tel: 679194380
[Link]@[Link]

Version : December 2021


Objectives
Upon successful completion of this module, you should be able to:
• Demonstrate knowledge of the RMI
• Be able to write server and client programs that work with RMI
• Solve Distributed Computing problems using the RMI
• Explain the architecture of the RMI architecture
• Explain the concepts of Skeleton and Stub
Contents
1 General Introduction .................................................................................................................................. 2
1.1 Understanding Stub And Skeleton ...................................................................................................... 4
1.1.1 Stub .............................................................................................................................................. 4
1.1.2 Skeleton........................................................................................................................................ 5
1.2 Architecture of an RMI Application ................................................................................................... 5
1.3 Working of an RMI Application ......................................................................................................... 6
1.4 Marshalling and Unmarshalling .......................................................................................................... 7
1.5 RMI Registry ...................................................................................................................................... 7
1.6 Goals of RMI ...................................................................................................................................... 7
1.7 Advantages of RMI ............................................................................................................................. 8
1.8 Creating a Java RMI Application...................................................................................................... 10
Step 1: Create the Remote interface .................................................................................................... 10
Step 2: Implementation of the remote interface .................................................................................. 10
Step 3: Create the server program. ...................................................................................................... 10
Step 4: Create the client program. ....................................................................................................... 10
Step 5: Compile all the java programs ................................................................................................ 10
Step 6: Create a stub and skeleton : rmic Implementation class ......................................................... 10
Step 7: Start the registry service by the rmiregistry tool: rmiregistry ................................................. 10
Step 8: Start the Server........................................................................................................................ 10
Step 9: Start the Client ........................................................................................................................ 10
1.8.0 Example 1 – First RMI Application ........................................................................................... 10
1.8.1 Defining the Remote Interface ................................................................................................... 10
1.8.2 Developing the Implementation Class (Remote Object) ........................................................... 11
1.8.3 Developing the Server Program ................................................................................................. 12

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 1
1.8.4 Developing the Client Program.................................................................................................. 13
1.9 Example 2 – Calculator RMI Application ........................................................................................ 14
1.9.1 Example 2 – Calculator RMI Application ................................................................................. 14
1.9.2 Step 1: Create the Remote interface ........................................................................................... 14
1.9.3 Step 2: Implementation Of The Remote Interface ..................................................................... 15
1.9.4 Step 3: Creating the Server application program. ...................................................................... 16
1.9.5 Step 4: Creating The Client Application Program ..................................................................... 17
1.9.6 Step 5: Compiling all the java programs .................................................................................. 18
1.9.7 Step 6: Create a stub and skeleton ............................................................................................. 19
1.9.8 Step: 7 Start The Registry Service Using The rmiregistry Tool ................................................ 19
1.9.9 Step: 8 Start The Server ............................................................................................................. 19
1.9.10 Step: 9 Start The Client ............................................................................................................ 19
1.10 Example 3 – Program To Calculate a Number x Raised to A Power y .......................................... 21
1.10.1 Step 1: Create The Remote Interface ....................................................................................... 21
1.10.2 Step 2: Implementation Of The Remote Interface ................................................................... 22
1.10.3 Creating The Server Application ............................................................................................. 22
1.10.4 Creating The Client App .......................................................................................................... 23
Tutorial and Revision Questions ............................................................................................................. 24

1 General Introduction
RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing
in one system (JVM) to access/invoke an object running on another JVM. RMI is used to build
distributed applications; it provides remote communication between Java programs. It is provided
in the package [Link].
In a distributed computing environment, distributed object communication realizes
communication between distributed objects. The main role is to allow objects to access data and
invoke methods on remote objects (objects residing in non-local memory space). Invoking a
method on a remote object is known as remote method invocation (RMI) or remote invocation,
and is the object-oriented programming analog of a Remote Procedure Call (RPC).
As we have seen, Java sockets, combined with serialization makes is it easy to send objects across
the network, however, sockets require the programmer to encode and decode the messages that are
being exchanged. As the application facilities grow, so do the complexities in communication.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 2
RMI is a full architecture for distributed computing. It provides a mechanism for distributing
objects as services, where a remote service request looks similar to a local one. The object is not
passed to and from the client/server, rather it is fixed in the one place on the server. The Virtual
Machine that is responsible for the object declares it exportable and makes it available to an object
server that can call on it when a request is received.
The remote client obtains a reference to the object and calls methods on it. To obtain this reference
to the object, the client must know the name of the object, where it is and also what methods it has.
Once this information is known, RMI takes care of the passing of information back and forth, tasks
such as: object serialization, object transport, exception handling (Exceptions may occur when
network connections are broken, one of the client/server pair may crash etc..) and security
management.
To use a remote object, we first must have a reference to it. An application can register its remote
objects with RMI's simple naming facility, the rmiregistry, or the application can pass and return
remote object references as part of its normal operation The only concise way to do this is to have
lookup information available (such as the method names) to the client, generally prior to run-time.
This is facilitated through the use of a pair of classes, termed skeletons and stubs, that are derived
directly from the remote object.
The client calls the remote object by using this lookup information, which is coded into the client.
If the lookup succeeds then the RMI Server could return the remote object's stub, which acts as a
stand-in for the remote object's methods. A call to any of the stub's methods, is sent from the stub
to the skeleton reference, that resides on the server-side. The skeleton in effect calls the method on
the server-side and routes the remote object's response back through to the stub.
The stub for a remote object acts as a client's local representative or proxy for the remote object.
The caller invokes a method on the local stub which is responsible for carrying out the method call
on the remote object. In RMI, a stub for a remote object implements the same set of remote
interfaces that a remote object implements. When a stub's method is invoked, it:
• initiates a connection with the remote JVM containing the remote object,
• marshals (writes and transmits) the parameters to the remote JVM,
• waits for the result of the method invocation,
• unmarshals (reads) the return value or exception returned, and,
• returns the value to the caller.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 3
In the remote JVM, each remote object may have a corresponding skeleton (in Java 2 platform-
only environments, skeletons are not required). The skeleton is responsible for dispatching the call
to the actual remote object implementation. When a skeleton receives an incoming method
invocation it does the following:
• unmarshals (reads) the parameters for the remote method,
• invokes the method on the actual remote object implementation, and
• marshals (writes and transmits) the result (return value or exception) to the caller.

Figure 1 Skeleton and stub communication


This communication takes place over the RMI remote reference layer (which relies on the TCP/IP
transport layer).
The RMI provides remote communication between the applications using two
objects stub and skeleton.
1.1 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. Let's understand
the stub and skeleton objects:
1.1.1 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. 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. 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

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 4
5. It finally, returns the value to the caller.

1.1.2 Skeleton
The skeleton is an object, acts as a gateway for the server-side object. All the incoming requests
are routed through it. This is the object which resides on the server side. stub communicates with
this skeleton to pass request to the remote object. 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.
In the Java 2 SDK, an stub protocol was introduced that eliminates the need for skeletons.

1.2

Architecture of an RMI Application


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.
The following diagram shows the architecture of an RMI application.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 5
Let us now discuss the components of this architecture.
• 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.

1.3 Working of an RMI Application


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.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 6
1.4 Marshalling and Unmarshalling
Whenever a client invokes a method that accepts parameters on a remote object, the parameters
are bundled into a message before being sent over the network. These parameters may be of
primitive type or objects. In case of primitive type, the parameters are put together and a header
is attached to it. In case the parameters are objects, then they are serialized. This process is known
as marshalling.
At the server side, the packed parameters are unbundled and then the required method is invoked.
This process is known as unmarshalling.

1.5 RMI Registry


RMI registry is a namespace on which all server objects are placed. Each time the server creates
an object, it registers this object with the RMIregistry (using bind() or reBind() methods). These
are registered using a unique name known as bind name. The [Link] class provides
methods for storing and obtaining references to remote objects in the remote object registry. Each
method of the Naming class takes as one of its arguments a name that is a String in URL format.
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).
The following illustration explains the entire process −

1.6 Goals of RMI


Following are the goals of RMI −

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 7
• To minimize the complexity of the application.
• To preserve type safety.
• Distributed garbage collection.
• Minimize the difference between working with local and remote objects.

1.7 Advantages of RMI


At the most basic level, RMI is Java's remote procedure call (RPC) mechanism. RMI has several
advantages over traditional RPC systems because it is part of Java's object oriented approach.
Traditional RPC systems are language-neutral, and therefore are essentially least-common-
denominator systems-they cannot provide functionality that is not available on all possible target
platforms.
RMI is focused on Java, with connectivity to existing systems using native methods. This means
RMI can take a natural, direct, and fully-powered approach to provide you with a distributed
computing technology that lets you add Java functionality throughout your system in an
incremental, yet seamless way.
The primary advantages of RMI are:
• Object Oriented: RMI can pass full objects as arguments and return values, not just
predefined data types. This means that you can pass complex types, such as a standard
Java hashtable object, as a single argument. In existing RPC systems you would have to
have the client decompose such an object into primitive data types, ship those data types,
and the recreate a hashtable on the server. RMI lets you ship objects directly across the
wire with no extra client code.
• Mobile Behavior: RMI can move behavior (class implementations) from client to server
and server to client. For example, you can define an interface for examining employee
expense reports to see whether they conform to current company policy. When an
expense report is created, an object that implements that interface can be fetched by the
client from the server. When the policies change, the server will start returning a different
implementation of that interface that uses the new policies. The constraints will therefore
be checked on the client side-providing faster feedback to the user and less load on the
server-without installing any new software on user's system. This gives you maximal
flexibility, since changing policies requires you to write only one new Java class and
install it once on the server host.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 8
• Design Patterns: Passing objects lets you use the full power of object oriented
technology in distributed computing, such as two- and three-tier systems. When you can
pass behavior, you can use object oriented design patterns in your solutions. All object
oriented design patterns rely upon different behaviors for their power; without passing
complete objects-both implementations and type-the benefits provided by the design
patterns movement are lost.
• Safe and Secure: RMI uses built-in Java security mechanisms that allow your system
to be safe when users downloading implementations. RMI uses the security manager
defined to protect systems from hostile applets to protect your systems and network from
potentially hostile downloaded code. In severe cases, a server can refuse to download
any implementations at all.
• Easy to Write/Easy to Use: RMI makes it simple to write remote Java servers and Java
clients that access those servers. A remote interface is an actual Java interface. A server
has roughly three lines of code to declare itself a server, and otherwise is like any other
Java object. This simplicity makes it easy to write servers for full-scale distributed object
systems quickly, and to rapidly bring up prototypes and early versions of software for
testing and evaluation. And because RMI programs are easy to write they are also easy
to maintain.
• Connects to Existing/Legacy Systems: RMI interacts with existing systems through
Java's native method interface JNI. Using RMI and JNI you can write your client in Java
and use your existing server implementation. When you use RMI/JNI to connect to
existing servers you can rewrite any parts of you server in Java when you choose to, and
get the full benefits of Java in the new code. Similarly, RMI interacts with existing
relational databases using JDBC without modifying existing non-Java source that uses
the databases.
• Write Once, Run Anywhere: RMI is part of Java's "Write Once, Run Anywhere"
approach. Any RMI based system is 100% portable to any Java Virtual Machine *, as is
an RMI/JDBC system. If you use RMI/JNI to interact with an existing system, the code
written using JNI will compile and run with any Java virtual machine.
• Distributed Garbage Collection: RMI uses its distributed garbage collection feature to
collect remote server objects that are no longer referenced by any clients in the network.
Analogous to garbage collection inside a Java Virtual Machine, distributed garbage

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 9
collection lets you define server objects as needed, knowing that they will be removed
when they no longer need to be accessible by clients.
• Parallel Computing: RMI is multi-threaded, allowing your servers to exploit Java
threads for better concurrent processing of client requests.
• The Java Distributed Computing Solution: RMI is part of the core Java platform
starting with JDK?? 1.1, so it exists on every 1.1 Java Virtual Machine. All RMI systems
talk the same public protocol, so all Java systems can talk to each other directly, without
any protocol translation overhead.

1.8 Creating a Java RMI Application


These are the steps to be followed sequentially to implement Interface as defined below as follows:
Step 1: Create the Remote interface
Step 2: Implementation of the remote interface
Step 3: Create the server program.
Step 4: Create the client program.
Step 5: Compile all the java programs
Step 6: Create a stub and skeleton : rmic Implementation class
Step 7: Start the registry service by the rmiregistry tool: rmiregistry
Step 8: Start the Server
Step 9: Start the Client

1.8.0 Example 1 – First RMI Application


1.8.1 Defining the Remote Interface
A remote interface provides the description of all the methods of a particular remote object. The
client communicates with this remote interface. (Recall: An interface is an abstract type that is
used to specify a behavior that classes must implement. They are similar to protocols. Interfaces
are declared using the interface keyword)
To create a remote interface −
• Create an interface that extends the predefined interface Remote which belongs to the
package.
• Declare all the business methods that can be invoked by the client in this interface.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 10
• Since there is a chance of network issues during remote calls, an exception
named RemoteException may occur; throw it.
Following is an example of a remote interface. Here we have defined an interface with the
name MainInterface and it has a method called PrintMessage().
import [Link];
import [Link];

// Creating Remote interface for our application


public interface MainInterface extends Remote {
void PrintMessage() throws RemoteException;
}

1.8.2 Developing the Implementation Class (Remote Object)


We need to implement the remote interface created in the earlier step. (We can write an
implementation class separately or we can directly make the server program implement this
interface.)
To develop an implementation class −
• Implement the interface created in the previous step.
• Provide implementation to all the abstract methods of the remote interface.
Following is an implementation class. Here, we have created a class
named ImplementationClassand implemented the interface MainInterface created in the
previous step and provided body for this method which prints a message.
// Implementing the remote interface
public class ImplementationClass implements MainInterface{

// Implementing the interface method


public void PrintMessage() {
[Link]("My First RMI Application");
}
}

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 11
1.8.3 Developing the Server Program
An RMI server program should implement the remote interface or extend the implementation
class. Here, we should create a remote object and bind it to the RMIregistry. This is done
using rebind() method of [Link] class. rebind() method take two arguments, first
represent the name of the object reference and second argument is reference to instance of
To develop a server program −
• Create a client class from where you want invoke the remote object.
• Create a remote object by instantiating the implementation class
(ImplementationClass) as shown below.
• Export the remote object using the method exportObject() of the class
named UnicastRemoteObject which belongs to the package [Link].
• Get the RMI registry using the getRegistry() method of the LocateRegistry class which
belongs to the package [Link].
• Bind the remote object created to the registry using the bind() method of the class
named Registry. To this method, pass a string representing the bind name and the object
exported, as parameters.
Following is an example of an RMI server program.
import [Link];
import [Link];
import [Link];
import [Link];

public class Server extends ImplementationClass{


public Server() {}
public static void main(String args[]) {
try{
//Instantiating the implementation class
ImplementationClass iclass = new ImplementationClass();

// Exporting the object of implementation class


// (here we are exporting the remote object to the stub)
MainInterface stub = (MainInterface)[Link](iclass, 0);

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 12
//Binding the remote object (stub) in the registry
Registry registry = [Link]();
[Link]("Hello Guys", stub);
[Link]("Server ready");
}catch(Exception e){
[Link]("Server Error "+[Link]());
[Link]();
}
}
}
1.8.4 Developing the Client Program
Write a client program in it, fetch the remote object and invoke the required method using this
object.
To develop a client program −
• Create a client class from where your intended to invoke the remote object.
• Get the RMI registry using the getRegistry() method of the LocateRegistry class which
belongs to the package [Link].
• Fetch the object from the registry using the method lookup() of the class Registry which
belongs to the package [Link].
To this method, you need to pass a string value representing the bind name as a parameter.
This will return you the remote object.
• The lookup() returns an object of type remote, down cast it to the type Hello.
• Finally invoke the required method using the obtained remote object.
Following is an example of an RMI client program.
import [Link];
import [Link];

public class Client {


private Client(){}
public static void main(String args[]) {
try{

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 13
//Getting the registry
Registry registry = [Link](null);

//Looking up the registry for the remote


MainInterface stub = (MainInterface)[Link]("Hello");

//Calling the remote method using the obtained object


[Link]();
[Link]("Remote method invoked");
}catch(Exception e){
[Link]("Client exception: " + [Link]());
[Link]();
}
}

1.9 Example 2 – Calculator RMI Application

1.9.1 Example 2 – Calculator RMI Application


1.9.2 Step 1: Create the Remote interface
First, we will create 4 interfaces for the 4 basic operations (addition, subtraction, multiplication,
division). These interfaces are helpful for the operation. To create remote interfaces we need to
extend the remote interface and the method prototype within the interface should throw
the RemoteException.
[Link]
import [Link].*;
public interface AddInterface extends Remote {
// Declaring the method prototype
public int add(int x, int y) throws RemoteException;
}
[Link]

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 14
// Creating a SubInterface interface
import [Link].*;
public interface SubInterface extends Remote {
// Declaring the method prototype
public int sub(int x, int y) throws RemoteException;
}
[Link]
// Creating a MulInterface interface
import [Link].*;
public interface MulInterface extends Remote {
// Declaring the method prototype
public int mul(int x, int y) throws RemoteException;
}
[Link]
// Creating a DivInterface interface
import [Link].*;
public interface DivInterface extends Remote {
// Declaring the method prototype
public int div(int x, int y) throws RemoteException;
}

1.9.3 Step 2: Implementation Of The Remote Interface


Now it is time to provide implementation to all the interfaces. To implement the
remote interface, the class should extend to the UnicastRemoteObject class
of the [Link] package. Also, a default constructor needs to be created to throw
the [Link] from its parent constructor in the class.
[Link]
// Java program to implement the 4 Interfaces
import [Link].*;
import [Link].*;

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 15
public class Impl extends UnicastRemoteObject
implements AddInterface, SubInterface, MulInterface,
DivInterface {

// Default constructor to throw RemoteException


// from its parent constructor
public Impl() throws Exception { super(); }

// Implementation of the +-*/ Interfaces


public int add(int x, int y) { return x + y; }
public int sub(int x, int y) { return x - y; }
public int mul(int x, int y) { return x * y; }
public int div(int x, int y) { return x / y; }
}

1.9.4 Step 3: Creating the Server application program.


The next step is to create the server application program. The rebind method
of the Naming class is used to bind the remote object to the new name.

[Link]
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) throws Exception
{
// Create an object of the interface implementation class
Impl obj = new Impl();

// Binds the remote object by the name ADD


[Link]("ADD", obj);

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 16
[Link]("Server Started Successfully");
}
}

1.9.5 Step 4: Creating The Client Application Program


The next step is to create the client application program. The lookup method of Naming class is
used to get the reference of the Stub object.
[Link]
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner([Link]);
while (true) {
[Link](

"\[Link]\[Link]\[Link]\[Link]\[Link]");
[Link]("Enter the option:");
int opt = [Link]();
if (opt == 5) {
break;
}
[Link]("Enter the the first number:");
int a = [Link]();
[Link]("Enter the second number:");
int b = [Link]();
int n;
switch (opt) {
case 1:

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 17
// lookup method to find reference of remote object -ADD
AddInterface obj = (AddInterface)[Link]("ADD");
n = [Link](a, b);
[Link]("Addition= " + n);
break;
case 2:
SubInterface obj1 = (SubInterface)[Link]("ADD");
n = [Link](a, b);
[Link]("Subtraction= " + n);
break;
case 3:
MulInterface obj2 = (MulInterface)[Link]("ADD");
n = [Link](a, b);
[Link]("Multiplication = " + n);
break;
case 4:
DivInterface obj3 = (DivInterface)[Link]("ADD");
n = [Link](a, b);
[Link]("Division = " + n);
break;
}
}
}
}

1.9.6 Step 5: Compiling all the java programs


To compile all the java programs, use the command javac *.java and it will generate class files
for all the files.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 18
1.9.7 Step 6: Create a stub and skeleton
The rmic tool is used to invoke the rmi compiler that creates the Stub and
Skeleton objects. Compile the implementation class. rmic Impl

1.9.8 Step: 7 Start The Registry Service Using The rmiregistry Tool
Now start the rmi registry service by using rmiregistry tool. We need to specify
port number. If we don’t specify the port number, it uses a default port number
for example we are using port number 5259. rmiregistry or start rmiregistry

1.9.9 Step: 8 Start The Server


Start the server app, without the server app running, the client app cannot run.
1.9.10 Step: 9 Start The Client
Once the registry has been started and the server running, the client app is ready
to be executed.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 19
IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 20
1.10 Example 3 – Program To Calculate a Number x Raised to A Power y
1.10.1 Step 1: Create The Remote Interface
[Link]
import [Link].*;
public interface Power extends Remote
{
public int power1()throws RemoteException;

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 21
}

1.10.2 Step 2: Implementation Of The Remote Interface


[Link]
import [Link].*;
import [Link].*;
import [Link];
public class PowerRemote extends UnicastRemoteObject implements Power
{
public PowerRemote() throws Exception { super(); }

public int power1(int z)


{
int p;
Scanner sc = new Scanner([Link]);
[Link]("Enter the base number ::");
int x = [Link]();
[Link]("Enter the exponent number ::");
int y = [Link]();
p=y^x;
[Link](x+" ^ "+y+" = "+p);
}
}

1.10.3 Creating The Server Application


[Link]
import [Link].*;
import [Link].*;
public class Server
{
public static void main(String args[])
{

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 22
try
{
Power stub=new PowerRemote();
[Link]("GO",stub);
}
catch(Exception e)
{
[Link](e);
}
}
}

1.10.4 Creating The Client App


[Link]
import [Link].*;
public class Client
{
public static void main(String args[])
{
try
{
Power stub=(Power)[Link]("GO");
[Link](stub.power1());
}
catch(Exception e){//error here}
}
}

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 23
Tutorial and Revision Questions
1. What is RMI ?
2. What is the basic principle of RMI architecture ?
3. What are the layers of RMI Architecture ?
4. What is the role of Remote Interface in RMI ?
5. What is the role of the [Link] Class ?
6. What is meant by binding in RMI ?
7. What is the difference between using bind() and rebind() methods of Naming Class ?
8. What are the steps involved to make work a RMI program ?
9. What is the role of stub in RMI ?
10. What is DGC ? And how does it work ?
11. What is the purpose of using RMISecurityManager in RMI ?
12. Explain Marshalling and demarshalling.
13. Explain Serialization and Deserialization.
14. Using java, develop a client-server application to demonstrate the RMI architecture.

IUGET/2021-2022/BSc/300/DISTRIBUTED_PROGRAMMING/MODULE3 24

You might also like