VI SEM BCA Advanced Java - UNIT 3 - RMI - STUDY MATERIAL
VI SEM BCA Advanced Java - UNIT 3 - RMI - STUDY MATERIAL
STUDY MATERIALS
FOR
VI SEMESTER BCA
1|Page
Program Name : B.C.A. Semester : VI
UNIT III
[ Study Material ]
RMI Distributed Applications
The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java. The RMI allows an object to invoke methods on an
object running in another JVM. The RMI provides remote communication between the
applications using two objects stub and skeleton.
With RMI, a Java programmer can create a publicly accessible remote server object.
This object facilitates seamless client-server communications through simple method
calls on the server object. Client programs can communicate directly with the server
object and with each other using a URL and HTTP.
Working of RMI
The communication between client and server is handled by using two intermediate
objects: Stub object (on client side) and Skeleton object (on server-side) as also can be
depicted from below media as follows:
2|Page
A high-level overview of how client and server communicate through remote objects
using RMI is described below.
3|Page
6. Invoke Remote Methods:
The client invokes methods on the remote object reference obtained from
the RMI registry.
RMI handles the communication details, including parameter organizing,
network communication, and error handling, transparently to the client.
7. Handle Exceptions:
Both the client and server should handle java.rmi.RemoteException and
any other application-specific exceptions that may occur during remote
method invocation.
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
across different JVMs and physical machines.
Object persistence and serialization
Object persistence and serialization are two related concepts in Java that deal with
the storage and retrieval of Java objects in a persistent form, typically to a file or over
a network.
Object Persistence:
Object persistence refers to the ability to store and retrieve objects beyond the
lifetime of the application's execution.
With object persistence, the state of an object can be saved to a persistent
storage medium (such as a database, file system, or cloud storage) and later can
be restored, allowing the application to work with the same data across different
runs or even different instances of the application.
4|Page
Persistence is essential for applications that need to maintain data integrity,
share data between multiple users or instances, or store data for long-term
use.
Serialization:
Serialization is the process of converting an object into a stream of bytes, which
can be easily stored or transmitted and later reconstructed to create an identical
copy of the original object.
In Java, serialization is achieved by implementing the Serializable interface, which
is a marker interface indicating that the class is serializable.
Serializable objects can be written to an output stream (e.g., a file or network
socket) using an ObjectOutputStream, and later read from an input stream using
an ObjectInputStream.
Serialization allows objects to be easily stored to disk, transmitted over a network,
or saved to a database, enabling object persistence.
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.
5|Page
Introduction to Distributed Computing
Distributed computing is the method of making multiple computers work together to
solve a common problem. It makes a computer network appear as a powerful single
computer that provides large-scale resources to deal with complex challenges.
It is a collection of independent components located on different machines that share
messages with each other in order to achieve common goals.
Distributed computing refers to a system where processing and data storage is
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
the central hub.
Components
There are several key components of a Distributed Computing System
6|Page
Resource Management: Distributed systems often have some type of
resource management system in place to allocate and manage shared
resources such 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
There are several characteristics that define a Distributed Computing System
7|Page
Reliability: Distributed systems are often more reliable than centralized
systems, as they can continue to operate even if one device or system fails.
Flexibility: Distributed systems are generally more flexible than centralized
systems, as they can be configured and reconfigured more easily to meet
changing computing needs.
There are a few limitations to Distributed Computing System
8|Page
Distributed Architectures: Many modern computing systems, such as
microservices architectures, use distributed architectures to distribute
processing and data storage across multiple devices or systems.
9|Page
Distributed System Software: This Software enables computers to
coordinate their activities and to share the resources such as Hardware,
Software, Data, etc.
Database: It is used to store the processed data that are processed by each
Node/System of the Distributed systems that are connected to the
Centralized network.
10 | P a g e
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.
To Transfer the Data to Autonomous Systems, Centralized System should be
having a Middleware Service and should be connected to a Network.
Middleware Services enable some services which are not present in the local
systems or centralized system default by acting as an interface between the
Centralized System and the local systems. By using components of
Middleware Services systems communicate and manage data.
The Data which is been transferred through the database will be divided into
segments or modules and shared with Autonomous systems for processing.
The Data will be processed and then will be transferred to the Centralized
system through the network and will be stored in the database.
11 | P a g e
Semantic Transparency:
Syntactic transparency: This implies that there should be a similarity between the
remote process and a local procedure.
Semantic transparency: This implies that there should be similarity in the semantics
i.e. meaning of a remote process and a local procedure.
Working of RPC:
There are 5 elements used in the working of RPC:
Client
Client Stub
RPC Runtime
Server Stub
Server
12 | P a g e
Client: The client process initiates RPC. The client makes a standard call, which triggers
a correlated procedure in the client stub.
Client Stub: Stubs are used by RPC to achieve semantic transparency. The client calls
the client stub. Client stub does the following tasks:
The first task performed by client stub is when it receives a request from a client, it
packs(marshalls) the parameters and required specifications of remote/target
procedure in a message.
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 the Client.
13 | P a g e
RPC Runtime: The RPC runtime is in charge of message transmission between client
and server via the network. Retransmission, acknowledgement, routing, and
encryption are all tasks performed by it. On the client-side, it receives the result values
in a message from the server-side, and then it further sends it to the client stub
whereas, on the server-side, RPC Runtime got the same message from the server stub
when then it forwards to the client machine. It also accepts and forwards client
machine call request messages to the server stub.
The first task performed by server stub is that it unpacks(unmarshalled) the call request
message which is received from the local RPC Runtime and makes a regular call to
invoke the required procedure in the server.
The second task performed by server stub is that when it receives the server’s
procedure execution result, it packs it into a message and asks the local RPC Runtime
to transmit it to the client stub where it is unpacked.
Server: After receiving a call request from the client machine, the server stub passes it
to the server. The execution of the required procedure is made by the server and
finally, it returns the result to the server stub so that it can be passed to the client
machine using the local RPC Runtime.
RPC process:
The client, the client stub, and one instance of RPC Runtime are all running on the
client machine.
A client initiates a client stub process by giving parameters as normal. The client stub
acquires storage in the address space of the client.
14 | P a g e
At this point, the user can access RPC 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 encryption are all tasks
performed by it.
On the server-side, values are returned to the server stub, after the completion of
server operation, which then packs (which is also known as marshaling) the return
values into a message. The transport layer receives a message from the server stub.
The resulting message is transmitted by the transport layer to the client transport
layer, which then sends a message back to the client stub.
The client stub unpacks (which is also known as unmarshalling) the return
arguments in the resulting packet, and the execution process returns to the caller
at this point.
15 | P a g e
When the client process requests by calling a local procedure then the procedure
will pass the arguments/parameters in request format so that they can be sent in a
message to the remote server. The remote server then will execute the local
procedure call ( based on the request arrived from the client machine) and after
execution finally returns a response to the client in the form of a message. Till this
time the client is blocked but as soon as the response comes from the server side it
will be able to find the result from the message. In some cases, RPCs can be executed
asynchronously also in which the client will not be blocked in waiting for the
response.
The parameters can be passed in two ways. The first is to pass by value, whereas
the 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 function is called
using pointers to pass the address of variables. Call by value refers to the method of
sending variables’ actual values.
The language designers are usually the ones who decide which parameter passing
method to utilize. It is sometimes dependent on the data type that is being provided.
Integers and other scalar types are always passed by value in C, whereas arrays are
always passed by reference.
RMI Architecture
The Remote Method Invocation (RMI) architecture in Java facilitates communication
between Java objects residing in different Java Virtual Machines (JVMs) over a
16 | P a g e
network. It allows objects to invoke methods on remote objects as if they were local
objects. The RMI architecture typically involves several components and follows a
client-server model:
1. Remote Interface:
The remote interface is a Java interface that defines the methods that can be
invoked remotely by clients. It extends the java.rmi.Remote interface.
Each method in the remote interface must declare java.rmi.RemoteException in
its throws clause to handle remote method invocation errors.
2. Remote Object Implementation:
The remote object implementation is a Java class that provides the actual
implementation of the methods defined in the remote interface.
This class typically extends java.rmi.server.UnicastRemoteObject or
implements java.rmi.Remote.
It is responsible for executing the methods invoked remotely by clients.
3. RMI Registry:
The RMI registry is a simple naming service provided by Java RMI that allows
clients to look up remote objects by name.
It acts as a central registry where remote objects are bound to names, making
them accessible to clients.
The server creates an RMI registry, and clients use it to locate remote objects.
4. Client:
The client is the application or component that initiates communication with
remote objects.
17 | P a g e
It obtains a reference to the remote object from the RMI registry using the naming
service provided by java.rmi.Naming or java.rmi.registry.LocateRegistry.
Once it has the reference, the client can invoke methods on the remote object as
if it were a local object.
6. Network Communication:
RMI uses TCP/IP as the underlying network protocol for communication between
the client and server JVMs.
It establishes a connection between the client and server JVMs, allowing them to
exchange method invocations and data.
.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
(using bind() or reBind() methods). These are registered using a unique name known
as bind name. It allows remote clients to get a reference to these objects.
18 | P a g e
To invoke a remote object, the client needs a reference of that object. At that time, the
client fetches the object from the registry using its bind name (using lookup() method).
Goals of RMI
Following are the goals of RMI −
19 | P a g e
Minimize the difference between working with local and remote objects.
An RMI callback occurs when the client of one service passes an object ,that is the
proxy, for another service. The recipient can then call methods in the object it
received, and be calling back (hence the name) to where it came from
With RMI, a Java programmer can create a publicly accessible remote server object.
This object facilitates seamless client-server communications through simple method
calls on the server object. Client programs can communicate directly with the server
object and with each other using a URL and HTTP.
20 | P a g e
The second source file, AddServerImpl.java, implements the remote 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.
The third source file, AddServer.java, contains the main program for the server
machine. Its primary function is to update the RMI registry on that machine. This is
done by using the rebind() method of the Naming class (found in java.rmi). That
method associates a name with an object reference. The first argument to the rebind()
method is a string that names the server as "AddServer". Its second argument is a
reference to an instance of AddServerImpl.
21 | P a g e
The fourth source file, AddClient.java, implements the client side of this
distributed application. AddClient.java requires three command-line arguments. The
first is the IP address or name of the server machine. The second and third arguments
are the two numbers that are to be summed.
The application begins by forming a string that follows the URL syntax. This URL
uses the rmi protocol. The string includes the IP 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 AddServerIntf. All remote method invocations can then be directed to
this object.
The program continues by displaying its arguments and then invokes the remote
add() method. The sum is returned from this method and is then printed.
22 | P a g e
Compile all the four source code file using javac.
23 | P a g e
A remote method may accept arguments that are simple types or objects. All of
this information must be sent to the remote machine. That is, an object passed as an
argument to a remote method call must de serialized and sent to the 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 are returned to a client.
To generate a stub, we use a tool called the RMI compiler, which is invoked from
the command line, as shown here:
rmic AddServerImpl
This command generates the file AddServerImpl_Stub.class. When using rmic,
be sure that CLASSPATH is set to include the current directory.
24 | P a g e
Step Five: Start the Server
The server code is started from the command line, as shown here:
java AddServer
Recall that the AddServer code instantiates AddServerImpl and registers that
object with the name "AddServer".
In the first line, the name of the server is provided. The second line uses its IP address
(11.12.13.14)
We can try this example without actually having a remote server. To do so, simply
install all of the programs on the same machine, start rmiregistry, start AddServer,
and then execute AddClient using this command line:
java AddClient 127.0.0.1 8 9
Here, the address 127.0.0.1 is the "loop back" address for the local machine. Using
this address allows you to exercise the entire RMI mechanism without actually having
to install the server on a remote computer.
Sample output from this program is shown here:
The first number is: 8
The second number is: 9
The sum is: 17.0
25 | P a g e