Java Programming Chapter 6.2 RMI Introduction
Java Programming Chapter 6.2 RMI Introduction
RMI
RMI stands for “Remote Method Invocation” means communicating the object across the
network.
RMI is a one type of structure or system that allows an object running in one Java virtual
machine (Client) to invoke methods on an object running in another Java virtual machine
(Server).This object is called a Remote Object and such a system is also called RMI Distributed
Application.
RMI provides for remote communication between programs written in the JAVA.
1|P ag e
(2) RMI Client
RMI Architecture:
The RMI Architecture (System) has a FOUR layer,
2|P ag e
(1) Application Layer:
It’s a responsible for the actual logic (implementation) of the client and server applications.
Generally at the server side class contain implementation logic and also apply the reference to
the appropriate object as per the requirement of the logic in application.
A Stub class is a client side proxy handles the remote objects which are getting from the
reference.
A Skeleton class is a server side proxy that set the reference to the objects which communicates
with the Stub.
It’s a responsible for manage the references made by the client to the remote object on the server
so it is available on both JVM (Client and Server).
The Client side RRL receives the request for methods from the Stub that is transferred into byte
stream process called serialization (Marshaling) and then these data are send to the Server side
RRL.
The Server side RRL doing reverse process and convert the binary data into object. This process
called deserialization or unmarshaling and then sent to the Skeleton class.
It’s a responsible for the managing the existing connection and also setting up new connections.
So it is a work like a link between the RRL on the Client side and the RRL on the Server side.
RMI Components:
The RMI application contains the THREE components
3|P ag e
(3) RMI Registry
RMI Server contains objects whose methods are to be called remotely. It creates remote objects
and applies the reference to these objects in the Registry, after that the Registry registers these
objects who are going to be called by client remotely.
The RMI Client gets the reference of one or more remote objects from Registry with the help of
object name. Now, it can invoke the methods on the remote object to access the services of the
objects as per the requirement of logic in RMI application.
Once the client gets the reference of remote object, the methods in the remote object are invoked
just like as the methods of a local object.
4|P ag e
(3) RMI Registry:
In the Server side the reference of the object (which is invoked remotely) is applied and after that
this reference is set in the RMI registry.
When the Client call the method on this object, it’s not directly call but it call by the reference
which is already set in the Registry so first get the object from this reference which is available at
RMI Registry then after calls the methods as per the requirement of logic in RMI application.
RMI Registry:
The RMI Registry is a naming service.
RMI server programs use this service to bind the remote java object with the names.
Clients executing on local or remote machines retrieve the remote objects by their name
registered with the RMI registry and then execute methods on the objects.
RMI creates a remote proxy for that object and sent it to clients.
start rmiregistry
By default the port 1099 is used by RMI registry to look up the remote objects. After the RMI
registry starts objects can bind to it.
2. Now in second step to bind the remote object with the RMI registry, execute the server
program.
5|P ag e
From the figure we can see that server calls the RMI registry to map a name with a remote object
represented by black circle.
By using the name of remote objects in the server's registry client program locate the remote
object and then methods are called on the remote object by the client program.
6|P ag e
Third program is for server side.
At the last we will give steps to run this program in one system.
Calculator.java
import java.rmi.Remote;
import java.rmi.RemoteException;
CalculatorImpl.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
CalculatorServer.java
import java.rmi.Naming;
7|P ag e
}
CalculatorClient.java
import java.rmi.Naming;
First of all put these four programs inside bin folder of JDK.
cd\
d:
cd Java\jdk1.6.0_23\bin
javac Calculator.java
javac CalculatorImpl.java
javac CalculatorServer.java
javac CalculatorClient.java
rmic CalculatorImpl
rmiregistry 1099
java CalculatorServer
8|P ag e
open another cmd and again go to same path d:\Java\jdk1.6.0_23\bin
java CalculatorClient
Output:
9|P ag e
RMI example - code in java - application:
Steps for Developing the RMI Application:
This is an interface in which we are declaring the methods as per our logic and further these
methods will be called using RMI.
Here we create a simple calculator application by RMI so in that we need four methods such as
addition, subtraction, multiplication and division as per logic.
so create an interface name Calculator.java and declare these methods without body as per the
requirement of a simple calculator RMI application.
10 | P a g e
Calculator.java:
import java.rmi.Remote;
import java.rmi.RemoteException;
Note:
We must extend the Remote interface because this interface will be called remotely in between
the client and server.
Note:
The RemoteException is an exception that can occur when a failure occur in the RMI process.
(2) Define the class and implement the remote interface(methods) in this class:
CalculatorImpl.java:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
11 | P a g e
return a*b;
}
public long division(long a, long b) throws RemoteException
{
return a/b;
}
public long addition(long a, long b) throws RemoteException
{
return a+b;
}
}
Note:
The UnicastRemoteObject is a base class for most user-defined remote objects. The general form
of this class is,
It supplies the TCP based point-to-point references so this class provides some necessary
services that we need in our application otherwise have to implement of interface cannot do
ourselves as well as can’t access these methods remotely.
The server must bind its name to the registry by passing the reference link with remote object
name.
For that here we are going to use rebind method which has two arguments:
The first parameter is a URL to a registry that includes the name of the application and The
second parameter is an object name that is access remotely in between the client and server.
This rebind method is a method of the Naming class which is available in the java.rmi.* package.
The server name is specified in URL as a application name and here the name is
CalculatorService in our application.
Note:
rmi://localhost:port/application_name
Here, 1099 is the default RMI port and 127.0.0.1 is a localhost-ip address.
CalculatorServer.java:
import java.rmi.Naming;
12 | P a g e
public class CalculatorServer
{
CalculatorServer()
{
try
{
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService",
c);
}
catch (Exception e)
{
System.out.println(“Exception is : ”+e);
}
}
public static void main(String[] args)
{
new CalculatorServer();
}
}
To access an object remotely by client side that is already bind at a server side by one reference
URL we use the lookup method which has one argument that is a same reference URL as already
applied at server side class.
This lookup method is a method of the Naming class which is available in the java.rmi.*
package.
The name specified in the URL must be exactly match the name that the server has bound to the
registry in server side class and here the name is CalculatorService.
After getting an object we can call all the methods which already declared in the interface
Calculator or already defined in the class CalculatorImpl by this remote object.
CalculatorClient.java:
import java.rmi.Naming;
13 | P a g e
System.out.println("Multiplication
:"+c.multiplication(10,5));
System.out.println("Division : "+c. division(10,5));
}
catch (Exception e)
{
System.out.println(“Exception is : ”+e);
}
}
}
javac Calculator.java
javac CalculatorImpl.java
javac CalculatorClient.java
javac CalculatorServer.java
After compiled, in the folder we can see the four class files such as
Calculator.class
CalculatorImpl.class
CalculatorClient.class
CalculatorServer.class
Syntax:
rmic class_name
Here the class_name is a java file in which the all methods are defined so in this application the
class name is CalculatorImpl.java file.
Example:
rmic CalculatorImpl
The references of the objects are registered into the RMI Registry So now you need to start the
RMI registry for that use the command
start rmiregistry
14 | P a g e
So the system will open rmiregistry.exe (like a blank command prompt)
Java CalculatorServer
Now open a new command prompt for the client because current command prompt working as a
server and finally run the RMI client class.
Java CalculatorClient
Addition : 15
Subtraction : 5
Multiplication : 50
Division : 2
NOTE:
For compile or run all the file from command prompt and also use the different commands like
javac, java, start, rmic etc you need to set the class path or copy all the java files in bin folder of
JDK.
15 | P a g e