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.

Working of RMI

Here,

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

AddI.java



filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a AddInterface interface
import java.rmi.Remote;
public interface AddInterface extends Remote {
    // Declaring the method prototype
    public int add(int x, int y) throws RemoteException;
}

chevron_right


SubI.java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a SubInterface interface
import java.rmi.Remote;
public interface SubInterface extends Remote {
    // Declaring the method prototype
    public int sub(int x, int y) throws RemoteException;
}

chevron_right


MulI.java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a MulInterface interface
import java.rmi.Remote;
public interface MulInterface extends Remote {
    // Declaring the method prototype
    public int mul(int x, int y) throws RemoteException;
}

chevron_right


DivI.java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a DivInterface interface
import java.rmi.Remote;
public interface DivInterface extends Remote {
    // Declaring the method prototype
    public int div(int x, int y) throws RemoteException;
}

chevron_right


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.

Impl.java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement the AddInterface,
// subInterface, MulInterface, and DivInterface
import java.rmi.*;
import java.rmi.server.*;
  
public class Impl extends UnicastRemoteObject
    implements AddInterface, SubInterface, MulInterface,
               DivInterface {
  
    // Default constructor to throw RemoteException
    // from its parent constructor
    public Impl() throws Exception { super(); }
  
    // Implementation of the AddInterface,
    // subInterface, MulInterface, and DivInterface
    public int add(int x, int y) { return x + y; }
    public int sub(int x, int y) { return x - y; }
    public int mul(int x, int y) { return x * y; }
    public int div(int x, int y) { return x / y; }
}

chevron_right


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.



Server.java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program for server application
import java.rmi.*;
import java.rmi.registry.*;
public class Server {
    public static void main(String[] args) throws Exception
    {
  
        // Create an object of the interface
        // implementation class
        Impl obj = new Impl();
  
        // Binds the remote object by the name ADD
        Naming.rebind("ADD", obj);
  
        System.out.println("Server Started");
    }
}

chevron_right


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.

Client.java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program for client application
import java.rmi.*;
import java.util.*;
public class Client {
    public static void main(String[] args) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        while (true) {
            // User Menu
            System.out.println(
                "\n1.Addition\n2.Substraction\n3.multiplication\n4.division\n5.Exit");
            System.out.println("Enter the option:");
            int opt = sc.nextInt();
            if (opt == 5) {
                break;
            }
            System.out.println(
                "Enter the the first number:");
            int a = sc.nextInt();
            System.out.println("Enter the second number:");
            int b = sc.nextInt();
            int n;
            switch (opt) {
            case 1:
                // lookup method to find reference of remote
                // object
                AddInterface obj
                    = (AddInterface)Naming.lookup("ADD");
                n = obj.add(a, b);
                System.out.println("Addition= " + n);
                break;
            case 2:
                SubInterface obj1
                    = (SubInterface)Naming.lookup("ADD");
                n = obj1.sub(a, b);
                System.out.println("Substaction= " + n);
                break;
            case 3:
                MulInterface obj2
                    = (MulInterface)Naming.lookup("ADD");
                n = obj2.mul(a, b);
                System.out.println("Multiplication = " + n);
                break;
            case 4:
                DivInterface obj3
                    = (DivInterface)Naming.lookup("ADD");
                n = obj3.div(a, b);
                System.out.println("Division = " + n);
                break;
            }
        }
    }
}

chevron_right


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;

javac *.java

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:

rmic classname

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:

Calculator using rmi in java - rmiregistry tool

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


Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.