Basic Model of JAVA RMI: Execution of distributed object in Java is possible with the help of Java RMI. A client can access to a Java Object Hosted which is on a remote virtual machine from any JVM. It is two step procedures. First step is about execution of the clients and servers in Java. This will allows them to an inherent object-oriented appearance and objects to interact with all the characteristics of Java. So it’s mean we can access/run a server object from any java virtual machine (JVM) therefore, we can achieve the platform independently.
Second is that the Java RMI’s fundamental model always have a client program, and this client program is used to approach the remote objects from any Java virtual machine. For the Connection between a client and a remote object need a reference to object, this is hosted by a server program. Remote server object can be located by server in two different ways. These two procedures have their own methods to give remote reference to the client. These are procedures are,
•Explicitly. •Implicitly.
Both are used for “obtaining a remote reference”.
Java RMI Architecture:
To create a Math service using Java RMI I have follow these steps.
1. Define a remote interface 2. Implementation of the server 3. Implementation of the client 4. Compile the source code 5. Start Java RMI registry, server and then client.
Create the Remote Interface:
In Java RMI an interface is used to extend the “java.rmi.Remote” interface. Remote interface doesn’t have any method of its own, and it is used for tagging the remote objects, which makes possible to identify as it is. (Harold E. R., 2000). Set of remote methods also declared in the interface. Every single remote method must declare “java.rmi.RemoteException” or a superclass of the “RemoteException” in its throws section, in addition to any application’s specific exception.
Example which I have use in this paragraph for remote interface, “bite.example.SampleServer”. It declares the four methods, “Addition”, “Subtraction”, “Multiplication” and “Square”.
Following is the source code for “SampleServer.java”.
Sample Server package bite.example; import java.rmi.Remote; import java.rmi.RemoteException; public interface SampleServer extends Remote { public int addition(int x, int y) throws RemoteException; public int subtract(int x, int y)throws RemoteException; public int multiply(int x, int y)throws RemoteException; public int square(int x)throws RemoteException; public int getValue()throws RemoteException; public String getLastOp()throws RemoteException; }
Implementation of Sever:
In this context our “server” class exports the remote object and this server class holds a “main” method, this produce an instance of the remote object implementation. Then it combines that instance to a name in a Java RMI registry. The class which holds this “main” method possibly the implementation class by itself or another class completely.
In the class “Server” we declare the “main” method for server, and it also perform the remote interface SampleServer. Our server’s “main” method follows these two steps. A new remote object is produced and export in first step, second step is about the registration of an object with a Java RMI registry. Source code for class “SampleServerImpl.java” is as following.
Sample Server Impl. package bite.example; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class SampleServerImpl implements SampleServer { private int value =0; private String lastOp; SampleServerImpl(){ } public int addition(int x, int y) throws RemoteException { value = x + y; lastOp = “ADDITION”; return value; } public int subtract(int x, int y)throws RemoteException{ value = x – y; lastOp = “SUBTRACTION”; return value; } public int multiply(int x, int y)throws RemoteException{ value = x*y; lastOp = “MULTIPLY”; return value; } public int square(int x)throws RemoteException{ value = x*x; lastOp = “SQUARE”; return value; } /* Resource properties */ public int getValue() throws RemoteException { return value; }
public void setValue(int value) { this.value = value; } public String getLastOp()throws RemoteException { return lastOp; } public void setLastOp(String lastOp) { this.lastOp = lastOp; } public static void main(String args[]) { try { //Create and export a remote object SampleServerImpl obj = new SampleServerImpl(); SampleServer stub = (SampleServer)UnicastRemoteObject.exportObject(obj, 0); //Register the remote object with a Java RMI registry //and bind the remote object’s stub in the registry Registry registry = LocateRegistry.getRegistry(); registry.bind(“SampleServer”, stub);
System.err.println(“Server ready”); } catch (Exception e) { System.err.println(“Server exception: ” + e.toString()); e.printStackTrace(); } } }
Implementation of Client:
Client class acquires a “stub” for the registry on the server’s host, and it is searches remote object’s stub in the registry by their names, and then it invokes the “Addition”, “Subtraction”, “Multiply” and “Square” methods on the remote object using stub. Source code for the Client is following.
Sample Client
package bite.example; import java.io.*; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.Scanner; public class SampleClient { public static void main(String[] args) { String host = (args.length start java -Djava.rmi.server.codebase=file:C:/rmi/ -Djava.rmi.server.name=192.168.0.03 bite.example.SampleServerImpl” And then I have got the output “Server ready”
Start the Client: Final step is to start the client. When server was ready I open another window of command prompt line and then run the client as follow “C:rmi>java bite.example.SampleClient”