JAVA - RMI

  • 1.	steps to write RMI program and run it.
    
    					    						

    	RMI : remote method invocation
    
    
    	1) Remote interface (client side and server side)
    	2) define a class which implements remote interface (server side)
    	3) launch registry
    	4) define server : write defined interface object in registry
    	5) define client : read object from registry and access specified method
    	===========================================================================================
    	1)	Remote interface
    		- define an interface
    		- extends this interface java.rmi.Remote interface
    		- declare functions in interface which client can access and server has to define
    		  with RemoteException
    		
    		import java.rmi.Remote;
    		import java.rmi.RemoteException;
    		interface AddInterface extends Remote
    		{
    			int add(int a, int b)throws RemoteException;
    		}
    	===========================================================================================
    	2) define a class which implements remote interface (server side)
    
    		- define a java class
    		- implements from AddInterface 
    		- override all methods of interface with public visibility mode
    		- extends class from UnicastRemoteObject
    		- have to define constructor with declaration of RemoteException
    			reason : UnicastRemoteObject is a super class of AddClass and
    					UnicastRemoteObject having a constructor with thrown RemoteException
    					that must be handle in child class (AddClass) constructor
    					otherwise has to throw from child class (AddClass) constructor
    		class AddClass extends UnicastRemoteObject implements AddInterface
    		{
    			AddClass()throws RemoteException
    			{}
    			public int add(int a, int b)throws RemoteException
    			{
    				return a+b;
    			}		
    		}
    	===========================================================================================
    	3) launch registry
    		open cmd prompt
    			rmiregistry 1000
    	===========================================================================================
    	4)	define server	
    		- create an object of remote interface
    		- write object into registry 
    		
    			class server
    			{
    				public static void main(String ar[])throws RemoteException
    				{
    					AddInterface object = new AddClass();
    					Naming.bind("rmi://localhost:1000/surat",object);
    				}
    			}
    	===========================================================================================
    	5)	define client
    		- get the object from registry
    		- call remote method
    			class client
    			{
    				public static void main(String ar[])throws RemoteException
    				{
    					AddInterface object = (AddInterface)Naming.lookup("rmi://localhost:1000/surat");
    					int n = object.add(10,20);
    					System.out.println(n);
    				}
    			}
    	===========================================================================================
    						

  • 2. 	Add two numbers using RMI
    
    					    						

    	//AddInterface : step : 1:
    	import java.rmi.Remote;
    	import java.rmi.RemoteException;
    	interface AddInterface extends Remote
    	{
    		int add(int a, int b)throws RemoteException;
    	}
    	
    	//AddClass: step : 2:	
    	import java.rmi.RemoteException;
    	import java.rmi.server.UnicastRemoteObject;
    
    	class AddClass extends UnicastRemoteObject implements AddInterface
    	{
    		AddClass()throws RemoteException
    		{
    		}
    		public int add(int a, int b)throws RemoteException
    		{
    			return a+b;
    		}		
    	}
    
    	//server : step : 3
    	import java.rmi.Naming;
    	class server
    	{
    		public static void main(String ar[])throws Exception
    		{
    			AddInterface object = new AddClass();
    			Naming.bind("rmi://localhost:1000/surat",object);	
    		}
    	}
    	
    	//client : step : 4
    	import java.rmi.RemoteException;
    	import java.rmi.Naming;
    	class client
    	{
    		public static void main(String ar[])throws Exception
    		{
    			AddInterface object = (AddInterface)Naming.lookup("rmi://localhost:1000/surat");
    			int n = object.add(10,20);
    			System.out.println(n);
    		}
    	}
    						

  • 3.	convert string into uppercase using RMI
    
    					    						

    	//step 1 : MyInterface
    	import java.rmi.Remote;
    	import java.rmi.RemoteException;
    	interface MyInterface extends Remote
    	{
    		String convert(String a)throws RemoteException;
    	}
    	
    	//step : 2 : MyClass
    	import java.rmi.RemoteException;
    	import java.rmi.server.UnicastRemoteObject;
    
    	class MyClass extends UnicastRemoteObject implements MyInterface
    	{
    		MyClass()throws RemoteException
    		{
    		}
    		public String convert(String a)throws RemoteException
    		{
    			return a.toUpperCase();
    		}		
    	}
    	
    	//step : 3 : server
    	import java.rmi.Naming;
    	class server
    	{
    		public static void main(String ar[])throws Exception
    		{
    			MyInterface object = new MyClass();
    			Naming.bind("rmi://localhost:1000/surat",object);	
    		}
    	}
    	//step : 4 : client
    	import java.rmi.RemoteException;
    	import java.rmi.Naming;
    	class client
    	{
    		public static void main(String ar[])throws Exception
    		{
    			MyInterface object = (MyInterface)Naming.lookup("rmi://localhost:1000/surat");
    			String n = object.convert("welcome");
    			System.out.println(n);
    		}
    	}