remote method invocation a client server approach

25
Remote Method Invocation A Client Server Approach

Upload: maximillian-marshall

Post on 18-Jan-2018

238 views

Category:

Documents


0 download

DESCRIPTION

RMI for distributed computing RMI is a simple method used for developing and deploying distributed applications in Java environment. RMI enables a programmer to create distributed Java applications, in which methods of remote java objects can be called from other java virtual machines either on same host or on different host scattered across the network.

TRANSCRIPT

Page 1: Remote Method Invocation A Client Server Approach

Remote Method Invocation

A Client Server Approach

Page 2: Remote Method Invocation A Client Server Approach

Distributed Object Technologies It aims at location transparency which

involves followings- Locating & loading remote classes Locating remote objects and providing a

reference to them Enabling remote method calls Notifying programs of network failures and other

problems.

Page 3: Remote Method Invocation A Client Server Approach

RMI for distributed computing RMI is a simple method used for

developing and deploying distributed applications in Java environment.

RMI enables a programmer to create distributed Java applications, in which methods of remote java objects can be called from other java virtual machines either on same host or on different host scattered across the network.

Page 4: Remote Method Invocation A Client Server Approach

Cont…….. A call to remote object using RMI is same as a call to local

object except- An object passed as a parameter to a remote method or

returned from the method must be serializable or be another Remote object.

An object passed as a parameter to a remote method or returned from the remote method called is passed by value and not by reference with the exception when a remote object is passed to a remote method or when a remote object is returned by remote method i.e. remote objects are passed by reference.

A client always refers to a remote object through one of Remote interfaces that it implements.

Page 5: Remote Method Invocation A Client Server Approach

Cont….. java.rmi and java.rmi.server packages

contain the interfaces and classes that define java RMI system.

Page 6: Remote Method Invocation A Client Server Approach

RMI Architecture It consists of 4 layers: Application,

Stub/Skeleton, Remote Reference and Transport Layer.

Page 7: Remote Method Invocation A Client Server Approach

Client Application Server Application

Stub Skeleton

Remote Reference Layer

Remote Reference Layer

Transport Layer

Page 8: Remote Method Invocation A Client Server Approach

RMI Architecture Remote method invocation is made through a

reference to a remote object. When a client makes a call to a remote method,

that client receives a reference to the remote object which implements the remote method.

The remote object is not sent over the network to the client.

A stub which is client side proxy for the remote object is placed which is responsible for data transfer between local (client) system and the remote (server) system.

Page 9: Remote Method Invocation A Client Server Approach

Cont…… Many clients may have references to a single

remote object. Each of these clients will have their own stub

objects representing the remote object but the remote object will not be replicated.

The stub object interacts with server side proxy object called skeleton which is responsible for transferring method calls and data between a stub and the actual object being referenced on server.

Page 10: Remote Method Invocation A Client Server Approach

Application Layer Any application that needs to make some of its methods for remote

clients must first be declared in an interface that extends java.rmi.Remote.

It is a just a flag an object as being remotely accessible. Remote methods must be declared as public. All remote objects must extend the UnicastRemoteObject which

provides functionality for remote accessing. The class UnicastRemoteObject belongs to java.rmi.Server. The Server application must register itself with name server or

registry. The client uses this to make first contact with server application and

obtain a reference to its remote objects.

Page 11: Remote Method Invocation A Client Server Approach

Proxy Layer It consists of Stub class and Skeleton class. These are generated using RMI compiler.

Page 12: Remote Method Invocation A Client Server Approach

Remote Reference Layer It is an abstraction between the stub and

skeleton classes and the actual communication protocols that are handled by the transport layer.

Page 13: Remote Method Invocation A Client Server Approach

Transport layer (TL) It is responsible for actual machine to machine

communication. By default communication take place via TCP/IP. When TL receives a request from client side RRL, it locates

the RMI server for the remote object that is been requested. Then TL establishes a socket connection to the server. After that, TL passes the established connection to client side

RRL and add reference to remote object. The TL monitors the “liveness” of connection. The timeout

period is 10 minutes.

Page 14: Remote Method Invocation A Client Server Approach

RMI Registry Service In any distributed application the client application first locate

the remote object. For this, RMI provides a registry service or name service. The registry keeps track of the addresses of the remote

objects that are being exported by their applications. All objects are assigned unique names that are used to

identify them. Applications can add, remove and access remote objects by

calling methods from java.rmi.registry.Registry interface or from rmi.Naming class.

Page 15: Remote Method Invocation A Client Server Approach

Creating RMI Application

Page 16: Remote Method Invocation A Client Server Approach

1. Define interface for Remote Classes An interface is created that contains all the

methods that the remote object must support. All such interfaces must extend Remote interface. Include throws RemoteException in methods.import java.rmi.*;interface MethodImpl extends Remote{ double getSqrt(double dbl) throws

RemoteException;}

Page 17: Remote Method Invocation A Client Server Approach

2. Implement the interface in Server application The server application extends

UnicastRemoteObject class and implements the remote interface inside the class.

Class RMIServer extends UnicastRemoteObject implements MethodImpl

{ // server side code}

Page 18: Remote Method Invocation A Client Server Approach

3. Binding Objects to a Registry Service For an application to bind or register an object with the

registry both the registry service as well as application must be running on same machine.

An application can bind any object which either extend UnicastRemoteObject or Remote interface.

No two objects can be registered with same name. java.rmi.Naming class has 2 methods bind(URL,Ref)

URL specifies the location of registry along with name that objects will be registered as.

Ref is a reference to the object that is being registered. rebind(URL,Ref)

It’ll never throw AlreadyBoundException.

Page 19: Remote Method Invocation A Client Server Approach

4. Creating Stub & Skeleton Classes Use RMIC compiler which comes along

with JDK. rmic <ServerClassName> It creates- ServerClassName_Stub.class ServerClassName_Skeleton.class

Page 20: Remote Method Invocation A Client Server Approach

5. Create & Compile Client Program Obtaining a reference to a remote object is

accomplished by making a call to the lookup(URL) method of java.rmi.Naming class. On failure it throws a NotBoundException.

The lookup() returns an object of java.rmi.Remote and hence must be cast to the type of object the client application expects.

Page 21: Remote Method Invocation A Client Server Approach

6. Install files on client and server machine On Server machine-

Server_stub.class, Server_skel.class, Interface.class

On client machine- clientApp.class, Interface.class,

Server_stub.class.

Page 22: Remote Method Invocation A Client Server Approach

7. Start RMI Registry RMI registry application is run as a

background application on default port 1099.

C:\> start rmiregistry [<port number>]

Page 23: Remote Method Invocation A Client Server Approach

8. RUN RMI Application start rmiregistry java serverApp java clientApp

Page 24: Remote Method Invocation A Client Server Approach

Removing Objects from registry Use java.rmi.Naming.unbind(URL)

Page 25: Remote Method Invocation A Client Server Approach

Example Create airline.mdb Create a DSN “airline” javac AirlineInterface.java javac AirlineServer.java rmic AirlineServer javac AirlineClient.java On server start rmiregistry On server java AirlineServer On Client java AirlineClient