0% found this document useful (0 votes)
12 views

Advanced Programming - CH-05-RPC - RMI

This document discusses Remote Procedure Call (RPC) and Remote Method Invocation (RMI). RMI allows Java objects on different hosts to communicate with each other. A remote object lives on a server and RMI is used to build distributed applications. Key components of an RMI application include stubs, skeletons, and the RMI registry. The remote interface specifies which methods can be invoked by clients. To create a remote object, you implement the remote interface, export the object, and bind it to the RMI registry. Clients look up the remote object in the registry and invoke methods on the stub.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Advanced Programming - CH-05-RPC - RMI

This document discusses Remote Procedure Call (RPC) and Remote Method Invocation (RMI). RMI allows Java objects on different hosts to communicate with each other. A remote object lives on a server and RMI is used to build distributed applications. Key components of an RMI application include stubs, skeletons, and the RMI registry. The remote interface specifies which methods can be invoked by clients. To create a remote object, you implement the remote interface, export the object, and bind it to the RMI registry. Clients look up the remote object in the registry and invoke methods on the stub.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CHAPTER 05: Remote Procedure Call & Remote Method

Invocation

By: Muleta T. E-mail: [email protected]

May 3, 2023

1 / 15
CH05: Topics to be covered

5.1 Overview of RPC & RMI


5.2 Stub and skeleton
5.3 The RMI Registry
5.4 The Remote Interface
5.5 Implementing RMI

2 / 15
CH-05: RPC and RMI

Networking has two fundamental applications.


1 Moving files and data between hosts
2 Allowing one host to run programs on another host.

Telnet
rlogin

what if we want to invoke programs from another machine connected with net ?

• Remote Procedure Call (RPC)
• Remote Method Invocation (RMI)

• are the mechanisms that enable a client to invoke the procedure or method
from the server
What is the Key difference b/n RPC and RMI
RPC 7→ only supports procedural programming
RMI 7→ supports object-oriented programming.

3 / 15
CH-05: RMI and RPC

Remote Method Invocation


Allows Java objects on different hosts communicate with each other.
A remote object lives on a server
RMI is used to build distributed applications
It is provided in the package java.rmi
Each remote object implements a remote interface

That specifies which of its methods can be invoked by clients

Figure: architecture of an RMI application. 4 / 15


CH-05: RMI and RPC

What are the componnets in RMI Application ?


Transport Layer 7→ connects the client and the server.
Stub 7→ 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 7→ This is the object which resides on the server side.
stub communicates with this skeleton to pass request to the remote object.
Remote Reference Layer 7→ It is the layer which manages the references
made by the client to the remote object
How actually RMI operates ?
Step 1: client makes a call to the remote object (i.e. Client -> Stub --> RRL )
Step 2: client-side RRL receives the request, it invokes a method called invoke() of the object remoteRef.
Step-3: RRL on the server side passes the request to the Skeleton which finally invokes the required object on the server.
Step4: The result is passed all the way back to the client.

5 / 15
CH-05: RMI and RPC

Clent Invokes a Method.


Without Parameter on Remote object. 7→ simply invoke the method
With Parameter 7→ the parameters are bundled into a message before sending
Primitive type 7→ parameters are put together and a header is attached to it
Object type 7→ then they are serialized.

Bundling process 7→ MARSHALLING

the server unpack the parameters.


How ?
BY removing the header and deserializing the object

UNMARSHALLING

6 / 15
CH-05: RMI and RPC

Key Component in RMI is RMI Registry



• Provides a centralized directory for servers to register and lookup services.
it is a namespace on which all server objects are placed.
it is container where the clients look up objects.
when server create object, registered in RMIregistry.

using bind() or reBind() methods with bind name
How the client invoke ?
the client fetches the object from the registry using its bind name

using lookup() method.

7 / 15
CH-05: RMI and RPC

8 / 15
CH-05: RMI and RPC

java.rmi packages provides everything to build RMI Application.


A remote interface provides the description of all the methods of a particular
remote object.
The java.rmi.Remote Interface
Any remote interface must satisfy the following requirements:
1 A remote interface must at least extend, either directly or indirectly, the
interface java.rmi.Remote.
2 Each method declaration in a remote interface must satisfy ff :
A remote method declaration must include the exception
java.rmi.RemoteException in addition to any application specific exception
A remote object declared as a parameter or return value must be declared as the
remote interface, not the implementation class of that interface.

9 / 15
CH-05: RMI and RPC

How do we create Remote interface ?


To create a remote interface
import java.rmi.Remote;
import java.rmi.RemoteException;

// Creating Remote interface


public interface InterfaceName extends Remote {
void methodInInterface() throws RemoteException;
void methodInInterface2() throws RemoteException;
void methodInInterface3() throws RemoteException;
. . .
}
Create an Implementation class of the remote interface
// Implementing the remote interface
public class RemoteInterfaceImplmenterClass implements InterfaceName {
// Implementing the interface method
public void methodInInterface() {
// business logic of the interface
}
public void methodInInterface2() {
// business logic of the interface
}
}

10 / 15
CH-05: RMI and RPC

Next step is : Building the RMI server


RMI Server implement the remote interface or extend the implementation class
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.
Export the remote object using the method exportObject() of named
UnicastRemoteObject which belongs to the package java.rmi.server.
Get the RMI registry using the getRegistry() method of theLocateRegistry class
which belongs to the package java.rmi.registry.
Bind the remote object created to the registry using the bind() method of the
class named Registry

11 / 15
CH-05: RMI and RPC

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server extends RemoteInterfaceImplmenterClass {

public static void main(String args []){


try {
// Instantiating the implementation class
RemoteInterfaceImplmenterClass remoteObj = new RemoteInterfaceImplmenterClass();
//Exporting the remote object
InterfaceName stub = (InterfaceName) UnicastRemoteObject.exportObject(remoteObj);
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();

registry.bind("InterfaceName", stub);
}catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}

12 / 15
CH-05: RMI and RPC

Next Step 7→ is Building your Client machine process.


Client fetch the remote object and invoke the required method using this object
To do So:
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 java.rmi.registry
Fetch the object from the registry using the method lookup() of the class
Registry which belongs to the package java.rmi.registry.
The lookup() returns an object of type remote,
Finally invoke the required method using the obtained remote object

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ClientProcesses {


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

13 / 15
CH-05: RMI and RPC

// Looking up the registry for the remote object


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

// Calling the remote method using the obtained object


stub.methodInInterface();
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}

Then Compile * java file (Remote interface, implementation class, server


program, client program)
Executing the RMI Application needs starting the registry
start rmiregistry
java Server
Java Client

14 / 15
Done!
Question!

15 / 15

You might also like