remote method invocation by kamalakar dandu

33
Remote Method Invocation RMI

Upload: kamalakar-dandu

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 1/33

Remote Method Invocation

RMI

Page 2: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 2/33

Introduction

Any large and complicated problem can be

sorted out easily if we were to break it up

and divide the work between a number of people. When one person cannot cope with

a huge problem due to time or physical

constraints, the best solution is spilt thework among two or more persons.

Page 3: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 3/33

• In the world of computers, instead of investing millions on massive computers, it

is economically wiser to spilt thecomputations among several smallercomputers.

• Distributed computing is a technologywhich solved by a number of computers andlater integrating them into a solution for theproblem.

• By using the resources of many computerswe can make better use of computing thatworld probably have been underutilized.

Page 4: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 4/33

• Distributed object systems apply theprinciples of object oriented programming

to distributed computing such that objectson remote systems can be accessed with thesame ease as those on local systems.

• The advantages of distributed objectsystems are scalability – easier to build n-tier systems using this technology,minimizing bandwidth requirements and

making it easier for the programmer byhandling or hiding complexities acrosssystems.

Page 5: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 5/33

Java Distributed Object Model

• The various models for developing distributed objectapplications that exist in the industry today are :

 –  Distributed Component Object Model(DCOM): This isMicrosoft‟s approach to developing distributed

applications. –  Common Object Request Broker

Architecture(CORBA): This provides another approachto developing distributed applications.

 – Visigenic‟s VisiBroker for Java: This is Visigenicapproach to distributed object systems.

 –  Remote Method Invocation(RMI): This is SunMicrosystems approach to developing distributedapplications.

Page 6: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 6/33

• All these methods of developing distributed

applications allow objects on one host toinvoke methods of objects on other

computers or even computers on a different

network.• DCOM and CORBA are standards of 

developing distributed applications and can

be developed in any language, whereas RMIis specific to developing distributed

applications in Java.

Page 7: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 7/33

RMI

Due to the advancement in science and technology,distances are no longer an issue. Physical boundaries nolonger act as barriers in the world of telecommunication.People are able to communicate with each other with great

ease. One computer can “talk” to another computer, whichis remotely located, or to a computer in the same room,which the same ease. RMI is a technology introduced byJavaSoft that allows objects in one JVM to invoke methodsof objects in a separate JVM. These separate JVMs may

execute as a different process in the same computer or on aremote computer. Java employs the policy of “Write Once,Run Anywhere”. Whith RMI the java model is extended to“Run Everywhere”. 

Page 8: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 8/33

• Distributed object systems require that objectsrunning in different address spaces(potentially on

different hosts) be able to communicate with eachother. This can be done through socketcommunication, which is, however suitable onlywhen we want to exchange files or small amountof data. When we have to exchange formatted datafor example, information about a book, thismechanism becomes very tricky, since we have todo some stream parsing on the server side andsome string parsing on the client side. RMI makes

this very easy because we can directly have amethod that returns a book object. The RMIsystem takes care of sending the object from theserver to the client.

Page 9: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 9/33

The essence of RMI

Database

Client

Applet

Database

Server

ApplicationData Store

rmi

Page 10: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 10/33

RMI Architectute

• In the RMI architecture the code that defines the

behavior and the code that implements the

behavior remain separate and run on separateJVMs.

• In RMI, the definition of a remote service is coded

using a java Interface. The implementation of the

remote service is coded in a class. Therefore, theinterfaces define behavior and classes define

implementation.

Page 11: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 11/33

The RMI system consists of three layers in its

architecture

• The stub/skeleton layer: stubs are local copy

or proxy for the remote object. Skeleton is the

remote proxy for the remote object.• The remote reference layer: supports

communication between stub and skeleton.

• The transport layer: sets up and managesconnections between different JVMs.

Page 12: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 12/33

The client objects invoking the remote object using

stub, skeleton, and remote reference layer and

transport layer are shown diagrammatically

Application Application

Client Server

Stub Layer Skeleton Layer

RemoteReference Layer

RemoteReference Layer

Transport Layer Transport Layer

Page 13: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 13/33

Stub and Skeleton Layer

Before a client can invoke a method on aremote object, it must acquire a reference to

the remote objects. Once it has the referenceit can invoke any method that the remoteobject supports. The invocation from theclient actually results in an invocation on

the stub; the stub forwards the request to theremote reference layer, which in turnforward the request to the transport layer.

Page 14: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 14/33

The transport layer then contacts the

skeleton through the remote reference layer

on the server. The skeleton then contacts the

remote registry to see if the object, which is

called, is registered. If it is registered then

the object method is called and the resultpassed back to the stub using the remote

reference layer and the transport layer

finally to the stub and then to the clientobject.

Page 15: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 15/33

There are some concepts that are commonto all the distributed systems. Every

distributed system will have stubs andskeletons. We have to put a stub on theclient machine that acts like a proxy for theserver object when we compile the client.

This leads the compiler into believing thatall method calls are being made locally.Similarly when compile the server code we

put a skeleton on the server and lead thecompiler into believing that the methodcalls are originating from the same machine.

Page 16: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 16/33

Remote Reference Layer

• RMI supports only unicast objects.• The term unicast stands for communication

between a single sender and a single receiver overa network. The remote reference layer is

responsible for unicast point-to-point methodinvocation.

• The remote reference layer may deactivate anobject and store the data of the object in a file or a

database so that it can be reactivated at some laterpoint of time.if the server is down, the remotereference layer may try to look for somealternatives to fulfill the request.

Page 17: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 17/33

Transport Layer

• RMI currently uses TCP/IP to communicateclient transport layer and server transport

layer. The transport layer uses TCP socketsby default to communicate between theclient and server transport layer. Thetransport layer has been designed in such a

way that we can implement protocols suchas TCP and UDP. The transport layer takescare of multiple connections from the client.

Page 18: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 18/33

Remote Registry

• If a server object has to be accessed remotely, it

has to register itself with the remote registry. The

remote registry maintains a database of all theobjects that can be called remotely and the name

with which they can be called remotely.the remote

registry is always stored on the server.

• The rmiregistry program listens on port 1099 forincoming request.

Page 19: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 19/33

Packages of RMI

• The RMI API has mainly five packages,

which are

 –  java.rmi

 –  java.rmi.registry

 –  java.rmi.server

 –  java.rmi.activation –  java.rmi.dgc

Page 20: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 20/33

The java.rmi package

This package declares

 – The Remote interface

 – The MarshalledObject class

 – The Naming class

 – The RMISecurityManager class

 – Number of Exceptions classes

Page 21: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 21/33

• The Remote Interface must be implemented

by all remote object. This interface does not

have any methods, it is used only to indicate

that the class which implements this

interface can be accessed remotely.

• The Naming class provides following static

functions for accessing objects using RMI

 – Bind()

 – Rebind()

 – Lookup()

Page 22: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 22/33

The java.rmi.registry package

• This package provides

 –  Registry interface

 –  RegistryHandler interface

 –  LocateRegistry class

These interfaces and class are used to handle

remote objects by name and also to registerthose remote objects.

Page 23: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 23/33

The java.rmi.server Package

• This package implements several interfaces andclasses that support both client and server aspectsof RMI like a class implementation of Remoteinterface, client stub, server skeleton and so on.

• This package provides –  RemoteObject class

 –  RemoteServer class

 –  UnicastRemoteObject

 –  RemoteStub

 –  RmiClassLoader

 –  RmiSocketFactory

 –  Skeleton interface

Page 24: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 24/33

The java.rmi.activation Package

• This package provides support for RMIObject Activation. It provides a number of 

classes and interfaces for this purpose.• Some of them are:

 – Activatable class

 – ActivationGroup class

 – ActivationGroupDesc class

 – ActivationID class

Page 25: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 25/33

The java.rmi.dgc package

• This package provides classes and interface for

RMI distributed garbage-collection(dgc). It has

one interface and two classes which are: –  DGC interface: is used for the server side of the

distributed garbage collection algorithm.

 –  A Lease class object: contains a unique VM identifier

and a lease duration. –  A VMID class object: is an identifier that is unique

across all Java Virtual machines.

Page 26: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 26/33

Implementing the RMI Server

• The steps involved in creating andregistering a remote object are as follows:

 – Create the remote interface – Create a class that implements the Remote

interface

 – Create the stub and Skeleton classes

 – Copy the Remote interface and stub file to theclient.

 – Create and register the Remote object.

Page 27: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 27/33

Create the Remote interface

import java.rmi.*;

public interface myrem extends Remote

{

public String display(String nm)throws

RemoteException;

}

Page 28: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 28/33

Create a class that implements the

Remote interfaceimport hava.rmi.*;

import java.rmi.server.*;

public class remclass extends UniCastRemoteObject implements myrem

{public remclas()throws RemoteException

{

super();

}

public String display(String nm) throws RemoteException{

return “welcome”+nm+” to rmi programming”; 

}

}

Page 29: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 29/33

Create the Stub and Skeleton Classes

• The above class now has to be registered with theremote registry. To achieve this we use a toolprovided by Java, which generates the Stub andSkeleton classes. The tool, which we use, is „rmic‟

compiler.• The usage of this is shown here

rmic <classname>

• Run the compiler in the directory, which has the

„remclass‟. The rmic compiler generates followingtwo files

 –  remclass_stub.class

 –  remclass_skel.class

Page 30: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 30/33

Copy the Remote interface and the stub

file to the client• The remote interface and the stub file need

to be copied to a directory, which is in the

classpath on the client machine. This isrequired to compile and run the client class.

• In out example, we need to copy the

„myrem.class‟ and „remclass_stub.class‟ tothe client machine.

Page 31: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 31/33

Create Server side program

import java.rmi.*;

import java.net.*;

public class rmiserver

{

public static void main(String args[])

{

try

{

remclass rc = new rmiclass();

 Naming.rebind(“rcobj”,rc); 

System.out.println(“remote method is bound”); 

}

catch(RemoteException e)

{}

catch(MalFormedURLException e)

{

}

}}

Page 32: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 32/33

Create and register the Remote object

• Once the skeleton and stub are generated

and the files copied to the client machine,

then start the RMI registry by executing thefollowing command.

Start rmiregistry

• After starting the rmiregistry, we executethe rmiserver.class like any other java

program.

Page 33: Remote Method Invocation by Kamalakar Dandu

8/2/2019 Remote Method Invocation by Kamalakar Dandu

http://slidepdf.com/reader/full/remote-method-invocation-by-kamalakar-dandu 33/33

Create a client program

import java.rmi.*;

import java.net.*;

public class client{

public static void main(String args[])

{

try

{myrem rm;

rm = (myrem)Naming.lookup(“rmi://localhost/remobj”); 

System.out.println(“value getting from remote method….”); 

System.out.println(rm.display(“satyam”)); 

}

catch(Exception e)

{

}

}

}