Loading, please wait...

A to Z Full Forms and Acronyms

Java - Remote Method Invocation Concept.

This Program is used to calculate the Factorial Number using Java RMI concept.
Remote Method Invocation

RMI (Remote Method Invocation) is a way that a programmer, using the Java programming language and development environment, can write object-oriented programming in which objects on different computers can interact in a distributed network.

Working with RMI

Interface program
Implementation program
Server program
Client program
Example: Calculating the factorial of a given number using RMI concept.


Step 1: Interface Program

Declare the Methods only.
Open the first note pad, type the following program, and save the file as “rint.java”.


Interface Program (rint.java)

import java.rmi.*;  

public interface rint extends Remote // rint is the interface name   

{  

    double fact(double x) throws RemoteException; // fact() is the method declaraton only

}  



Step 2: Implementation Program

Define the Methods only.

Open the second note pad, type the following program, and save the file as “rimp.java”.



Implementation Program (rimp.java)

import java.rmi.*;  

import java.rmi.server.*;  

public class rimp extends UnicastRemoteObject implements rint // rimp is implementation name   

{  

    public rimp() throws RemoteException // rimp() is constructor   

        {}  

    public double fact(double x) throws RemoteException // fact() method definition   

        {  

            if (x <= 1) return (1);  

            else return (x * fact(x - 1));  

        }  

}  



Step 3: Server Program

It is the main program.

Open the third note pad, type the following program, and save the file as “rser.java”.



Server Program (rser.java)

import java.rmi.*;  

import java.net.*;  

public class rser // rser is server name   

{  

    public static void main(String arg[]) 

     {  

       try {  

               rimp ri = new rimp();  

               Naming.rebind("rser", ri);  

            } catch (Exception e)

         {  

            System.out.println(e);  

        }  

    }  

}  

 

Step 4: Client Program

It is the data send (request) to the Server program.

Open the fourth note pad, type the following program, and save the file as “rcli.java”.


Client Program (rcli.java)

import java.rmi.*;  

public class rcli // rcli is client name   

{  

    public static void main(String arg[]) 

     {  

        try

        {  

            rint rr = (rint) Naming.lookup("rmi://172.16.13.2/rser"); // system IP address   

            double s = rr.fact(5);  

            System.out.println("Factorial value is… : " + s);  

        } catch (Exception e) 

             {  

                System.out.println(e);  

             }  

    }  

}  

A to Z Full Forms and Acronyms

Related Article