Remote Method Invocation NEW
-
Upload
richie-neal -
Category
Documents
-
view
232 -
download
0
Transcript of Remote Method Invocation NEW
-
8/2/2019 Remote Method Invocation NEW
1/21
RMI(Remote Method
Invocation) In RMI, the server defines objects that the client can use
remotely. The clients invoke methods of this remote objectas if it were a local object running in the same machine as
the client. RMI follows the principles of OOPS.
-
8/2/2019 Remote Method Invocation NEW
2/21
Comparison of Distributed and Non distributed JavaPrograms
Local Object Remote Object
ObjectDefinition
Local object is defined by aJava class.
A remote object's exported behavior isdefined by an interface that must extend the
Remote interface.
ObjectImplementation
A local object isimplemented by its Javaclass.
A remote object's behavior is executed by aJava class that implements the remoteinterface.
Object Creation A new instance of a localobject is created by thenew operator.
A new instance of a remote object is createdon the host computer with the new operator.A client cannot directly create a new remoteobject.
Object Access A local object is accesseddirectly via an objectreference variable.
A remote object is accessed via an objectreference variable which points to a proxystub implementation of the remote interface.
References In a single JVM, an objectreference points directly atan object in the heap.
A "remote reference" is a pointer to a proxyobject (a "stub") in the local heap. That stubcontains information that allows it to connectto a remote object, which contains theimplementation of the methods.
-
8/2/2019 Remote Method Invocation NEW
3/21
Comparison of Distributed and Non distributedJava Programs
Local Object Remote Object
ActiveReferences
In a single JVM, an objectis considered "alive" ifthere is at least onereference to it.
A remote object is considered to have an activeremote reference to it if it has been accessedwithin a certain time period (the lease period). Ifall remote references have been explicitlydropped, or if all remote references have expiredleases, then a remote object is available fordistributed garbage collection.
Finalization If an object implementsthe finalize() method, it iscalled before an object isreclaimed by the garbagecollector.
If a remote object implements the Unreferencedinterface, the unreferenced method of thatinterface is called when all remote referenceshave been dropped.
GarbageCollection
When all local references toan object have been
dropped, an objectbecomes a candidate for
garbage collection.
The distributed garbage collector works with thelocal garbage collector. If there are no remote
references and all local references to a remoteobject have been dropped, then it becomes a
candidate for garbage collection through thenormal means.
Exceptions Exceptions are eitherRuntime exceptions orExceptions. The Java
compiler forces a programto handle all Exceptions
RMI forces programs to deal with any possibleRemoteException objects that may be thrown.
-
8/2/2019 Remote Method Invocation NEW
4/21
Java RMI Architecture
Interfaces
RMI Architecture Layers
Application Layer
Stub and Skeleton Layer
Remote Reference Layer
Transport Layer
-
8/2/2019 Remote Method Invocation NEW
5/21
Java RMI Architecture (Interfaces)
The Heart of RMI
The RMI architecture is based on one important principle: thedefinition of behavior and the implementation of thatbehavior are separate concepts.
RMI allows the code that defines the behavior and the codethat implements the behavior to remain separate and to run
on separate JVMs.
In RMI, the definition of a remote service is coded using aninterface.
The implementation of the remote service is coded in a class.
Therefore, the key to understanding RMI is to remember thatinterfaces define behaviorand classes define implementation.
-
8/2/2019 Remote Method Invocation NEW
6/21
Java RMI Architecture (Interfaces)
Java interface does not contain executable code. RMI supports two classes that implement the
same interface. The first class is the implementation of the
behavior, and it runs on the server.
The second class acts as a proxy for the remoteservice and it runs on the client.
A client program makes method calls on theproxy object.
RMI sends the request to the remote JVM, and
forwards it to the implementation. Any return values provided by the implementation
are sent back to the proxy and then to the client'sprogram.
-
8/2/2019 Remote Method Invocation NEW
7/21
Java RMI Architecture (Interfaces)
-
8/2/2019 Remote Method Invocation NEW
8/21
RMI Architecture Layers
-
8/2/2019 Remote Method Invocation NEW
9/21
Application Layer
Actual implementation of the client and the serverprograms.
Calls are made to access and export remote objects
Client can access the remote method through aninterface that extends java.rmi.Remote.
The remote methods are declared in one/more
interfaces that extends java.rmi.Remote
-
8/2/2019 Remote Method Invocation NEW
10/21
Stub and Skeleton Layer
Serializes any arguments toa remote method call andsends this information toserver
Receives any result from
the remote method andreturns it to the client
Receives remote methodcall and any arguments anddeserializes the arguments
Invokes appropriate methodin server using thesearguments
Receives return value fromthis method call and sendsthis information back to theclient.
Stubs and skeletons are created using RMI compiler (RMIC).
Stub (Client side) Skeleton (Server side)
-
8/2/2019 Remote Method Invocation NEW
11/21
Remote Reference Layer
The Remote Reference Layers defines and supports theinvocation semantics of the RMI connection. This layer provides aRemoteRefobject that represents the link to the remote serviceimplementation object.
This layer gets stream oriented data from transport layer andgives it to the proxy layer
The stub objects use the invoke() method in RemoteRef to
forward the method call. The RemoteRefobject understands theinvocation semantics for remote services.
-
8/2/2019 Remote Method Invocation NEW
12/21
Transport Layer
The Transport Layer makes the connection between JVMs. Allconnections are stream-based network connections.
The RMI transport layer is designed to make a connectionbetween clients and server.
Manages communication between JVMs and transfersserialized objects between the remote reference layers ofJVMs.
Informs remote reference layer of methods it should invoke.
-
8/2/2019 Remote Method Invocation NEW
13/21
Naming Class
Clients find the remote objects by using a naming service. It uses the RMI registry that runs on each machine (by
default on port 1099)that will host the remote serviceobjects.
A server registers the object with registry by calling the
bind() or rebind() method of NamingClass.
On the client side, the RMI registry is accessed through thelookup() method of Naming class.
This class resides in the package java.rmi
This class communicates with the registry running on the
server to map the URLs to a particular remote object. Each entry in the registry has a name and an object
reference.
Client gives the name and gets back a reference to aremote object
-
8/2/2019 Remote Method Invocation NEW
14/21
Genesis of RMI
Define a remote interface
Implement the remote interface
Develop the server Develop a client
Generate Stubs and Skeletons.
Start the RMI registry.
Start server.
Start client
-
8/2/2019 Remote Method Invocation NEW
15/21
Steps to create a RMI system
Write and compile java code for interfaces.
Write and compile java code for implementation class(Server).
Generate stub and skeleton class files from implementation
class by using the rmic compiler. Write and compile java code for the client program.
Start the RMI Registry program on the server using thecommand start rmiregistry
Run the RMI System by using the following commands.
>java serverprog>java clientprog
-
8/2/2019 Remote Method Invocation NEW
16/21
Parameters in RMI
RMI supports method calls to remote objects.
When these calls involve passing parameters or accepting areturn value, how does RMI transfer these between JVMs?
What semantics are used?
Does RMI support pass-by-value or pass-by-reference?
The answer depends on whether the parameters areprimitive data types, objects, or remote objects.
-
8/2/2019 Remote Method Invocation NEW
17/21
Parameters in single JVM
The normal semantics for Java technology is pass-by-value. When a parameter is passed to a method, the JVM makes a copy
of the value, places the copy on the stack and then executes themethod.
When the code inside a method uses a parameter, it accesses itsstack and uses the copy of the parameter. Values returned frommethods are also copies.
When aprimitive data type (boolean, byte, short, int, long, char,float, or double) is passed as a parameter to a method, themechanics of pass-by-value are straightforward.
The mechanics of passing an objectas a parameter are morecomplex. An object resides in heap memory and is accessedthrough one or more reference variables.
String s = "Test";
System.out.println(s); It is the reference variable that is passed to the method. In the
example, a copy of reference variable s is made (increasing thereference count to the String object by one) and is placed on thestack. Inside the method, code uses the copy of the reference toaccess the object.
-
8/2/2019 Remote Method Invocation NEW
18/21
Primitive Parameters
When a primitive data type is passed as a parameter to aremote method, the RMI system passes it by value.
RMI will make a copy of a primitive data type and send it tothe remote method.
If a method returns a primitive data type, it is also returnedto the calling JVM by value.
Values are passed between JVMs in a standard, machine-
independent format.
This allows JVMs running on different platforms tocommunicate with each other reliably.
-
8/2/2019 Remote Method Invocation NEW
19/21
Object parameters When an object is passed to a remote method, the semantics change
from the case of the single JVM.
RMI sends the object itself, not its reference, between JVMs.
It is the objectthat is passed by value, not the reference to the
object. Similarly, when a remote method returns an object, a copy ofthe whole object is returned to the calling program.
RMI uses a technology called Object Serialization to transform anobject into a linear format that can then be sent over the network.
Object serialization essentially flattens an object and any objects itreferences.
Serialized objects can be de-serialized in the memory of the remoteJVM and made ready for use by a Java program
-
8/2/2019 Remote Method Invocation NEW
20/21
Remote Object Parameters
RMI introduces a third type of parameter to consider: remoteobjects.
A client program can obtain a reference to a remote objectthrough the RMI Registry program.
There is another way in which a client can obtain a remotereference, it can be returned to the client from a method call.
-
8/2/2019 Remote Method Invocation NEW
21/21
Distributed Garbage Collection
An automatic garbage collector that will reclaim the memory from
any object that has been discarded by the running program.
The interface to the DGC (distributed garbage collector) is hiddenin the stubs and skeletons layer. However, a remote object canimplement the java.rmi.server.Unreferenced interface and get a
notification via the unreferenced method when there are no longerany clients holding a live reference.
In addition to the reference counting mechanism, a live clientreference has a lease with a specified time. If a client does notrefresh the connection to the remote object before the lease term
expires, the reference is considered to be dead and the remoteobject may be garbage collected. The lease time is controlled bythe system property java.rmi.dgc.leaseValue. The value is inmilliseconds and defaults to 10 minutes.