Java RMI
Java RMI
B.C.A. Semester: VI
UNIT III
I Study Material |
RMI Distributed Applications
The RMt (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in The RMI allows an object to invoke methods on an
java.
Working o!RM!
The cormmunicaion between client and server is handled by using two intermediate
objecis: Stuboiject (on client side) and Skeleton object (on server-side) as also can be
depictei from below media as follows:
Working of RMI
Client Server
onSrver!
Internet
2|Pae
A high-level overview how client and
of server communicate through remote objects
using RMI isdescribed below.
The server creates an RMI registry, which acts as a centra! registry for
remote objects. The registry listens for incoming requests Gn a specific
port.
You can start the RMI registry from the command line using the
rmiregistry toolprovided with the JDK.
4. Bind the Remote Object to the Registry:
The server binds the remote object to the RMl registry using a unique
name.
The client looks up the remote object in the RMIregistry using the naming
service ([Link] [Link].
The client obtains a reference to the remote objeci, vhich it can then use
to invoke remote methods.
3| Pag
6. nvoke Remote Methods:
7. iiandleExceptions:
By following these steps, you can establish communication between a client and a
server using remote objects in Java RMI. This mechanism allows for transparent
communication between distributed Java applications, enabling method invocation
acrossditferent JVMs and physical machines.
Objecr Persistence:
Object persistence refers to the ability to store and retrieve objects beyond the
lifetime of the application's execution.
4|Page
Persistence is essential for applications that need to maintain data integrity,
share data between multiple users or instances, or store data ior long-term
use.
Serialization:
Hence object persistence and serialization provide a powerful mechanism for storing
and retrieving Java objects in a persistent form. They are commonly Used in various
applications, including data storage, caching, messaging systems, and distributed
computing. However, it's essential to consider factors like performance,data integrity,
and security when designing and implementing object persistence and serialization
solutions.
Introducion to Distributed Computing
distributed across multiple devices or systems, rather than being handled by a single
central device. In a distributed system, each device or system has its own processing
capabilities and may also store and manage its own data. These devices or systems
work together to perform tasks and share resources, with no single device serving as
Internet and accessed on demand. In this ty pe of system, users can access and use
Cornponents
GWi processing capabilities and may also store and manage their own data.
Metwork: The network connects the devices or systems in the distributed
6|Page
Resource Management:
Distributed systems often have some type
of
resource management system in place to allocate and manage shared
resourcessuch as computing power,
storage, and networking.
The architecture of a
Distributed Computing System is typically a
Peer-to-Peer
Architecture, where devices or systems can act as both clients and servers and
communicate directly with each other.
Characteristics
. HorizontalScaling: Scaling a
devices or systems in
distributed computing
the netiwork.
Scalability: Distributed
systems are generally
more scalable thancentralized
systems, as they can easily add new devices or systems lo the
network to
increase processing and storagecapacity.
|Pase
ikuliubility: Distributed systems are often more reliable than centralized
systems, as they can continue to operate even if one device or system fails.
Applicaiot:
computing system that is used to share resources such as files and computing
8|Page
Distributed Architectures: Many modern computing systems, such as
Challenges of Distributed
Systems
While distributed systems offer many
advantages,they also present
that must be [Link] somechallenges
challenges include:
Network latency: The communication
network distributed in a
system can
introducelatency, which can
affect the
performance of the system.
Distributed
coordination: Distributed systems require
the nodes, which coordination among
can be challenging due to the distributed nature of the
system.
Example of a Distributed
. System
Any Social Media can
have its
Centralized
Computer Network as its
Headquarters and computer
systems that can be accessed by any user and
using their
services will be the Autonomous
Systems in the Distributed
Architecture. System
9|Pae
Distributed System
corauCI
Working of
Distributed System
LstrtLled
Conpute
Conpsty
|
10 Page
As we can see that each Autonomous System has a common Application that
can have its own data that is shared by the Centralized Database System.
The calling
parameters are sent to the
remote process during a
Procedure Call, and the Remote
caller waits for
a
response irom ihe
[Link] flow of remote
activities during an RPCcall
systems is depicted
between wo networking
in the diagram below.
11 |PaBe
(5..:t
elt
Senakie 'iruspareney:
• Syuiace iransparency:This implies that there should be a similarity between the
remote provess and a local procedure.
Senautic iransparency:This implies that there should be similarity in the
semantics
i.e. mcmy of a remote process and a local procedure.
Worki:y 0 RPC:
There are S clettents used in the working of RPC:
Cint
ClhentStub
RPC Ruie
Server Sub
Server
12 |
Page
Client: The client process
initiates RPC. The client
makes a standardcail, which
a correlated triggers
procedure in the client stub.
The second task performed by the client stub is upon receiving the result
values after
execution,
it unpacks
(unmarshalled)
those results and sends
them to ihe Client.
13 |P as
RPC Runtime: The RPC runtime is in charge of message transmission between client
whereas, onthe server-side, RPC Runtime gotthe same message from theserverstub
when hen i íorwards to the client machine. It also accepts and forwards client
The second iask performed by server stub is that when it receives the server's
procedure cxecuion result, it packs it into a message and asks the local RPC Runtime
to iransmit it to the client stub where it is unpacked.
Servei:After receiving a call requestfrom the client machine, the serverstub passes it
tothe server. The execution of the required procedure is made by the server and
finaliy, iL returns the result to the server stub so that it can be passed to the client
RPC iucess:
. The client, the client stub, and one instance of RPC Runtime are all running on the
client nachine.
14 |
Page
• At this point, the user can accessRPC by using a normal Local Procedural Call. The
RPC runtime is in charge of message transmission between client and server via the
network. Retransmission, acknowledgment, routing, and encrvption are all tasks
performed by it.
Cailre.2cte
3NOCCure
Recuest
15 |
Pagc
SaAS2
When the client process requests by calling a local procedure then the procedure
will pass the arguments/parameters in requestformat so that they can be sent in a
message Lo the remote server. The remote server then will execute the local
pocedure based on the request arrived from the client machine) and after
call (
time the client is blocked but as soon as the response comes from the serverside it
will be able to find the result from the message. Insome cases, RPCs can be executed
asynchronously also in which the client will not be blocked in waiting for the
response.
The paraneLers can be passed in two ways. The first is to pass by value, whereas
tte secOnd is to pass by reference. The parameters receiving the address should be
pointers when we provide it to a function. In Pass by reference, a functionis caled
using pointers to pass the address of variables. Call by value refers to the method of
method Itis sometimes dependent on the data type that is being provided.
to uulize.
Iniegers and other scalar types are always passed by value in C, whereas arrays are
always passed by reference.
RMIArchitecture
Java communication
The Rer)ote Method Invocation (RMI)architecture in facilitates
between Java objects residing in different Java Virtual Machines (JVMs) over a
16 |Pa&e
network. It allows objects to invoke methods on remote objects as if they were local
objects. The RMI architecture typically involves several componcnis ord follows a
client-server model:
[Link]
• Interface:
. The RMI registry is a simple naming service provided by Java RMI that allows
clients to look up remote by name.
. It acts as a central registry
objects
4. Client:
17 |Pae
it oba1b àreference to theremote object from the RMI registry using the naming
.Once it has the reference, the client can invoke methods on the remote object as
method parameters and return values transparently to the client and server.
R2etnod gaiameters and return values are automatically serialized into a stream
of bytes before being transmitted over the network and deserialized back into
G. Neink CoHunication:
.RM uses TCP/!IP as the underlying network protocol for communication between
.RMI registry is a namespace on which all server objects are placed. Each time
the server creates an object, it registers this object with the RMIregistry
(usiny its t) crteBind() methods). These are registered using a unique name known
|
18 Paee
To invoke a remote object, the client needs a referenceof that obijeci. At tlhat time,the
client fetchesthe object from the registry using its bind name (its!n; 'n'u)method).
The following illustration explains the
entire process -
Irvccs rire m2t0s using:
fetched je:
using
chs
the
obje
lookup()
ct
tron
the
-
reis!r
Goals of RMI
19|ase
Miuithze the difference between working with local and remote objects.
An RMI Cal!back occurs when the client of one service passes an object ,that is the
pro:,. iuf htier service. The recipient can then call methods in the object it
Wiuh RMI,a Java programmer can create a publicly accessible remote server object.
calls u:ihe server object. Client programs can communicate directly with the server
[Link]
and
this
returns
a request from a client,
inport [Link].',
20 |
Pa&e
The second source file, [Link],implements
the remole interface.
The implementation of the add() method is direct. All remote objects must extend
UnicastRemoteObject, which provides functionality that is needed to make objects
available from remote machines.
import [Link].;
import [Link].*;
public class
AddServerlmplextends Unicast RemoteObjectimplements AddServerlntf
public AddServerlmpl()
throws RemoteException (}
public double add (doubled1,
doubled2) throws Remote
Exception
d1 + d2;
{return
21 | a
import [Link].*;
import [Link].*;
public class AddServer
tiy(
AddServerlmpl addserverlmpl= new AddServedmpl();
Naming rebind("AddServer", addServerlmpl);
System,[Link]("Server is running...");
catch (Exception e)
([Link]("Exception:"+e);
The fourth source file, [Link], implements the client side of this
Theapplication begins by forming a string that followsthe URL Syntax. This URL
uses therniprotocol. The string includes the lP address or name of the server and the
string "AddServer". The program then invokes the lookup() method of the Naming
class. This method accepts one argument, the rmi URL, and returns a reference to an
object of type AddServerlntf. All remote method invocationscan then be directed to
this object.
The proyram continues by displaying its arguments and then invokes the remote
add()mehod, The sumis returned from this method and is then printed.
|
22 Page
importjava,rmi.";
public class AddClient
try {
catch (Exception e)
object that resides on the client machine. Its function is topresent the same interfaces
astheremote server. Remote method calls initiated by the client are actually directed
to the stub,The stub works with the other parts of the RMI system to formulate a
23 |P
A remote method may accept arguments that are simple types or objects. All of
this informationmust be sent to the remote machine. That is, an object passed as an
argument to aremote method call must de serialized and sent tothe remote machine.
If a response must be returned to the client, the process works in reverse. The
serialization and deserialization facilities are also used if objects arereturned to aclient.
To generate a stub, we use a tool called the RMIcompiler, which is invoked from
the comnand line, as shown here:
rmic AddServerimpl
RMI has techniques for dynamic class loading, but they are not used by the
example at hand. Instead,all of the files that are used by the client and server
variable includes the directory in which your files are located. Then, start the RMI
Registry from the command line, as shown here:
start rmiregistry
When this command returns, a new window has been created. This window must be
open until experimentingwith the RMI done. is
24 | Pate
Step Five: Start the Server
java AddServer
Recall that the AddServer code instantiates AddServerlmpl and registers that
object with the name "AddServer".
machine and the two numbers that are to be summed together. We may invoke it from
the command line by using one of the two formats shown here:
java AddClientserver189
In the first line, the name of theserver is provided. The second line uses ils IP address
([Link])
25 |ae