English 中文(简体)
Java RMI - RMI Application
  • 时间:2024-11-05

Java RMI Apppcation


Previous Page Next Page  

To write an RMI Java apppcation, you would have to follow the steps given below −

    Define the remote interface

    Develop the implementation class (remote object)

    Develop the server program

    Develop the cpent program

    Compile the apppcation

    Execute the apppcation

Defining the Remote Interface

A remote interface provides the description of all the methods of a particular remote object. The cpent communicates with this remote interface.

To create a remote interface −

    Create an interface that extends the predefined interface Remote which belongs to the package.

    Declare all the business methods that can be invoked by the cpent in this interface.

    Since there is a chance of network issues during remote calls, an exception named RemoteException may occur; throw it.

Following is an example of a remote interface. Here we have defined an interface with the name Hello and it has a method called printMsg().

import java.rmi.Remote; 
import java.rmi.RemoteException;  

// Creating Remote interface for our apppcation 
pubpc interface Hello extends Remote {  
   void printMsg() throws RemoteException;  
} 

Developing the Implementation Class (Remote Object)

We need to implement the remote interface created in the earper step. (We can write an implementation class separately or we can directly make the server program implement this interface.)

To develop an implementation class −

    Implement the interface created in the previous step.

    Provide implementation to all the abstract methods of the remote interface.

Following is an implementation class. Here, we have created a class named ImplExample and implemented the interface Hello created in the previous step and provided body for this method which prints a message.

// Implementing the remote interface 
pubpc class ImplExample implements Hello {  
   
   // Implementing the interface method 
   pubpc void printMsg() {  
      System.out.println("This is an example RMI program");  
   }  
} 

Developing the Server Program

An RMI server program should implement the remote interface or extend the implementation class. Here, we should create a remote object and bind it to the RMIregistry.

To develop a server program −

    Create a cpent class from where you want invoke the remote object.

    Create a remote object by instantiating the implementation class as shown below.

    Export the remote object using the method exportObject() of the class named UnicastRemoteObject which belongs to the package java.rmi.server.

    Get the RMI registry using the getRegistry() method of the LocateRegistry class which belongs to the package java.rmi.registry.

    Bind the remote object created to the registry using the bind() method of the class named Registry. To this method, pass a string representing the bind name and the object exported, as parameters.

Following is an example of an RMI server program.

import java.rmi.registry.Registry; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 

pubpc class Server extends ImplExample { 
   pubpc Server() {} 
   pubpc static void main(String args[]) { 
      try { 
         // Instantiating the implementation class 
         ImplExample obj = new ImplExample(); 
    
         // Exporting the object of implementation class  
         // (here we are exporting the remote object to the stub) 
         Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);  
         
         // Binding the remote object (stub) in the registry 
         Registry registry = LocateRegistry.getRegistry(); 
         
         registry.bind("Hello", stub);  
         System.err.println("Server ready"); 
      } catch (Exception e) { 
         System.err.println("Server exception: " + e.toString()); 
         e.printStackTrace(); 
      } 
   } 
} 

Developing the Cpent Program

Write a cpent program in it, fetch the remote object and invoke the required method using this object.

To develop a cpent program −

    Create a cpent class from where your intended to invoke the remote object.

    Get the RMI registry using the getRegistry() method of the LocateRegistry class which belongs to the package java.rmi.registry.

    Fetch the object from the registry using the method lookup() of the class Registry which belongs to the package java.rmi.registry.

    To this method, you need to pass a string value representing the bind name as a parameter. This will return you the remote object.

    The lookup() returns an object of type remote, down cast it to the type Hello.

    Finally invoke the required method using the obtained remote object.

Following is an example of an RMI cpent program.

import java.rmi.registry.LocateRegistry; 
import java.rmi.registry.Registry;  

pubpc class Cpent {  
   private Cpent() {}  
   pubpc static void main(String[] args) {  
      try {  
         // Getting the registry 
         Registry registry = LocateRegistry.getRegistry(null); 
    
         // Looking up the registry for the remote object 
         Hello stub = (Hello) registry.lookup("Hello"); 
    
         // Calpng the remote method using the obtained object 
         stub.printMsg(); 
         
         // System.out.println("Remote method invoked"); 
      } catch (Exception e) {
         System.err.println("Cpent exception: " + e.toString()); 
         e.printStackTrace(); 
      } 
   } 
}

Compipng the Apppcation

To compile the apppcation −

    Compile the Remote interface.

    Compile the implementation class.

    Compile the server program.

    Compile the cpent program.

Or,

Open the folder where you have stored all the programs and compile all the Java files as shown below.

Javac *.java

Stored Programs

Executing the Apppcation

Step 1 − Start the rmi registry using the following command.

start rmiregistry

Start Execution

This will start an rmi registry on a separate window as shown below.

Separate Window

Step 2 − Run the server class file as shown below.

Java Server

Run Server

Step 3 − Run the cpent class file as shown below.

java Cpent 

Run Cpent

Verification − As soon you start the cpent, you would see the following output in the server.

Output Advertisements