16
R. M . I. Mohanraj S AP / IT Angel College of Engg & Tech., www.sunface.in/rdrive

R. M . I

Embed Size (px)

DESCRIPTION

R. M . I. Mohanraj S AP / IT Angel College of Engg & Tech., www.sunface.in/rdrive. Remote Method Invocation. API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM. - PowerPoint PPT Presentation

Citation preview

Page 1: R. M . I

R. M . I.

Mohanraj SAP / IT

Angel College of Engg & Tech.,www.sunface.in/rdrive

Page 2: R. M . I

Remote Method Invocation

• API that provides a mechanism to create distributed application in java.

• The RMI allows an object to invoke methods on an object running in another JVM.

• The RMI provides remote communication between the applications using two objects stub and skeleton

Page 3: R. M . I

Stub• The stub is an object, acts as a gateway for the

client side. All the outgoing requests are routed through it.

• Tasks :– Initiates a connection with remote Virtual

Machine (JVM)– Writes and transmits (marshals) the parameters

to the remote Virtual Machine (JVM)– Waits for the result and reads (unmarshals) the

return value or exception, and– Finally, returns the value to the caller.

Page 4: R. M . I

Skeleton• The skeleton is an object, acts as a gateway

for the server side object. All the incoming requests are routed through it.

• Tasks :– Reads the parameter for the remote method– Invokes the method on the actual remote object,

and– Writes and transmits (marshals) the result to the

caller.

Page 5: R. M . I

Communication b/w stub & skeleton

Page 6: R. M . I

Steps to write the RMI program

• 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

Page 7: R. M . I

Case Study - Example

• The client application need only two files, remote interface and client application.

• In the rmi application, both client and server interacts with the remote interface.

• The client application invokes methods on the proxy object, RMI sends the request to the remote JVM.

• The return value is sent back to the proxy object and then to the client application.

Page 8: R. M . I
Page 9: R. M . I

Step 1 : create the remote interface

Creating a remote interface that extends the Remote interfaceimport java.rmi.*; public interface Adder extends Remote{

public int add(int x,int y)throws RemoteException; }

Page 10: R. M . I

Step 2 : Implementation of the remote interface

import java.rmi.*;

public class AdderRemote extends UnicastRemoteObject implements Adder{

AdderRemote()throws RemoteException{ super(); }

public int add(int x,int y){return x+y;} }

Page 11: R. M . I

Step 3 : create the stub and skeleton objects using the rmic tool

The rmic tool invokes the RMI compiler and creates stub and skeleton objects.

rmic AdderRemote

Page 12: R. M . I

Step 4 : Start the registry service by the rmiregistry tool

• Start the registry service by the rmiregistry tool :

rmiregistry 5000 • If you don't specify the port number, it uses a

default port number.

Page 13: R. M . I

Step 5 : Create and run the server application

• Now rmi services need to be hosted in a server process.• In this example, we are binding the remote object by the name sonoo.

import java.rmi.*; public class MyServer{

public static void main(String args[]){ try{ Adder stub=new AdderRemote(); Naming.rebind("rmi://localhost:5000/sonoo",stub); }catch(Exception e){System.out.println(e);} } }

Page 14: R. M . I

Step 6: Create and run the client application

import java.rmi.*;

public class MyClient{ public static void main(String args[]){ try{ Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo"); System.out.println(stub.add(34,4)); }catch(Exception e){} } }

Page 15: R. M . I

Compiling & Running 1) compile all the java files

javac *.java

2)create stub and skeleton object by rmic tool

rmic AdderRemote

3)start rmi registry in one command prompt

rmiregistry 5000

4)start the server in another command prompt

java MyServer 5)start the client application in another command prompt

java MyClient

Page 16: R. M . I

Screenshot