RMI (Remote Method Invocation)
RMI was most widely used technology(protocol) in old days to
JAVA to JAVA distributed application communication since JDK 1.1. Although now
a day’s most of web-based applications use modern technologies like REST based
web-services, still we SHOULD NOT FORGET to learn those type of old technologies
because they will help you to understand the modern solutions. And by understanding those type of old technologies,
you can easily understand, why design patterns, new architectural styles comes
in to play.
Scenario:
Volta is a weather forecasting company which have
several data collection centers to collect weather data to predict accurate
weather forecasting to its country citizens. Initially Volta’s head office
collects its sub stations data using manual documentations. Due to this manual
system, their predictions are not very up to date. So company management decided
to automate their manual process. They have signed an agreement with One Soft Solutions to implement
automated system.
 |
| Figure 1.1 Weather Station |
John works for One
Soft Solutions as a senior engineer.
One Soft Solutions asked John to design an approach to address the above
issue. In this situation, John have to face following problems:
- Almost all the One Soft Solutions software engineers are busy with new projects
except few java engineers.
- Those non-busy engineers are not familiar with
newer technologies like REST.
- Client asked for demo within couple of weeks.
- Solution should be expandable and should have
ability to migrate to newer technologies after other developers are come in to
play.
First of all John decided to explain RMI communication process in high level and then he created a documentation by including high level steps to to fallow to complete the project.
 |
| Figure 1.2 RMI Communication Workflow |
Step 01 :Designing and implementing the components of your distributed application.
- Create Remote Interface (WeatherStation.java)
- Implement the Remote Interface (WeatherStationImpl.java)
- Implement the Client
Step 02 :Compiling sources
Step 03 :Making classes network accessible
Step 04 :Start the rmiregistry
Step 05 :Starting the server and client applications
Step 01 :Designing and implementing the components of your distributed application
Create Remote Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface WeatherStation extends Remote
{
public String updateTodayConditions(
String subStation,
float temp,
float humidity,
float pressure
)
throws RemoteException;
}
|
Implement the Remote Interface
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class WeatherStationImpl extends
UnicastRemoteObject implements WeatherStation {
protected WeatherStationImpl() throws RemoteException {
super();
}
public String updateTodayConditions(
String subStation,
float temp,
float humidity,
float pressure
) {
System.out.println(
"Sub Station
:" + subStation +
" Temparature :" + temp +
" Humidity :" +
humidity+
" Pressure :" + pressure
);
return "Success";
}
}
|
WeatherStation Main Class
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class Server {
public static void main(String []args) throws RemoteException,
MalformedURLException{
WeatherStation
data = new WeatherStationImpl();
Naming.rebind("rmi://localhost:5000/WEATHERDATA",data);
System.out.println("Server
Started..!!");
}
}
|
Client Main Class
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Client {
public static void main(String []args) throws RemoteException, MalformedURLException,
NotBoundException{
WeatherStation weatherData=(WeatherStation);
Naming.lookup("rmi://localhost:5000/WEATHERDATA");
String response=weatherData.updateTodayConditions("Station
A",29,81, 54.1f);
System.out.println(response);
}
}
|
Step 02 :Compiling sources
Note:Before compile Client.java,you need to put remote interface (WeatherStation.class) to client folder. Client will invoke it as remote proxy.
Step 03 :Making classes network accessible
You need to allow client to access Server Stub's at run-time.Other wise server will throw remote exceptions.
Step 04 :Start the rmiregistry
Go to root folder of class folder and start rmiregisty by giving the port number.
"start rmiregistry 5000"
Step 05 :Starting the server and client applications
Start Server program 1st and then start the client program.
You have done.
Next:I will explain one of the famous design pattern in my next blog post.Then you will understand why we learned this old jargon.
References: