L19RMI2 - IT Knowledge Base

Download Report

Transcript L19RMI2 - IT Knowledge Base

Remote Method Invocation
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.
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.
• 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.
• A client always refers to a remote object through one of
Remote interfaces that is implements.
• java.rmi and java.rmi.server packages
contain the interfaces and classes that
define java RMI system.
RMI Architecture
• It consists of 4 layers: Application,
Stub/Skeleton, Remote Reference and
Transport Layer.
Client Application
Server Application
Client Stub
Server Skeleton
Remote Reference
Layer
Remote Reference
Layer
Transport Layer
• 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.
• 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.
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.
Proxy Layer
• It consists of Stub class and Skeleton
class.
• These are generated using RMI compiler.
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.
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 id 10 minutes.
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.
Creating RMI Application
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;
}
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
}
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 registred as.
– Ref is a reference to the object that is being registered.
• rebind(URL,Ref)
– It’ll never throw AlreadyBoundException.
4. Creating Stub & Skeleton
Classes
• Use RMIC compiler which comes along
with JDK.
• rmic <ServerClassName>
• It creates• ServerClassName_Stub.class
• ServerClassName_Skeleton.class
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.
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.
7. Start RMI Registry
• RMI registry application is run as a
background application on default port
1099.
• C:\> start rmiregistry [<port number>]
8. RUN RMI Application
– start rmiregistry
– java serverApp
– java clientApp
• Removing Objects from registry
• Use java.rmi.Naming.unbind(URL)
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