Java RMI – Here we will see with an example how Java RMI works n can be used for creating distributed application over the network.
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.
In example we will four different files :-
- RemoteMathInterface.java – A Remote Interface for Mathematical functions
- RemoteMath.java – Class implementing RemoteMathInterface
- Server.java – Class runs as a server creating RemoteMath object
- Client.java – Class can be run on any machine over the network which call method of Server
- In addition security.policy – a security file contains policies n guidelines for jvm
Here is the code for RemoteMathInterface.java
// this the interface which is shared with different classes which // wants to get access to the RemoteMath class functions over the network // by sandeep nandal 2012 import java.rmi.*; public interface RemoteMathInterface extends Remote{ public int multiply(int x, int y) throws RemoteException; public int add(int x, int y) throws RemoteException; }
Here is the code for RemoteMath.java
// this is the class used by server to create the object and serve // the requests from various clients over the network // by sandeep nandal 2012 import java.rmi.*; import java.rmi.server.*; public class RemoteMath extends UnicastRemoteObject implements RemoteMathInterface{ public RemoteMath() throws RemoteException{ super(); } public int multiply(int x, int y){ return x*y; } public int add(int x, int y){ return x+y; } }
Here is the code for Server.java
// server class running on server machine which creates the object of RemoteMath class // and serves the requests from various clients over the network // by sandeep nandal 2012 import java.rmi.*; import java.rmi.registry.LocateRegistry; public class Server{ public static void main(String arg[]){ try{ RemoteMath rm = new RemoteMath(); Naming.rebind("rmi://127.0.0.1:6000/MathService", rm); System.out.println("Server is waiting for request on port 6000...."); }catch(Exception e){ e.printStackTrace(); } } }
Here is the code for Client.java
// this is the client file which will invoke a function on server and // then collect the result through rmi over the network // by sandeep nandal 2012 import java.rmi.*; public class Client{ public static void main(String arg[]){ int x = 20, y = 30; int sum = 0, mul = 0; try{ if(System.getSecurityManager() == null){ System.setSecurityManager(new RMISecurityManager()); } RemoteMathInterface server = (RemoteMathInterface) Naming.lookup("rmi://127.0.0.1:6000/MathService"); sum = server.add(x, y); mul = server.multiply(x, y); System.out.println("Here is result fetched - "); System.out.println("sum is " + sum); System.out.println("mul is " + mul); }catch(NotBoundException nbe){ System.out.println("NotBoundException occured : \n" + nbe.getMessage()); } catch(java.net.MalformedURLException mfue){ System.out.println("MalformedURLException occured : \n" + mfue.getMessage()); } catch(RemoteException re){ System.out.println("RemoteException occured :\n" + re.getMessage()); } } }
Here is the security.policy file for security policies for jvm
grant { // Allow the client to connect to any port above 1024 permission java.net.SocketPermission "*:1024-", "connect"; };
Now we are ready with all the code needed for demonstrating the use of rmi. It’s time to compile the source files using javac compiler by the following command.
javac RemoteMathInterface.java RemoteMath.java Server.java Client.java
Now we will have all the class files of these source files. Now we need to create stub from RemoteMath class using rmic tool provided by java using the following command
rmic RemoteMath
It will generate RemoteMath_Stub.class out of RemoteMath.class. Therefore, whosoever client wants to use the functionality of object running on our server will need to have access to the RemoteMathInterface and the RemoteMath_Stub.class.
Now start the rmiregistry on the server machine using the following command for 6000 port
rmiregistry 6000
It’s time to run the server for accepting the requests from the remote clients using java
java Server
this will give the following output:
Server is waiting for request on port 6000....
Now run the client using the security.policy file as the following command
java -Djava.security.policy=security.policy Client
Now we will have the following output on client terminal
Here is result fetched - sum is 50 mul is 600
This will run the client and request the server for the functionality then return the sum and multiplication of the two numbers from server and display the result.
NoteThe server and the client can be on different machines connected with network. We only need to change the ip address provided in the source code for the server and then follow the same steps to make work on a network. We used 127.0.0.1 for localhost machine. So that our same machine is working as a server and as client. We need to open three terminals in order to demonstrate this code. One for rmiregistry, second for server and the third for the client.
If in doubt, feel free to post your queries.
Twit this with your twitter acount : Tweet
Pingback: miguel