Calculator Using RMI(Remote Method Invocation) in Java
RMI (Remote Method Invocation) is an API used to access objects running on another JVM(Server-side). It is mainly used for the creation of distributed systems and is provided in Java Rome. Stub and Skeleton are the two objects used for handling communication between client and server. The following figure shows an overview of RMI.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- Stub Object: The stub object on the client machine builds an information block and sends this information to the server.
- Skeleton Object: The skeleton object passes the request from the stub object to the remote object. RMI contains a rmiregistry that holds all the server objects. The server binds all the objects to the registry and then the client fetches the object from the respective registry after which the client invokes the methods using the fetched objects.
Steps to create Calculator using RMI
Step 1: Create the Remote interface
First, we will create 4 interfaces(addition, subtraction, multiplication, division). These interfaces are helpful for the operation. To create remote interfaces we need to extend the remote interface and the method prototype within the interface should throw the RemoteException.
Step 2: Implementation of the remote interface
Now it is time to provide implementation to all the interfaces. To implement the remote interface, the class should extend to the UnicastRemoteObject class of the java.rmi package. Also, a default constructor needs to be created to throw the java.rmi.RemoteException from its parent constructor in the class.
Step 3: Create and execute the server application program.
The next step is to create the server application program and execute it on a separate command prompt. The rebind method of the Naming class is used to bind the remote object to the new name.
Step 4: Create and execute the client application program.
The next step is to create the client application program and execute it on a separate command prompt. The lookup method of Naming class is used to get the reference of the Stub object.
Step 5: Compile all the java program
Now we need to compile all the java program. To compile all the java program we need to open the command prompt and enter into the respective folder. Now enter into the folder where all the files are stored. We can compile all file at a time by using the following command;
Step 6: Create a stub and skeleton
The rmic tool is used to invoke the rmi compiler that creates the Stub and Skeleton objects. Its prototype is:
Step: 7 Start the registry service by the rmiregistry tool
Now start the rmi registry service by using rmiregistry tool. We need to specify port number. If we don’t specify the port number, it uses a default port number for example we are using port number 5259.
rmiregistry 5259 or rmiregistry & or start rmiregistry(windows)
After doing the above steps properly it may look like this:
Output: After successfully following the above steps you can see the following output or any confusion in the above steps you can watch the following video