18
Advanced Java Programming Topic: Distributed Programming (RMI) By Ravi Kant Sahu Asst. Professor, LPU

Distributed Programming (RMI)

Embed Size (px)

Citation preview

Advanced Java Programming

Topic: Distributed Programming (RMI)

ByRavi Kant Sahu

Asst. Professor, LPU

Remote method INVoCAtIoN(RMI)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Introduction

RMI is a mechanism for communicating (only) between two machines running Java Virtual Machines.

When Java code on machine A needs a service or a method, respectively, of (remote Java object) objB on machine B it starts a remote method invocation.

It does this the same way as invoking a local (Java) object's method.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Remote Method Invocation

Java RMI is a mechanism that allows one to invoke a method on an object that exists in another address space.

The other address space could be on the same machine or a different one.

The RMI mechanism is basically an object-oriented RPC mechanism.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

RMI ARchItectuRe

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Remote Communication RMI allows an object to invoke methods on an object running in

another JVM.

RMI provides remote communication between the applications using two objects:1. Stub2. Skeleton

A remote object is an object whose method can be invoked from another JVM.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Understanding Stub and Skeleton

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Stub Stub is an object which acts as a gateway for the client side. All the outgoing requests are routed through Stub. It represents the remote object at the client side.

Stub performs the following tasks:

1. It initiates a connection with remote JVM.

2. It writes and transmits the parameters to remote JVM (Marshaling).3. It reads the returned value or exception (Unmarshaling).4. Finally, returns the result to the Caller.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Skeleton Skeleton is an object which acts as a gateway for the srver side.

All the outgoing requests are routed through Stub.

Skeleton performs the following tasks:

1. It reads the parameters for the remote method.

2. It reads the parameters from caller (Unmarshaling).3. It invokes the method on the actual remote object. 4. It writes and transmits the result to the caller (Marshaling).

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Writing rMi ApplicAtions

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Writing RMI Programs

Steps to write RMI program:1. Create a remote Interface.2. Provide implementation of the remote interface (Remote class).3. Compile the implementation class and create the stub and skeleton objects using rmic tool (rmic remoteclass).4. Start the registry service by rmiregistry tool (start rmiregistry).5. Create and start the remote application.6. Create and start the client application.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Remote InteRface & class

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Remote Class

A Remote class has two parts: the interface and the class itself.

Remote Interface:The Remote interface must have the following properties:

1. The interface must be public.

2. The interface must extend the interface java.rmi.Remote.3. Every method in the interface must declare that it throws java.rmi.RemoteException. Other exceptions may also be thrown.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Remote Interface

import java.rmi.*;public interface RemoteInterface extends Remote

{double sum(double d1, double d2) throws RemoteException;}

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Remote Class

The Remote class itself has the following properties: 1. It must implement a Remote interface. 2. It should extend the java.rmi.server.UnicastRemoteObject class. 3. It can have methods that are not in its Remote interface. These can only be invoked locally.

Note: Objects of such a class exist in the address space of the server and can be invoked remotely.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Remote Classimport java.rmi.*;import java.rmi.server.*;public class RemoteClass extends UnicastRemoteObject implements

RemoteInterface {public RemoteClass() throws RemoteException

{ }

public double sum(double d1, double d2) throws RemoteException {

return d1 + d2;}

}

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)