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

(PDC) Lecture 10 - Remote Method Invocation (RMI)

Uploaded by

Minahil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

(PDC) Lecture 10 - Remote Method Invocation (RMI)

Uploaded by

Minahil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Topic: Remote Method

Invocation (RMI)

Parallel and Distributed Computing


Remote Method
Invocation
RMI stands for Remote Method Invocation. It's a Java API that allows
objects on one computer to interact with objects on another computer
in a distributed network.
RMI Structure
RMI Remote Interface
A remote interface in RMI is an interface that declares a set of methods that can be invoked
from a remote Java virtual machine. Remote interfaces must extend the interface java.rmi,

• Defines: The methods that the remote object will expose to the clients.
• Extends: java.rmi.Remote interface.
• Methods: Declared with the throws RemoteException to handle remote exceptions.
RMI Server Side
The server-side of Remote Method Invocation (RMI) is the RMI registry. The RMI registry is
a namespace that contains all remote server objects, each with a unique bind name. It allows
remote clients to get a reference to these objects.
• Implements: The remote interface.
• Creates: An instance of the remote object.
• Binds: The object to the RMI registry using java.rmi.registry.Registry to make it
available for clients.
RMI Client Side
Through RMI, an object running in a JVM present on a computer (Client-side) can invoke
methods on an object present in another JVM (Server-side).
• Locates: The remote object using the RMI registry.
• Obtains: The stub (proxy object representing the remote object).
• Invokes: Methods on the stub as if they were local.
How to Run RMI:
Compile all three files separately using java:
• javac MyRemoteInterface.java
• javac MyServer.java
• javac MyClient.java
In a terminal or command prompt, start the RMI registry:
• start rmiregistry (Windows)
• rmiregistry & (Unix/Linux)

In a new terminal or command prompt window, run the server:


• Java MyServer

In a new terminal or command prompt window, run the server:


• Java MyClient
Remote Interface Code:
java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyRemoteInterface extends Remote {


String sayHello() throws RemoteException;
// Add other remote methods here
}
Server-Side Code:
java public static void main(String[] args) {
import java.rmi.RemoteException; try {
import java.rmi.registry.LocateRegistry; MyServer server = new MyServer();
import java.rmi.registry.Registry; MyRemoteInterface stub =
(MyRemoteInterface)
import java.rmi.server.UnicastRemoteObject; UnicastRemoteObject.exportObject(server, 0);
public class MyServer implements Registry registry =
MyRemoteInterface { LocateRegistry.createRegistry(1099);
public MyServer() throws RemoteException { registry.rebind("MyRemoteServer", stub);
}
System.out.println("Server is running...");
@Override
} catch (Exception e) {
public String sayHello() throws
System.err.println("Server exception: " +
RemoteException {
e.toString());
return "Hello from the server!";
e.printStackTrace();
}
}}}
Client-Side Code:
java String response = stub.sayHello();
import java.rmi.registry.LocateRegistry; System.out.println("Response from
server: " + response);
import java.rmi.registry.Registry;
} catch (Exception e) {
public class MyClient {
System.err.println("Client exception: " +
public static void main(String[] args) {
e.toString());
try {
e.printStackTrace();
Registry registry =
}}}
LocateRegistry.getRegistry("server_address",
1099);
MyRemoteInterface stub =
(MyRemoteInterface)
registry.lookup("MyRemoteServer");
The End :)

You might also like