To develop a RMI application, we follow the following 6 points:
Here the client application need only two files, remote interface and client application. In the RMI application, both client and server interacts with the remote interface. The client application invokes methods on the proxy object, RMI sends the request to the remote JVM. The return value is sent back to the proxy object and then to the client application.
For creating the remote interface, extend the Remote interface and throws the RemoteException with all the methods of the remote interface. Here, we are creating a remote interface that extends the Remote interface. There is only one method named area() and it declares RemoteException.
import java.rmi.*; public interface Area extends Remote { public int area(int l, int b) throws RemoteException; }
Provide the implementation of the remote interface
Now provide the implementation of the remote interface. For providing the implementation of the Remote interface, we need to
In case, you extend the UnicastRemoteObject class, you must define a constructor that declares RemoteException.
import java.rmi.*; import java.rmi.server.*; public class AreaRemote extends UnicastRemoteObject implements Area{ AreaRemote() throws RemoteException{ super(); } public int area(int l,int b){return l*b;} }
Create the stub and skeleton objects using the rmic tool.
Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and creates stub and skeleton objects.
Start the registry service by the rmiregistry tool
Now start the registry service by using the rmiregistry tool. If you don't specify the port number, it uses a default port number. In this example, we are using the port number 5000.
rmiregistry 5000
Create and run the server application
Now rmi services need to be hosted in a server process. The Naming class provides methods to get and store the remote object.
In this example, we are binding the remote object by the name java.
import java.rmi.*; import java.rmi.registry.*; public class MyServer{ public static void main(String[] args){ try{ Area stub=new AreaRemote(); Naming.rebind("rmi://localhost:5000/silan",stub); } catch(Exception e) { System.out.println(e); } } }
Create and run the client application
At the client we are getting the stub object by the lookup() method of the Naming class and invoking the method on this object. In this example, we are running the server and client applications, in the same machine so we are using localhost. If you want to access the remote object from another machine, change the localhost to the host name (or IP address) where the remote object is located.
import java.rmi.*; public class MyClient{ public static void main(String[] args){ try{ Area stub=(Area)Naming.lookup("rmi://localhost:5000/silan"); System.out.println(stub.area(10,20)); } catch(Exception e){} } }
For running this rmi example,
1) compile all the java files
javac *.java
2) create stub and skeleton object by rmic tool
rmic AreaRemote
3) start rmi registry in one command prompt
rmiregistry 5000
4) start the server in another command prompt
java MyServer
5) start the client application in another command prompt
java MyClient
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc