0% found this document useful (0 votes)
33 views26 pages

RMI Lect

Uploaded by

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

RMI Lect

Uploaded by

Mrunal Pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Lab Assignment: 3B

Implementation of RMI

Mrs. Mrunal Pathak


BE-IT 2021-22
COURSE: Computer Laboratory –IX (C406)
CLASS: B.E. SEMESTER: SEM-VIII

COURSE CODE: C406

FACULTY: MRS.M. K.PATHAK EXAMINATION SCHEME: PR&TW

Sr. Practical List Equipment Required

No.

2B Develop any distributed application through implementing client-server PC, Printer, UPS and Linux
communication programs based on Java RMI techniques. OS, Java

2
Course Objectives
1 To provide fundamental concepts & architecture of distributed systems.
2 To prepare student to implement distributed applications based on TCP/UDP Sockets, IPC and RPC/RMI.

3 To make students describe the concepts of replication and fault tolerance in distributed system as well as catching & replication in web.

4 To make students to describe the concepts of distributed file system & distributed multimedia systems.

5 To provide knowledge of the architecture and design of distributed web based systems.

6. To make students aware about security issues and protection mechanism for distributed environment.

Course Outcomes : Students will be able to


C401.1 Describe principals, desired properties and models of distributed systems.
C401.2 Develop distributed applications based on TCP/UDP Sockets, P2P,RPC/RMI and MPI.
C401.3 Explain concepts of replication and fault tolerance in distributed system as well as catching & replication in web.
C401.4 Apply the basic theoretical concept of Distributed systems to implement Distributed file and Distributed multimedia systems.
C401.5 Explain different models with architecture and application for Distributed Web based system.
C401.6 Identify the security challenges faced by distributed systems and able to select appropriate Solutions.

3
Sr. Practical List Equipment Required CO PO PSO

No.

Develop any distributed application through implementing client-server PC, Printer, UPS and Linux C406.2 PO1, PO2, PSO1, PSO2
2
OS, Java PO3, PO4,
communication programs based on Java RMI techniques. PO5, PO8,
PO9, PO10

4
Java RMI - 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 java.rmi.

5
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.

6
Remote
Reference Layer

Fig1: components of the RMI architecture.

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


8
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.

9
10
Java RMI Application
• To write an RMI Java application, you would have to follow the steps given
below −

1. Define the remote interface


2. Develop the implementation class (remote object)
3. Develop the server program
4. Develop the client program
5. Compile the application
6. Execute the application

11
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.

• 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.

• Since there is a chance of network issues during remote calls, an exception named
RemoteException may occur; throw it.
12
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.

• 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).

13
• Following is an example of a remote interface. Here we have defined an
interface with the name Hello and it has a method called printMsg()

import java.rmi.Remote;
import java.rmi.RemoteException;

// Creating Remote interface for our application


public interface Hello extends Remote {
void printMsg() throws RemoteException;
}

14
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.

15
• Following is an implementation class. Here, we have created a class named ImplExample and
implemented the interface Hello created in the previous step and provided body for this
method which prints a message.

// Implementing the remote interface


public class ImplExample implements Hello
{
// Implementing the interface method
public void printMsg() {
System.out.println("This is an example RMI program");
}
}
16
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.

• To develop a server program −


1. Create a client class from where you want invoke the remote object.
2. Create a remote object by instantiating the implementation class as shown below.
3. Export the remote object using the method exportObject() of the class named UnicastRemoteObject which
belongs to the package java.rmi.server.
4. Get the RMI registry using the getRegistry() method of the LocateRegistry class which belongs to the package
java.rmi.registry.
5. 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.
17
Following is an example of an RMI server program.
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server extends ImplExample {


public Server() {}
public static void main(String args[]) {
try {
System.setproperty("java.rmi.server.hostname", "127.0.0.1");
// Instantiating the implementation class
ImplExample obj = new ImplExample();

// Exporting the object of implementation class


// (here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

18
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();

registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
19
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 −
1. Create a client class from where your intended to invoke the remote object.
2. Get the RMI registry using the getRegistry() method of the LocateRegistry class which
belongs to the package java.rmi.registry.
3. Fetch the object from the registry using the method lookup() of the class Registry which
belongs to the package java.rmi.registry.
4. To this method, you need to pass a string value representing the bind name as a
parameter. This will return you the remote object.
5. The lookup() returns an object of type remote, down cast it to the type Hello.
6. Finally invoke the required method using the obtained remote object.
20
Following is an example of an RMI client program.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {


private Client() {}
public static void main(String[] args) {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);

// Looking up the registry for the remote object


Hello stub = (Hello) registry.lookup("Hello");

21
// Calling the remote method using the obtained object
stub.printMsg();

// System.out.println("Remote method invoked");


} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
22
5.Compiling the Application

• To compile the application −

1. Compile the Remote interface.


2. Compile the implementation class.
3. Compile the server program.
4. Compile the client program.

23
6. Executing the Application

Step 1 − Start the rmi registry using the following command.


start rmiregistry
Step 2 − Run the server class file as shown below.
Java Server
Step 3 − Run the client class file as shown below.
java Client

24
ASSIGNMENT-2

• 2A. Develop distribution application for String Operations ( length,


concatenate, reverse, etc.)using Java RMI
• 2B. Develop distribution application for Arithmetic Operations ( +,-,
*, /) using Java RMI

25
Thank You

You might also like