You are on page 1of 37

CHAPTER FIVE

JAVA REMOTE METHOD INVOCATION


Introduction
2

 Developing Client/Server applications using sockets involves the


design of a protocol that consists of a language agreed upon by
the client and server.

 The design of protocols is hard and error-prone. One issue, for


example, is deadlock.

 In a deadlock, processes never finish executing; these processes


may be holding system resources, preventing other processes
from accessing these resources.
Cont…
3

 Instead of working directly with Sockets, Client/Server


applications can be developed using Java's Remote Method
Invocation.
 Java RMI is a package that can be used to build distributed
systems/applications in java. It allows an object to invoke
methods running on an object on other Java Virtual Machines
(possibly on different hosts).
 The RMI provides remote communication between the
applications using two objects stub and skeleton.
stub and skeleton
4

RMI uses stub and skeleton object for


communication with the remote object.
A remote object is an object whose method can be
invoked from another JVM.
The stub is an object, acts as a gateway for the client
side.
All the outgoing requests are routed through it.
It resides at the client side and represents the remote
object.
Cont.…
5

 When the caller invokes method on the stub object,


it does the following tasks:
It initiates a connection with remote Virtual
Machine (JVM),
It writes and transmits (marshals) the
parameters to the remote Virtual Machine (JVM),
It waits for the result
It reads (unmarshals) the return value or
exception, and
It finally, returns the value to the caller.
Cont....
6

 The skeleton is an object, acts as a gateway for the


server side object. All the incoming requests are
routed through it. When the skeleton receives the
incoming request, it does the following tasks:
 It reads the parameter for the remote method
 It invokes the method on the actual remote
object, and
 It writes and transmits (marshals) the result to
the caller.
Java RMI - Remote Method Invocation
7

 A primary goal of RMI is to allow programmers to develop


distributed Java programs.
 RMI is the Java Distributed Object Model for facilitating
communications among distributed objects.
 RMI is a higher-level API built on top of sockets
 Socket-level programming allows you to pass data through
sockets among computers.
 RMI enables you not only to pass data among objects on
different systems, but also to invoke methods in a remote object.
The Differences between RMI and Traditional
Client/Server Approach
8

 RMI component can act as both a client and a server, depending


on the scenario in question.

 RMI system can pass functionality from a server to a client and


vice versa. A client/server system typically only passes data back
and fourth between server and client.
RMI Architecture Layers
9
Cont…
10

 The RMI architecture typically consists of the following


components.
 RMI Registry
 RMI Server
 Remote Objects and
 RMI Clients.
Cont…
11

 The RMI Registry is a naming service, a kind of a bootstrap


server used to register Remote Objects.
 Clients can query the registry for remote objects by specifying
their associated names.

 The java runtime environment comes with the registry and it can
be started as a service from the command line.

 The RMI server as discussed in the following section can also


start it.
Cont…
12

 The RMI Server uses the Registry to bind names to remote


objects and register them with the registry.

 The server can start up the RMI registry when it starts up.
 Enabling an RMI server application to start the registry and
control access to it gives more control to the developer and also
enables logging of whatever goes on.
Cont…
13

Remote Objects are those objects that can be exported and


made available to clients.
 Clients usually query for a remote object using the RMI registry
directly or using the server thereby getting a reference. Using
this reference, clients then call remote methods.
 Objects that have methods that can be called across virtual
machines are remote objects.
Cont…
14

RMI clients are applications that query the registry for remote
objects and using the reference obtained call remote methods on
them for various services that the remote object provides.
RMI works as follows:
15

(1) A server object is registered with the RMI registry;


(2) A client looks through the RMI registry for the remote object;
(3) Once the remote object is located, its stub is returned in the
client;
(4) The remote object can be used in the same way as a local object.
The communication between the client and the server is handled
through the stub and skeleton.
Cont…
16
Cont…
17

 The RMI system can be thought of as a four-layer model.


 Layer 1: This is the application layer, the actual implementation
of the client and server applications. Here high-level calls are
made to access and export remote objects.

 Layer2: This is the proxy layer, the skeleton and stub layer.
The application deals with this layer directly. All calls to remote
methods and marshalling of parameters and return objects are
done through these proxies.
Cont…
18

 Layer3: This is the remote reference layer. It is responsible for


dealing with the semantics of the remote invocations. This layer
is responsible for handling replicated objects and for performing
implementation specific tasks with remote objects.

 Layer4: This is the transport layer. This layer is responsible for


actually setting up connections and handling the transport of data
from one machine to another.
Cont…
19

 The Transport Layer makes the connection between JVMs. All


connections are stream-based network connections that use
TCP/IP.

 Even if two JVMs are running on the same physical computer,


they connect through their host computer's TCP/IP network
protocol stack.
Cont…
20

 The following diagram shows the unfettered use of TCP/IP


connections between JVMs.

In the current release of RMI, TCP/IP connections are used as the foundation for all
machine-to-machine connections.
Naming Remote Objects
21

Q. "How does a client find an RMI remote service?”


 Clients find remote services by using a naming or directory
service.
 This may seem like circular logic. How can a client locate a
service by using a service? In fact, that is exactly the case. A
naming or directory service is run on a well-known host and port
number.
(Well-known meaning everyone in an organization knowing what it
is).
Cont…
22

 RMI itself includes a simple service called the RMI Registry,


rmiregistry.

 The RMI Registry runs on each machine that hosts remote


service objects and accepts queries for services, by default on
port 1099.
Cont…
23

 On a host machine, a server program creates a remote service by


first creating a local object that implements that service. Next, it
exports that object to RMI.
 When the object is exported, RMI creates a listening service that
waits for clients to connect and request the service.
 After exporting, the server registers the object in the RMI
Registry under a public name.
Cont…
24

 On the client side, the RMI Registry is accessed through the


static class Naming. It provides the method lookup() that a client
uses to query a registry.
 The method lookup() accepts a URL that specifies the server host
name and the name of the desired service. The method returns a
remote reference to the service object. The URL takes the form:
rmi://<host_name>
[:<name_service_port>] /<service_name>
Cont…
25

 where the host_name is a name recognized on the local area


network (LAN) or a DNS name on the Internet.

 The name_service_port only needs to be specified only if the


naming service is running on a different port to the default 1099.
Steps To write RMI application
26

 Define the remote interface


 Develop the implementation class (remote object)
 Develop the server program
 Develop the client program
 Compile the application
 Execute the application
Remote Interface
27

 A remote interface provides the description of all the methods


of a particular remote object. The client communicates with
this remote interface.
To create a remote interface −
 Create an interface that extends the predefined
interface Remote.
 Declare all the business methods that can be
invoked by the client in this interface.
 Since there is a chance of network issues during
remote calls, an exception named
RemoteException may occur; throw it.
Example
28
Implementation Class (Remote Object)
29

We need to implement the remote interface created


in the earlier step.
We can write an implementation class separately or
we can directly make the server program implement
this interface.
To develop an implementation class −
 Implement the interface created in the previous step.
 Provide implementation to all the abstract methods of the
remote interface
Example
30
Developing the Server Program
31

An RMI server program should implement the remote


interface or extend the implementation class.
Here, we should create a remote object and bind it to the
RMIregistry.
To develop a server program −
 Create a client class from where you want invoke the remote object.
 Create a remote object by instantiating the implementation class
 Export the remote object using the method exportObject() of the class named
UnicastRemoteObject which belongs to the package java.rmi.server.
 Get the RMI registry using the getRegistry() method of the LocateRegistry
class which belongs to the package java.rmi.registry.
 Bind the remote object created to the registry using the bind() method of the
class named Registry. To this method, pass a string representing the bind name
and the object exported, as parameters.
Example
32
Developing the Client Program
33

Write a client program in it, fetch the remote object


and invoke the required method using this object.
To develop a client program −
 Create a client class from where your intended to invoke
the remote object.
 Get the RMI registry using the getRegistry() method of the
LocateRegistry class
 Fetch the object from the registry using the method lookup() of
the class Registry in java.rmi.registry package
 Finally invoke the required method using the obtained remote
object.
Example
34
Compiling the Application
35

To compile the application −


 Compile the Remote interface.
 Compile the implementation class.
 Compile the server program.
 Compile the client program.
Or, Open the folder where you have stored all the
programs and compile all the Java files as shown
below.
javac *.java
Executing the Application
36

Step 1 − Start the rmi registry.


start rmiregistry
Step 2 − Run the server class file.
Java Server
Step 3 − Run the client class file
java Client
Verification − As soon you start the client, you
would see some output from Implementation class
37

THANK YOU

You might also like