RMI_4

hedgebornabaloneSoftware and s/w Development

Dec 2, 2013 (3 years and 9 months ago)

93 views

RMI


The Java allows to develop distributed applications using RMI.


Here the programs, the data they process, and the actual computations are
spread

over a network .


RMI (Remote method Invocation) allows object
-
o
-
object communication
between different Java Virtual Machines (JVMs).


JVMs can be distinct entities located on the same or separate computers

yet
one JVM can invoke methods belonging to an object stored on another JVM.


Thus it enables applications to call object methods located remotely, sharing
resources, and processing load across systems.


Methods can even pass objects that a foreign virtual machine has never
encountered before, allowing the dynamic loading of new classes as required.





RMI Architecture

Stub & Skeleton



The JVM that calls the remote object is usually referred to as a client and the JVM that
contains the remote object is the server.


The same object can be both a client and a server.


A remote object is always accessed via its remote interface.


The Stub/Skeleton Layer


This layer intercepts method calls made by the client to the interface reference and
redirects these calls to a

remote object.


Stubs are specific to the client side, whereas skeletons are found on the server
side.


To achieve location transparency, RMI introduces two special kinds of objects known
as stubs and skeletons that serve as an interface between an application and rest of the
RMI system.



This Layer’s purpose is to transfer data to the Remote Reference Layer via marshalling
and unmarshalling.


Marshalling refers to the process of converting the data or object being transferred into
a byte stream and unmarshalling is the reverse


converting the stream into an object
or data.



This conversion is achieved via object serialization.


The Stub/ Skeleton layer of the RMI lies just below the actual application and is based
on the proxy design

pattern.



In the RMI use of the proxy pattern, the stub class plays the role of the proxy for the
remote service implementation.



In the remote VM, each remote object may have a
corresponding skeleton.


Skeleton


On the server side, the skeleton object takes care of all the details of “remoteness.


The skeleton is a helper class that is generated by RMI to help the object
communicate with the stub; it reads the parameters for the method call from the
link, makes the call to the remote service implementation object, accepts the
return value and then writes the return value back to the stub.


Sequence of events performed by the skeleton:


Unmarshals (reads) the parameters for the remote method (remember that these
were marshaled by the stub on the client side).



Invokes the method on the actual remote object implementation.


Marshals (writes and transmits) the result (return value or exception) to the
caller (which is then unmarshalled by the stub)



Stub


The stub is a client
-
side object that represents (or acts as a proxy for) the remote
object.


The stub has the same interface, or list of methods, as the remote objec.



However when the client calls a stub method, the stub forwards the request via the
RMI infrastructure to the remote object (via the skeleton), which actually executes

it.



Sequence of events performed by the stub:



Initiates a connection with the remote VM containing the remote object


Marshals (writes and transmits) the parameters to the remote VM.


Waits for the result of the method invocation.



Unmarshals (reads) the return value or exception returned.


Return the value to the caller


The Remote Reference Layer


The remote reference layer defines and supports the invocation semantics of the
RMI connection.


This layer maintains the session during the method call.



The Transport Layer


The Transport layer makes the stream
-
based network connections over TCP/IP
between the JVMs, and is responsible for setting and managing those connections.


Even if two JVMs are running on the same physical computer, they connect through
their host computers TCP/IP network protocol stack.



RMI uses a protocol called JRMP (Java Remote Method Protocol) on top of TCP/IP
(an analogy is HTTP over TCP/IP).


From the JDK 1.2 the JRMP protocol was modified to eliminate the need for
skeletons and instead use reflection to make connections to the remote services
objects.


Thus we only need to generate stub classes in system implementation compatible
with jdk 1.2 and above.



To generate stubs we use the Version 1.2 option with rmic.


The RMI transport layer is designed to make a connection between clients and
server, even in the face of networking obstacles.


The transport layer in the current RMI implementation is TCP
-
based, but again a
UDPbased transport layer could be substituted in a different implementation.


Other Alternatives to RMI:



Sockets



EJB


CORBA


DCOM


RMI Registry


RMI Registry acts a broker between RMI servers and the clients.



The server "registers" its services in the registry
-

hence a RMI Registry can act as a
"directory" for many servers/services.



The client does not need to know the location of individual servers, and does a
lookup on the RMI Registry for the service it needs.



The registry, being a naming directory returns the appropriate handle to the client to
invoke methods on
.




Java Naming and Directory Interface


JNDI, the Java Naming and Directory Interface, allows applications to access various
naming and directory services via a common interface.


The JNDI architecture consists of the JNDI API and the JNDI SPI. The JNDI API
allows Java


applications to access a variety of naming and directory services. The JNDI SPI is
designed to


be used by arbitrary service providers including directory service providers.


A directory service provides access to diverse kinds of information about users and
resources


It uses a
naming system
for the purpose of identifying and organizing


Directory objects to represent this information, A directory object provides an
association between
attributes and values.


The main function of a naming system is to map names to objects.

Object Serialization


Introduction to Object Serialization


Java object serialization is used
to persist(continue to exist)
Java objects to a file,
database, network, process or any other system.


Serialization keeps objects into an ordered, or serialized stream of bytes.


The ordered stream of bytes can then be read at a later time, or in another
environment, to recreate the original objects.


Java serialization does not cannot occur for transient or static fields.



Marking the field transient prevents the state from being written to the stream and
from being restored during deserialization.



Java provides classes to support writing objects to streams and restoring objects
from streams.


Only objects that support the java.io.
Serializable

interface or the
java.io.
Externalizable

interface can be written to streams.


Only objects of classes that implement java.io.Serializable interface can be serialized
or deserialized.

Internationalization


Internationalization

:
In terms of computer programming, it is a process of
designing and writing an application so that is can be used in a global or multinational
context.


Internationalization program is capable of supporting different languages as well as
date,time,currency and other values without software modifications.


It usually involves soft coding or separate textual file in a program .


Let's see how Internationalization works with a very simple program.


Suppose You've got a Frame with two buttons : "Yes" and "No", and you want this same
program to display "Oui" and "Non" in it's French version (Remember no code
changes are rquired)


Write down the following lines in a plain
-
text file (for the English version):


yesMessage=Yes


noMessage=No


Save your file as :
Messages_en_US.properties
.


Write another plain
-
text file for the French version:


yesMessage=Oui


noMessage=Non

Save this file as :
Messages_fr_FR.properties
.


Notice that
yesMessage

and
noMessage

are the same in the English and
French files, these words are keys.



These keys must not change.


Use this property files in your java code to view the output in your desired
language.







Thanks