Вы находитесь на странице: 1из 6

1

REMOTE METHOD INVOCATION (RMI)


INTRODUCTION: 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 EMI mechanism is basically an object-oriented RPC mechanism. CORBA is another object oriented RPC mechanism. CORBA differs from java RMI in a number of ways: 1. 2. CORBA is a language independent standard. CORBA includes many other mechanisms in its standard (such as a standard for TP monitors) none of which are part of java RMI. 3. There is also no notion of an Object Request Broker in java RMI.

There are three processes that participate in supporting remote method invocation. 1. on a remote object. 2. The server is the process that owns the remote object. The remote object is an ordinary object in the address space of the server process. 3. The object registry is a name server that relates objects with names. Objects are registered with the object registry. Once an object has been registered, one can use the object registry to obtain access to a remote object using the name of the object. There are two kinds of classes that can be used in java RMI A Remote Class is one whose instances can be used remotely. It referenced in two different ways. a) Within the address space where the object was constructed, the object is an ordinary object which can be used like any other object. b) Within other address spaces, the object can be referenced using an object handle. 1. An instance of remote class will be called a remote object. 2. address space to another. DEFINITION: RMI allows java objects running on the same or separate computers to communicate with one another via remote Method calls. It is based on a similar earlier technology for procedural programming called remote procedure calls (RPCs). RPC allows a procedural program to call a function residing on another computer as if that functions where part of the same program running on same computer. It performs all the networking and marshalling of the data (i.e. packaging of function arguments and return Values for transmission over a network). DISADVANTAGE OF RPC: It supports a limited set of simple data types. A Serializable class is one whose instance can be copied from one The client is the process that is invoking a method

2 It requires programmer to learn a special interface definition language (IDL) to describe the functions that can be invoked remotely. RMI is javas implementation of RPC for java-object-to-java object distributed communication. Once a method of java object is registered as being remotely accessible, a client can Look up that service and receive a reference that allows the client to use that service (call the method). RMI provides for transfer of objects of complex data types via object serialization mechanism. It does not require the programmer to learn an IDL because all the networking code is generated from the existing classes in the program. Creating a Distributed System With RMI To build java-to-java distributed application the four major steps should be followed they are 1. Define a remote interface that describes how the client and server communicate with each other 2. Define the server application that implements the remote interface. 3. Define the client application that users a remote interface reference to interact with the server implementation of the interface. 4. Compile and execute the server and client. REMOTE CLASSES AND INTERFACE: RMI Architecture:
JVM JVM

Client

Server

Stub Remote ref layer Transp ort layer

Skeleton Remote ref layer Transp ort layer

Client

S E R V E R

RMI Package: It consists of various classes and interface for creating and running the distributed applications using RMI. a. The java.rmi package: provides the remote interface, a class for accessing the remote names registered on the server, and a security manager for RMI. - It declares the remote interface and the naming and RMISecurityManager classes. It also defines a number of exception classes that are used with RMI. The java.rmi package consists of the following classes.

3 Naming Class: provide static methods for accessing remote objects through URLs. this class supports the following methods:

1. Rebind(): binds a remote object name to a specified URL and is normally used by the server object 2. Unbind(): removes the binding between an object name and a URL. 3. Lookup(): returns the remote object specified by a URL and is normally used by the client object. 4. List(): returns the list of URLs that are currently known to the RMI registry. RMISecurityManager class: defines the default security policy for remote object stubs. The java.rmi package defines a no of exceptions. The Remote Exception class is the parent of all the exceptions that are generated during RMI.

b. The java.rmi.registry package: - It provides the registry and registry handler interfaces and locates the registry. These interfaces are used to register and access remote objects by a name. - The registry process is started when the start rmiregistry command is executed. The command defines the rebind().unbind(),list() and lookup() methods that are used by the Naming class to associate the names and RMI URLs. c. The java.rmi.server package: - It implements the interfaces and classes that support both the client and the server parts of the RMI. RemoteObject class: implements the remote interface and provides remote implementation of the objects class. All objects that implements remote objects extend the RemoteObject class. RemoteServer Class: extends the Remoteobject class and is a common class that is subclassed by specific types of remote object implementation. UnicastRemoteObject class: Extends are Remoteserver class and provides the default RemoteObject implementation. By extending from the UnicastRemoteObject, the subclass can: Use RMIs default socket based transport for communication. Run all the time. RemoteStub class: provides an abstract implementation of the client side stubs. The static setref() method is used to associate a client-side stub with its corresponding remote object. INTERFACE: Remotecall interface: provide methods that are used by all the stubs and skeleton of RMI. Skeleton interface: provides the methods to access methods if the remote object and this interface is implemented by the remote skeletons. Unreferenced interface: enables to determine when a client no longer references a remote object. This interface is implemented by the remote class.

d. The java.rmi.dgc package: - This package contains classes and interfaces that are used by the distributed garbage collector. Lease class: creates objects that are used to keep track of object reference. Dirty() method used to indicate that a client is references a remote object.

4 Clean() method used to indicate that a remote reference has been completed.

Defining The Remote Interface The first step in creating client /server distributed application with RMI is to define the remote interface. It describes the remote methods which the client will use to interact with the remote server object through RMI. To create remote interface, define an interface that extends interface remote. Interface remote is tagging interface, it does not contain any methods. An object of a class that implements interface remote directly or indirectly is a remote object. Remote object can be accessed from a any JVM that has connection to computer on which remote object executes.

import java.rmi.*; Public interface intf extends remote{ Public void getResult(double f) throws RemoteException;} Define the Server Application that Implements Remote Interface The second step is to define a class that implements an interface that extends remote. The server class should extend class UniCastRemoteObject which provides the basic functionality required for all remote objects. Create remote object (i.e.server object) to register with rmiregistry. The name of the server object will be used by clients to attempt their connection. The name is normally of the form //host:port//remoteobjectname

Where host represents the computer that is running the registry for remote objects port represents the port number where the registry can be found on the host remoteobjectname is the name the client will supply when it attempts to locate the remote object through the registry. Remote objects use host and port to locate the rmiregistry . so they can register themselves as remote services. Client use host and port to locate a service. The default port number to which clients and servers connect for rmiregistry is 1099. Methods rebind of class naming binds the remote object to rmiregistry. There is also a bind method to bind remote object to registry. Method rebind is normally used because if remote objects name was previously registered, the new name will replace that was previously registered.

Syntax: Naming.rebind(serverobjectname,serverobject) Example Program: //server

5 Import java.rmi; Public interface hellointerface extends Remote { Public String say() throws RemoteException; } Import java.rmi.*; Import java.rmi.server.*; Public class hello extends UnicastRemoteobject implements hellointerface { Private String message; Public hello(String msg) throws RemoteException { message=msg; } Public String say() throws RemoteException{ Return message; } Public static void main(string args[]) { Try { Naming.rebind(Hello,new Hello(Hello,world!)); System.out.println(Hello Server is ready); } Catch(Exception e){ System.Out.Println(Hello server failed+e); } Define the Client Defining the client will calls remote class method through RMI. Method lookup of class naming is used by the client to obtain a remote reference to a remote object. This method returns a remote object reference; the returned reference should be cast to appropriate remote interface type. Intf e=(intf)naming.lookup(fahrmi); Eg: //client Import java.io.*; Import java.rmi.*; Public static void main(String arg[]) { Try {

6 hellointergace hello=(hellointerface)Naming.lookup (//Ortles.cos.neu.edu/hello); System.out.println(hello.say()); } Catch(Exception e) { System.out.println(HelloClient exception: +e); } } Compile and Execute Server and Client The remote server class must be compiled using the rmic compiler to produce a stub class An object of the stub class allows the client to invoke the server object remote methods. The stub object receives each remote method call and passes it to the java RMI system which performs networking. Rmic-v1.2 serverclassname (i.e) Rmic v1.2 frmis It allows client to connect to server and interact with remote server object. The next step is to start the rmiregistry to register server with registry Start rmiregistry (or) Rmiregistry To make the remote server object available to receive remote method calls,it must be bound to registry. Java frmis Now execute the client program on local host. Java frmic To access the server computer with IP address 192.168.0.150 Java frmic 192.168.0.150

Вам также может понравиться