Differences Between RPC And RMI

In enterprise application there is possibility that resources need to be access across multiple system to do a business process. One of the Java’s solutions for distributed application is Remote Method Invocation (RMI).

Objective of this paper is understand how a RMI system works in real time scenarios and how enterprise application can implemented using java RMI API’s. An enterprise distributed system is a set of objects that isolates the consumers of services from the providers of services by a well-defined interface. In other words, clients are completely abstracted from the implementation of business method as data structure and executable code. This is how one can distinguish with simple client / server application with object based remote invocation method model.

In the distributed enterprise object model, a client sends a request message to an object, which in turns analyzes the request to decide what service to perform. This business entity service, selection could be performed by either the object or a broker.

Remote Method Invocation (RMI):

RMI is one of the possible ways to access distributed business objects from another JVM and RMI uses object serialization to marshal and unmarshal parameters. If you want send objects across the wire, your class (object) need to implements Serializable interface.

Here is the RMI architecture and how RMI ‘s works internally.

RMI Transport Layer

Client

Server

skeleton

stub

Interface

Interface

Client Process

Server Process

Process

According to sun web site “Java Remote Method Invocation (Java RMI) enables the programmer to create distributed Java technology-based to Java technology-based applications, in which the methods of remote Java objects can be invoked from other Java virtual machines*, possibly on different hosts. RMI uses object serialization to marshal and unmarshal parameters and does not truncate types, supporting true object-oriented polymorphism.”

When a enterprise server process wants to export some remote method invocation based service to consumer, it does so by registering remote method invocation enabled objects with its local rmiregistry (Registry interface). Every remote object is registered with a name consumer can use to reference it. A consumer can obtain a reference of stub to the remote object by requesting for the remote object by name through the Naming interface.

Read also  Processor Is The Heart Of The Computer

The argument for Naming.lookup() method is name of a remote object and locates the object on the network. The object’s fully qualified name can be composed with host name port and the name of the object look like url syntax for the naming resource.

Few of the terminology one should know about RMI are listed below.

rmiregistry — A executable program used to bind remote object to names and that provides a bootstrap naming service which is used by servers on the server machine. JVMs on client and server machines can then look up remote objects and make remote method invocations.

rmic: The rmic compiler tool generates stub, and skeleton class files for remote objects. These classes’ files are generated from the compiled Java language classes that contain remote object implementations (implemented java.rmi.Remote interface).

skeleton : A skeleton for a remote object is a JRMP protocol server side business object that contains a method which dispatch calls to the actual remote object realization.

stub: A proxy object for a remote object which is responsible for delegating method on remote objects to the server where implementation of the actual remote object resides. A consumer program reference to a remote object, therefore, is actually a reference to a local stub.

Remote Interface: The Remote interface serves to recognize interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface.

Difference between RPC and RMI

Remote procedure call (RPC) is a network communication protocol with server and client architecture and the idea behind RPC is to call implemented code remotely as if we were just calling a function. The only difference between RMI and RPC is in case of RPC functions are invoked through a proxy function, and in case of RMI we invoke methods through a proxy object.

RMI is java solution to RPC, with connectivity to existing systems using native methods. RMI can take a natural, direct, and fully powered approach to provide a enterprise distributed computing technology that allows us to add Java functionality throughout the system. To get the cross-platform portability that Java provides, RPC requires a lot more overheads than RMI. RPC has to convert the arguments between architecture so that each computer can use its native data type.

Read also  An art gallery

Java-RMI is tightly coupled with the Java language. Whereas RPC is not specific to any single language and one can implement RPC using different language.

Because RMI can implemented using Java, its get all the advantages like object oriented, parallel computing, design pattern, easy to write and re use, safe and secure, Write once and run anywhere. But in the case of RPC, to achieve any of these advantages one has to write implementation code.

Sample application:

To demonstrate RMI and distributed application in real time I have implemented a Lottery system. The Lottery system is developed as per UK Lotto system. Assuming that user before using this RMI client application already purchased the lottery ticket.

Lottery client system displays the welcome message to customer.

Lottery system also displays the winning amount to the customer. The Lottery system is developed as per UK Lotto system. But simplifying system I have modified certain rules. Here how winner is chosen.

Jackpot, Match 6: £500,000

Match 5 numbers: £1,500

Match 4 numbers: £60

Match 3 numbers: £10.

System asks customer to enter positive integer number ranging 1 to 49.

Once he enters all 6 numbers Lottery system generate 6 winning non-repeating random number between 1 to 49.

System checks the match between user entered number and server generated number and calculate winning amount and display the result

Implementation:

Here is how I have implemented Lottery system

Define a remote interface

import java.rmi.Remote;

public interface LotteryGenerator extends Remote {

public ArrayList<Integer> getLottoryNumber() throws java.rmi.RemoteException;

}

Implement the remote interface

Below is the just a snippet of the implementation class for the remote interface. I have not listed the supporting private methods of the class.

import java.rmi.RemoteException;

public class LotteryGeneratorImpl extends java.rmi.server.UnicastRemoteObject implements LotteryGenerator{

private ArrayList<Integer> numbers; // Integer array for holding repeat

private ArrayList<Integer> lot;

private java.util.Random gen;

public ArrayList<Integer> getLottoryNumber(){

lot.clear();

for(int i=0;i<6;i++) {

lot.add(getNextInt());

}

System.out.println(“Generated Lottery number:”+lot);

return lot;

}

}

Develop the server

Create an instance of remote object and register the remote object with RMI registry. Here is the code snippet for the LotterServer.java

import java.rmi.Naming;

import java.rmi.Remote;

Read also  A Study About Blue Eye Technology

public class LotteryServer {

public LotteryServer() {

try {

LotteryGenerator c = new LotteryGeneratorImpl();

Naming.rebind(“rmi://127.0.0.1:1099/LotteryGenerator”,(Remote) c);

} catch (Exception e) {

System.out.println(“Trouble: ” + e);

e.printStackTrace();

}

}

public static void main(String args[]) {

new LotteryServer();

System.out.println(“*****Server started******”);

}

}

If RMI application running on default port 1099 then the rebind statement will be Naming.rebind(“rmi://127.0.0.1:1099/LotteryGenerator”,(Remote) c); and if you run the RMI registry on a non default port number , for example on port 5100, then binding statement becomes:

Naming.rebind(“rmi://127.0.0.1:5100/LotteryGenerator”,(Remote) c);

Develop a Client

Next step is implementing the RMI Lottery client and the client remotely invokes the method getLottoryNumber specified in the LotterGenerator remote interface. To do so however, the consumer program must first obtain an object reference to the LotteryGeneratorImpl remote object from the RMI registry. Once an object reference is obtained, the getLottoryNumber method is invoked.

import java.rmi.Naming;

import java.util.ArrayList;

import java.util.Scanner;

public class LotteryClient {

public static void main(String[] args) {

displayWelcomeMessage();

Scanner scanner = new java.util.Scanner(System.in);

System.out.println(“Please Enter 6 integer numbers ranging 1 to 49)”);

ArrayList<Integer> clientArray=new ArrayList<Integer>();

for(int ii=0;ii<6;ii++){

int val = scanner.nextInt();

clientArray.add(val);

}

System.out.println(“You have entered: “+clientArray);

try {

LotteryGenerator c = (LotteryGenerator) Naming.lookup(“rmi://localhost/LotteryGenerator”);

ArrayList servList=c.getLottoryNumber();

System.out.println(“Lottery Winning numbers:”+servList);

int count=checkWin(clientArray,servList);

dispalyResult(count);

System.out.println(“Thank you !!”);

} catch (java.net.MalformedURLException murle) {

System.out.println();

System.out.println(“MalformedURLException”);

System.out.println(murle);

} catch (java.rmi.RemoteException re) {

System.out.println();

System.out.println(“RemoteException”);

System.out.println(re);

} catch (java.rmi.NotBoundException nbe) {

System.out.println();

System.out.println(“NotBoundException”);

System.out.println(nbe);

}

}

private static void dispalyResult(int count) {

//implemenattion for display code

}

private static void displayWelcomeMessage(){

//display the welcome message and Lottery winning amount.

}

public static int checkWin(ArrayList clientArray,ArrayList serverArray){

int count=0;

for(int ii=0;ii<6;ii++){

int clientVal=(Integer)clientArray.get(ii);

for(int k=0;k<6;k++){

int serVal=(Integer)serverArray.get(k);

if(clientVal==serVal) {

count++; break;

}

}

}

return count;

}

}

Running RMI application:

Here is the steps how one can execute Lottery application.

Compile all the java classes.

Using rmic command, create stubs and skeleton for the implemented remote object.

rmic LotteryGeneratorImpl

Start the RMI registry using rmiregistry command

Execute LotteryServer application

Execute LotteryClient application

Conclusion:

Because it easy implementation nature RMI has definite advantages over other distributed application methodology like RPC, Corba etc. Because it is implemented using java it’s get all the advantages like object oriented, parallel computing(Multi-threading), design pattern, easy to write and re use, safe and secure, Write once and run anywhere. Also one can integrate RMI based application any other application because of its platform independent ability.

Order Now

Order Now

Type of Paper
Subject
Deadline
Number of Pages
(275 words)