Wednesday, August 21, 2019
Differences Between RPC And RMI
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 Javas 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 APIs. 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. The argument for Naming.lookup() method is name of a remote object and locates the object on the network. The objects 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. 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 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 numbers; // Integer array for holding repeat private ArrayList lot; private java.util.Random gen; public ArrayList getLottoryNumber(){ lot.clear(); for(int i=0;i
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.