Whats RMI ??The Java Remote Method InvocationApplication Programming Interface (API),Java RMI, is a Java API that performs theobject-oriented equivalent of remoteprocedure calls (RPC), with support fordirect transfer of Java objects over adistributed network.
RMI Vs RPCRPC is C based, and as such it has structuredprogramming semantics, on the other side, RMI is a Java based technology and its object orientedand can be used for complex applications.With RPC you can just call remote functions exported intoa server, RMI you can have references to remote objects andinvoke their methods, and also pass and return moreremote object references that can be distributed amongmany JVM instances, so its much more powerful.
Sockets Vs RMIIf your programming games, chat programs,streaming media, file transfer use socketsand read/write bytes. You have tighter controlover what is sent, you can optimize thestreams, by buffering, or compressing date.If you programming business applicationswhich require remote method calls, use RMI.Corba however would be useless for lets say,file transfer, or any streaming type app.
MarshallingDuring communication between two machinesthrough RPC or RMI, parameters are packedinto a message and then sent over the network.This packing of parameters into a message iscalled marshalling.On the other side these packed parameters areunpacked from the message which is calledunmarshalling.
RMI Application DevelopmentSteps for Developing the RMI Application• Define the remote interface• Define the class and implement the remoteinterface(methods) in this class• Define the Server side class• Define the Client side class• Compile the all four source(java) files• Generate the Stub/Skeleton class by command• Start the RMI remote Registry• Run the Server side class• Run the Client side class(at another JVM)
(1) Define the remote interface: This is an interface in which we are declaringthe methods as per our logic and furtherthese methods will be called using RMI.
(2) Define the class and implement theremote interface(methods) in this class: The next step is to implement the interfaceso define a class(ServerImpl.java) andimplements the interface(Vinterface.java) sonow in the class we must define the body ofthose methods.This class run on the remoteserver.
(3) Define the Server side class: The server must bind its name to the registry by passing thereference link with remote object name. For that here we are going to use rebind method which has twoarguments: The first parameter is a URL to a registry that includes the nameof the application and The second parameter is an object namethat is access remotely in between the client and server. This rebind method is a method of the Naming class which isavailable in the java.rmi.* package. The server name is specified in URL as a application name andhere the name is (ServerImp) in our application.
Note: The general form of the URL: rmi://localhost:port/application_name Here, 1099 is the default RMI port and127.0.0.1 is a localhost-ip address.
(4) Define the Client side class: To access an object remotely by client sidethat is already bind at a server side by onereference URL we use the lookup methodwhich has one argument that is a samereference URL as already applied at serverside class. This lookup method is a method of theNaming class which is available in thejava.rmi.* package.
(5) Compile the all four source(java) files: javac InterfaceFile.java javac InterfaceFileImpl.java javac Client.java javac Server.javaAfter compiled, in the folder we can see the fourclass files such as InterfaceFile.class , InterfaceFileImpl.class Client.class , Server.class
(6) Generate the Stub/Skeleton class bycommand: There is a command rmic by which we cangenerate a Stub/Skeleton class. Syntax: rmic class_name Here the class_name is a java file in whichthe all methods are defined so in thisapplication the class nameis ServerImpl.java file.
(7) Start the RMI remote Registry: The references of the objects are registeredinto the RMI Registry So now you need tostart the RMI registry for that use thecommand start rmiregistry So the system will open rmiregistry.exe (likea blank command prompt)
(8) Run the Server side class: Now you need to run the RMI Server class.(9) Run the Client side class(at another JVM): Now open a new command prompt for the clientbecause current command prompt working as aserver and finally run the RMI client class. Here Client.java file is a working as a Client sofinally run this fie. java Client