Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Advance

OOP

Instructor: Saif Ali


Agenda
• Remote Method Invocation RMI
• Java RMI
• Writing an RMI Server
• Creating an RMI Client Program
Remote Method Invocation (RMI):
Remote Method Invocation (RMI) is an API that
allows an object to invoke a method on an object
that exists in another address space, which could be
on the same machine or on a remote machine.

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). RMI
creates a public remote server object that enables
client and server-side communications through
simple method calls on the server object
Working of RMI:
The communication between client and server is
handled by using two intermediate objects: Stub
object (on client side) and Skeleton object (on
server-side) as also can be depicted from below
media as follows:
To write and RMI program or interface must
follow these steps:
• Create the remote interface.
• Provide the implementation of the remote
interface.
• Compile the implementation class and create the
stub and skeleton objects using the rmic tool.
• Start the registry service by rmiregistry tool.
• Create and start the remote application.
• Create and start the client application.
RMI Server
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject; ??unicast or multicast

public class RMIServer implements RMIServerInterface {

public static void main(String[] args) {


try {
// Create an instance of the server object
RMIServer server = new RMIServer();
jis ko call krna hy
// Export the server object to make it available for remote invocations
RMIServerInterface stub = (RMIServerInterface)
UnicastRemoteObject.exportObject(server, 0);

// Create and start the RMI registry on the default port (1099)
Registry registry = LocateRegistry.createRegistry(1099);
// Bind the server object's stub in the registry
registry.rebind("RMIServer", stub);

System.out.println("RMIServer is ready to accept remote


invocations...");
}
catch (RemoteException e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}

@Override
public String sayHello(String name) throws
RemoteException {
return "Hello, " + name + "!";
}
}
internet is a uni cast or multi cast
and why

The main method sets up the RMI server by performing the following
steps:
• Creating an instance of the RMIServer object.
• Exporting the server object using
UnicastRemoteObject.exportObject() to make it available for
remote invocations.
• Creating the RMI registry using LocateRegistry.createRegistry().
• Binding the server object's stub in the registry using
registry.rebind().

The sayHello() method is an example remote method


implementation defined in the RMIServer class. It simply takes a
name as a parameter and returns a greeting message.
RMI Client Program
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RMIClient {

public static void main(String[] args) {


try {
// Locate the RMI registry
Registry registry = LocateRegistry.getRegistry("localhost", 1099);

// Look up the remote object by its binding name


RMIServerInterface server = (RMIServerInterface)
registry.lookup("RMIServer");

// Invoke the remote method on the server object


String result = server.sayHello("John");

System.out.println("Server response: " + result);


} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}}}
Any Question?

You might also like