Java RMI (Remote Method Invocation)

Hi, now I am gonna show you how to write java RMI process to create server and client.
from oracle documentation

"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."

So from here, we can easily understand that java rmi server can be located in remote or same jvm host. we can call the remote method from a client that can be located different hosts or same from sever. so what is the difference between "Web Service" and "Java RMI" as both are called from remote client and send responses to the client. Well, the difference is "Java RMI" server and client both should be java process that means server and client both should be located in jvm and run under a specified jvm host. On the other hand web service is independent from client ( Client Agnostic ). The client may be either java process or C++ or php etc. as web service (JAX-WS) messages are xml (SOAP) format so it's client independent. if we can call with proper xml format to the server then server back to us response in xml format.anyways,

Now first go through server process.
1) Create an interface say "AdditionInterFace" which extends a Remote object.

2) declare a method say from the example we declare "add" method.
3) Now create an implementation class which inherits "UnicastRemoteObject" and also implements the "AdditionInterFace" interface. here we override the add method like this.


 @Override  
   public int add(int a, int b) throws RemoteException {  
     int result = a+b;  
     return result;  
   }  

4) Now came to the main class which runs the server process which is for registering our interface implementation class to a specific port and bind here with an alias name.by default port is 1099.


 Registry registry = LocateRegistry.createRegistry(1099);  
     registry.rebind("hello", object);  

don't worry about security policy file, I will explain it later.
so server-side code is done.

Now come to the client part.
1) get the server object instance from registry given below,


 Registry registry = LocateRegistry.getRegistry("localhost", 1099);  

here I give "localhost" as I run on the same machine. you can give any ip address where rmi server running.
2) From the registry you should extract your server object by the alias name and cast it to your own object.

 object = (AdditionInterFace)registry.lookup("hello");  

the client code given below

 package com.java.rmi.client;  
 import com.test.server.AdditionInterFace;  
 import java.net.MalformedURLException;  
 import java.rmi.NotBoundException;  
 import java.rmi.RMISecurityManager;  
 import java.rmi.RemoteException;  
 import java.rmi.registry.LocateRegistry;  
 import java.rmi.registry.Registry;  
 /**  
  *  
  * @author Ataur Rahman  
  */  
 public class RmiClient {  
   public static void main(String[] args) throws NotBoundException, RemoteException, MalformedURLException {  
     AdditionInterFace object = null;  
     System.setProperty("java.security.policy", "path_to_your_policy_file");  
     System.setSecurityManager(new RMISecurityManager());  
     Registry registry = LocateRegistry.getRegistry("localhost", 1099);  
     object = (AdditionInterFace)registry.lookup("hello");  
     int result = object.add(5, 5);  
     System.out.println("the result is = "+result);  
   }  
 }  

you are almost done. now run the server method and then run the client class. hopefully you get the result. for accessing different jvm host you need the access permission. so create a "Security.policy" file in your class path both in server and client and add the following code in that file.

  grant {  
   permission java.security.AllPermission;  
   };  

Here i put "Security.policy" file in client as well as server.
in my cases the whole project structure like this.



Key Point:
1) If you run your server side code in a different jvm host, then you can make a jar of the server side object (impl class) and include it to the client class path.

Happy Coding........ cheers ......... :)

Comments

Popular posts from this blog

UUID to BigInteger conversion and BigInteger to UUID conversion

ActiveMQ message producer and consumer with durable subscriber example

Create Maven Local Repository, Upload your own artifact & download jar from local mirror with Artifactory and Nexus OSS