CH 5
CH 5
UNIVERSITY
DEPARTMENT
OF
SOFTWARE
ENGINEERING
ADVANCED PROGRAMMING(SENG6132)
Chapter Five
Remote Method Invocation
OUTLINE
• Overview of RMI
• The RMI Registry
• The Remote Interface
• Implementing RMI
INTRODUCTION
• Remote Method Invocation (RMI) is a Java API that allows an object running in
one Java Virtual Machine (JVM) to invoke methods on an object running in another
JVM, potentially located on a remote server. It is a powerful and flexible mechanism
for creating distributed applications in Java, enabling communication between
different systems over a network.
• RMI enables developers to build distributed systems where objects can interact with
each other as if they were local to the application, even though they are hosted on
different machines.
• Is a powerful and simple way to build distributed applications in Java. It allows Java
objects to interact remotely as though they were local, providing seamless
communication across networks.
RMI
• It is an API that provide a mechanism to create distributed application in
java.
• RMI allow an object to invoke method on an object running in another
JVM.
• RMI simplifies the development of distributed applications by enabling the
interaction between objects running on different machines, within a
network.
• It provide remote communication between the applications using two object
stub and skeleton.
CONT…
• Stub: The client-side representation of the remote object. It acts as a gateway
to the actual object on the server side.
• Skeleton: The server-side representation that receives the remote method
calls and dispatches them to the actual object. Note that in modern versions
of Java, the skeleton is no longer required as RMI now uses dynamic
proxies.
THE RMI REGISTRY
• The RMI registry is a simple server that allows clients to find remote
objects on a server. It stores the references to the remote objects and makes
them available for clients to look up.
• The registry runs on a specific port (by default, port 1099) and is used to
bind objects so that clients can locate and invoke them.
• RMI uses a registry (a simple name-server for Java objects) to store and
retrieve remote objects. The server binds its remote objects to the RMI
registry, and clients use the registry to look up and access those objects.
THE REMOTE INTERFACE
• In RMI the remote interface plays a crucial role in defining the contract
between a client and a server in a distributed Java application. It enables the
client to invoke methods on an object that exists on a remote machine (the
server) as if it were a local object.
• The remote interface in RMI is an interface that declares the methods a
remote object can invoke. These methods can be called by a client, and the
remote object implements the interface to provide the actual functionality.
IMPLEMENTING RMI
• Implementing Remote Method Invocation (RMI) in Java
involves several steps, which can be summarized as defining the
remote interface, creating the remote object, setting up the RMI
registry, and writing the client to access the remote object. Below
is a step-by-step guide to implement an RMI-based application.
STEPS TO DEVELOP RMI SYSTEM
Define the Remote Interface
Implement the Remote Interface
Create the RMI Server
Create the RMI Client
Compile the Java Files
Start the RMI Registry
Run the Server and Client
STEP 1
import java.rmi.Remote;
import java.rmi.RemoteException;
// Remote interface
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
STEP 2
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
// Implementing the remote interface
public class HelloImpl extends UnicastRemoteObject implements Hello {
// Constructor
protected HelloImpl() throws RemoteException {
super();
}// Implement the remote method
public String sayHello() throws RemoteException {
return "Hello, World!";
}}
STEP 3
import java.rmi.*;
import java.rmi.registry.Registry;
public class HelloServer {
public static void main(String[] args) {
try {// Create and export the remote object
HelloImpl obj = new HelloImpl(); // Start the RMI registry
LocateRegistry.createRegistry(1099); // Default port is
1099 // Bind the remote object to the registry
Naming.rebind("rmi://localhost/Hello", obj);
System.out.println("Server ready");
} catch (Exception e) {e.printStackTrace();
}}}
STEP 4
import java.rmi.Naming;
public class HelloClient {
public static void main(String[] args) {
try {// Look up the remote object by its name in the registry
Hello hello = (Hello) Naming.lookup("rmi://localhost/Hello");
// Call the remote method
String response = hello.sayHello();
System.out.println("Response from server: " + response);
} catch (Exception e) {
e.printStackTrace();
}}}
STEP 5 RUNNING THE RMI APPLICATION
Implement interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RmiServerIntf extends Remote{
// an interace that extends java.rmi.Remote;remote interface: the client's view of the
remote object;
public String getMessage ()throws RemoteException;
public int summing(int x, int y)throws RemoteException ;
}
REMOTE OBJECT: RMI SERVER
import java.awt.GridLayout;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.*;
public class RmiServer extends UnicastRemoteObject implements
RmiServerIntf {
//RmiServerIntf is a remote interface;
//RmiServer is a remote object and getMessage is a remote method
public String MESSAGE="YY";
public RmiServer(String msg) throws RemoteException {
super(0);
}
CONT…