Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement Java's Remote Method Invocation (RMI) through a simple Java program ?.

Program to implement Java's Remote Method Invocation (RMI) through a simple Java program.

Step 1. Create a simple Java Interface AddService.java

package com.hubberspot.rmi.server;

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


public interface AddService extends Remote {

 public int add(int number1 , int number2) throws RemoteException;

}



Step 2. Create a simple Java Class AddServiceImpl.java which provides implementation to AddService.java

package com.hubberspot.rmi.server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;


public class AddServiceImpl extends UnicastRemoteObject implements AddService {

 private static final long serialVersionUID = 1L;

 protected AddServiceImpl() throws RemoteException {

  super();

 }

 @Override
 public int add(int number1, int number2) throws RemoteException {

  return number1 + number2;
 }

}


Step 3. Create a Server Java Class AddServer.java which creates a Object of AddServiceImpl and binds it to RMI Registry by lookup name as "AddServiceLookup".

package com.hubberspot.rmi.server;

import java.rmi.Naming;

public class AddServer {

 public static void main(String[] args) {

  try {  
   
   AddServiceImpl addService = new AddServiceImpl();
   
   Naming.rebind("AddServiceLookup", addService);
   
  } catch(Exception e) { 
   
   System.out.println("Exception is : " + e);
   
   e.printStackTrace();
   
  }
 }
}




Step 4. Create a Java Project which will have a Java Client which will call add method of AddService Interface.

package com.hubberspot.rmi.client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import com.hubberspot.rmi.server.AddService;

public class AddClient {
 
 public static void main(String[] args) {
  
  String lookUpUrl = "rmi://127.0.0.1/AddServiceLookup";
  
  AddService addService;  try {
   addService = (AddService) Naming.lookup(lookUpUrl);
    
  int number1 = 10;
  
  int number2 = 20;
  
  int sum = addService.add(number1 , number2);
  
  System.out.println("Sum of two numbers : " + number1 + " + " + number2 + " is: " + sum);
  
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NotBoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

}




Step 5. Generate Stubs for the AddServiceImpl Java class. The stubs created will act as a Proxy object for the AddServiceImpl.java. Command for generating stubs is as follows:

To generate stubs and skeletons, you use a tool called the RMI compiler:

rmic AddServerImpl

"AddServerImpl_Stub.class" will be created which will act as a stub for the AddServiceImpl class.


Step 7. Add AddService.class and AddServerImpl_Stub.class to Java Client project having AddClient.class

Step 8. Starting the RMI registry

Start the RMI Registry for registering the lookup name for AddServiceimpl.java. The command to start RMI Registry is:

start rmiregistry

Step 9. Run AddServer.java created in step 3

Step 10. Run AddClient.java created in step 4

Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot