Remote Method Invocation RMI Dr. Syed Imtiyaz Hassan Assistant Professor, Deptt. of CSE, Jamia Hamdard (Deemed to be University), New Delhi, India. s.imtiyaz@jamiahamdard.ac.in 1
Agenda Introduction Creating RMI Applications Components of RMI Executing RMI Summary References 2
Introduction RMI is a Java mechanism similar to RPCs. RMI allows a Java program on one machine to invoke a method on a remote object. 3
Compute Engine 4
RMI System Model Host M/C Client M/C Registry JVM server JVM ClientApp server Remote interface 5
Registry Name Reference Mortgage Tempertur 6
Creating RMI (Distributed) Applications 1. Design and implement the components of your distributed application. 2. Compile sources. 3. Make classes network accessible. 4. Start the application. 7
Components of distributed application Determine local objects and remote objects. Define the remote interfaces Implement The remote objects The server The client 8
RMI components A working RMI system is composed of several parts: Interface definitions for the remote services Implementations of the remote services A server to host the remote services A client program that needs the remote services An RMI Naming service that allows clients to find the remote services 9
RMI Example RMI Addition client/server application Interface (define remote services): Addition.java Implementation of remote services: AdditionImpl.java Server program: Server.java Client program: Client.java 10
Addition.java: Interface import java.rmi.*; public interface Addition extends Remote { public int add(int a, int b) RemoteException; throws } 11
AdditionImpl.java: Implementation import java.rmi.*; import java.rmi.server.unicastremoteobject; public class AdditionImpl extends UnicastRemoteObject implements Addition { public AdditionImpl() throws RemoteException { super(); } } public int add(int a, int b) throws RemoteException { System.out.println("Doing addition"); return a + b; } 12
Class UnicastRemoteObject Used for exporting a remote object with Java Remote Method Protocol (JRMP) and obtaining a stub that communicates to the remote object. Stubs are either generated at runtime using dynamic proxy objects, or they are generated statically at build time, typically using the rmic tool. 13
Class UnicastRemoteObject JRMP is the Java technology-specific protocol for looking up and referencing remote objects. RMI-IIOP is an alternative protocol which exposes Java objects to CORBA ORBs RMI-IIOP (read as "RMI over IIOP") denotes the RMI interface over the Internet Inter-Orb Protocol (IIOP), which delivers Common Object Request Broker Architecture (CORBA) distributed computing capabilities to the Java platform. 14
Server.java: Server program import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.rmi.server.unicastremoteobject; public class Server extends UnicastRemoteObject { public Server() throws RemoteException { super(); } public static void main(string... args) { try { Registry registry = LocateRegistry.createRegistry(1099); registry.rebind("server", new AdditionImpl()); System.out.println("Server Started"); } catch (Exception e) { System.out.println(e); } } } 15
Client.java: Client program import java.rmi.registry.locateregistry; import java.rmi.registry.registry; public class Client { public static void main(string[] args) { Client client = new Client(); try { Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099); Addition addition = (Addition) registry.lookup("server"); System.out.println("Server Started"); System.out.println(addition.add(5, 6)); } catch (Exception e) { System.out.println(e); } } 16
Pre Processing 17
Server Environment 18
Client Environment 19
References https://docs.oracle.com/javase/tutorial/rmi/index.html 20
Thank you 21