0% found this document useful (0 votes)
12 views19 pages

Lecture

Remote Method Invocation (RMI) is a Java-based distributed object model that allows objects in different Java Virtual Machines (JVMs) to communicate and invoke methods on each other. The RMI architecture consists of three layers: Stub/Skeleton Layer, Remote Reference Layer, and Transport Layer, with various packages supporting its implementation. The document also includes a simple 'Hello, world!' example demonstrating the creation of a remote interface, object, server, and client.
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)
12 views19 pages

Lecture

Remote Method Invocation (RMI) is a Java-based distributed object model that allows objects in different Java Virtual Machines (JVMs) to communicate and invoke methods on each other. The RMI architecture consists of three layers: Stub/Skeleton Layer, Remote Reference Layer, and Transport Layer, with various packages supporting its implementation. The document also includes a simple 'Hello, world!' example demonstrating the creation of a remote interface, object, server, and client.
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
You are on page 1/ 19

Remote Method Invocation (RMI)

Topics of today!!!
 What is Distributed Application?
 What is RMI?
 RMI System Layers
 RMI Architecture
 Remote Method Invocation API
 RMI Implementation
 RMI Flow
 RMI Example
2
What is Distributed Application?
 A Distributed Application is an application whose
processing is distributed across multiple networked
computers.

 The various models that can be used for developing


distributed applications
 Distributed Component Object Model(DCOM)
 Common Object Request Broker Architecture(CORBA)

 Remote Method Invocation(RMI)

3
What is RMI?
The distributed object model used by Java allows objects
in one JVM to invoke methods of objects in a separate
JVM. This is known as RMI.

Java Virtual Machine Java Virtual Machine

Client Object TCP Server Object

4
RMI System Layers
 The RMI system consists of three layers in its
architecture:

 Stub/Skeleton Layer – objects reside on the client and


server

 Remote Reference Layer – manages the references made


by the client to the remote object

 Transport Layer – connects the client and the server

5
RMI Architecture
Java Virtual Machine Java Virtual Machine

Client Object Server Object ‘S’

Object ‘S’ Stub Object ‘S’ Skeleton

Remote Reference Remote Reference


Layer Layer
TransportLayer TCP TransportLayer

6
Remote Method Invocation API
 The five packages in RMI API are:
 java.rmi
 java.rmi.registry
 java.rmi.server
 java.rmi.activation
 java.rmi.dgc
We shall be examining the first three in detail

7
The java.rmi Package
 The java.rmi package declares
 The Remote interface

 The MarshalledObject class

 The Naming class

 The RMISecurityManager class

 A number of exceptions that are used with remote


method invocations
8
The java.rmi.registry Package
 The interfaces and class of the java.rmi.registry
package are used to handle remote objects by name
and also to register those remote objects

 The java.rmi.registry package provides


 The Registry interface

 The RegistryHandler interface

 The LocateRegistry class.

9
The java.rmi.server Package
 The java.rmi.server package supports both client and
server aspects of RMI like a class implementation of
Remote interface, Client stub and Server skeleton
 Classes and Interfaces declared are :
 RemoteObject class  Skeleton interface

 RemoteServer class  RMIClassLoader class

 UnicastRemoteObject class  RMISocketFactory class

 RemoteStub class
10
RMI Implementation
 Implement RMI server
 Create Remote Interface
 Create a class that implements the remote interface

 Create stub and skeleton classes

 Copy Remote interface and stub to client

 Create and register remote objcet

 Implement the RMI client


 Call the remote interface by using the ‘lookup()’
method of the Naming class.

11
RMI Flow

Client Virtual Machine Server Virtual Machine

Client Remote Object 1

Stub Skeleton Server

1. Server Creates Remote Registry Virtual Machine


2
object.
Remote Object
2. Server Registers Remote Registered
object with RMI registry 12
RMI Flow (cont..)

Client Virtual Machine Server Virtual Machine

Client Remote Object

Stub Skeleton Server

4
3
3.Client requests object from
Registry Virtual Machine
registry

Remote Object 4. Registry returns remote


Registered reference through interface
13
RMI Flow (cont..)

Client Virtual Machine Server Virtual Machine


7
Client Remote Object

5 6 Server
Stub Skeleton

5. Client invokes stub method


Registry Virtual Machine
6. Stub talks to Skeleton
Remote Object
Registered 7. Skeleton invokes remote
object 14
Hello World: Remote Interface
import java.rmi.*;

public interface HelloInterface extends Remote {


/*
* Remotely invocable method,
* returns the message of the remote object,
* such as "Hello, world!"
* throws a RemoteException
* if the remote invocation fails
*/
public String say() throws RemoteException;
}

15
Hello World: Remote Object
import java.rmi.*;
import java.rmi.server.*;
public class Hello extends UnicastRemoteObject
implements HelloInterface {
private String message;
/* Constructor for a remote object
* Throws a RemoteException if exporting the object fails
*/
public Hello (String msg) throws RemoteException {
message = msg;
}
/* Implementation of the remotely invocable method
*/
public String say() throws RemoteException {
return message;
}
}
16
Hello World: Server
import java.io.*;
import java.rmi.*;

public class HelloServer{


/*
* Server program for the "Hello, world!" example.
*/
public static void main (String[] args) {
try {
Naming.rebind ("SHello",
new Hello ("Hello, world!"));
System.out.println ("HelloServer is ready.");
} catch (Exception e) {
System.out.println ("HelloServer failed: " + e);
}
}
}

17
Hello World: Client
import java.io.*;
import java.rmi.*;

public class HelloClient{


/*
* Client program for the "Hello, world!" example
*/
public static void main (String[] args) {
try {
HelloInterface hello = (HelloInterface)
Naming.lookup ("SHello");
System.out.println (hello.say());
} catch (Exception e) {
System.out.println ("HelloClient failed: " + e);
}
}
}

18
References
• https://www.oracle.com/technical-resources/articles/javase/rmi-
corba.html

• https://docs.oracle.com/en/java/javase/13/docs/specs/rmi/objm
odel.html

• https://www.cs.ait.ac.th/~on/O/oreilly/java-ent/dist/appd_01.htm

• https://www.tutorialspoint.com/java_rmi/java_rmi_introduction.
htm

• https://www.iitk.ac.in/esc101/05Aug/tutorial/rmi/overview.html

19

You might also like