RMI – Example

Home/RMI/RMI – Example
RMI – Example 2017-06-13T11:03:52+00:00

RMI example – code in java – application:

Steps for Developing the RMI Application:
(1) Define the remote interface
(2) Define the class and implement the remote interface(methods) in this class
(3) Define the Server side class
(4) Define the Client side class
(5) Compile the all four source(java) files
(6) Generate the Stub/Skeleton class by command
(7) Start the RMI remote Registry
(8) Run the Server side class
(9) Run the Client side class(at another JVM)
(1) Define the remote interface:
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.

Calculator.java:


 


Note:
We must extends 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:
The next step is to implement the interface so define a class(CalculatorImpl.java) and implements the interface(Calculator.java) so now in the class we must define the body of those methods(addition, subtraction, multiplication, division) as per the logic requirement in  the RMI application(Simple Calculator).
This class run on the remote server.
CalculatorImpl.java:

 


Note:
The UnicastRemoteObject is a base class for most user-defined remote objects. The general form of this class is,
Public class UnicastRemoteObject extends RemoteServer
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.
(3) Define the Server side class:
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:
The general form of the URL:
rmi://localhost:port/application_name
Here, 1099 is the default RMI port and 127.0.0.1 is a localhost-ip address.
CalculatorServer.java:

 


(4) Define the Client side class:
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:

 


(5) Compile the all four source(java) files:
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
(6) Generate the Stub/Skeleton class by command:
There is a command rmic by which we can generate a Stub/Skeleton 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 above command produce the “CalculatorImpl_Stub.class” file.
(7) Start the RMI remote Registry:
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
So the system will open rmiregistry.exe (like a blank command prompt)
(8) Run the Server side class:
Now you need to run the RMI Server class.
Here CalculatorServer.java file is a working as a Server so run this fie.
Java CalculatorServer
(9) Run the Client side class(at another JVM):
Now open a new command prompt for the client because current command prompt working as a server and finally run the RMI client class.
Here CalculatorClient.java file is a working as a Client so finally run this fie.
Java CalculatorClient
Get the output like
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.

CLICK HERE for simple addition program of RMI in java

Prev