How to Implement Java RMI?

10 Mar 2011 by

Java RMI Implementation

Java RMI Implementation

Implement Java RMI could be ever easier if you follow my rule strictly. You need to understand Java RMI Overview before you can catch the code.

According to the above figure, there are 4 steps need to be involved in Java RMI development. I will give you an example of creating Java RMI application dealing with Fibonacci and Factorial calculation using badly recursive algorithm. These two methods will be available for invocation at the server side. I will call the server program as PowerServer and client program as PowerClient.

Step 1: Define Object Server Interface

  • Extend the java.rmi.Remote class
  • Declare methods with throwing RemoteException

IPower.java

import java.rmi.*;
/**
 *
 * @author http://lycog.com
 */

//Define interface by extending Remote class
public interface IPower extends Remote{
  //Declare available methods and must throw RemoteException
  long fibonacci(int n) throws RemoteException;
  long factorial(int n) throws RemoteException;
}

Step 2: Define Server Implementation Class

  • Extend UnicastRemoteObject
  • Implement interface done in Step 1
  • Throw RemoteException on constructor

PowerImplementation.java

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

/**
 *
 * @author http://lycog.com
 */
public class PowerImplementation extends UnicastRemoteObject
                implements IPower{

  //Throw RemoteException on constructor
  public PowerImplementation() throws RemoteException{

  }

  //Overwrite IPower interface
  public long fibonacci(int n) throws RemoteException{
    //Log connection from a client
    try {
      System.out.println("Got invocation from " + getClientHost());
    } catch (ServerNotActiveException ex) {
      ex.printStackTrace();
    }

    return calculateFibonacci(n);
  }

  //Overwrite IPower interface
  public long factorial(int n) throws RemoteException{
    //Log connection from a client
    try {
      System.out.println("Got invocation from " + getClientHost());
    } catch (ServerNotActiveException ex) {
      ex.printStackTrace();
    }

    return calculateFactorial(n);
  }

  //Fibonacci calculation
  private long calculateFibonacci(int n){
    if (n == 0) {
      return 0L;
    }
    if (n == 1) {
      return 1L;
    }

    return (calculateFibonacci(n - 1) + calculateFibonacci(n - 2));
  }

  //Factorial calculation
  private long calculateFactorial(int n){
    if(n<=0) return 1L;

    return n*calculateFactorial(n-1);
  }
}

Step 3: Create and Register Server Object

  • Instantiate server object implemented in step 2
  • Get reference to registry service
  • Register server object to registry service

PowerServer.java

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

/**
 *
 * @author http://lycog.com
 */
public class PowerServer {
  public static void main(String[] args){
    try{
      //Create server object
      PowerImplementation power = new PowerImplementation();

      //Reference to registry service by creating registry service
      Registry registry = LocateRegistry.createRegistry(1099);

      //Register server object to registry with unique name
      registry.rebind("PowerObject", power);

      System.out.println("Server starts...");
    }catch(RemoteException re){
      re.printStackTrace();
    }
  }
}

Step 4: Develop Client Program

  • Get reference to server’s registry
  • Locate server object from server’s registry service

PowerClient.java

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

/**
 *
 * @author http://lycog.com
 */
public class PowerClient {
  public static void main(String[] args){
    try{
      //Get reference from server's registry
      Registry registry = LocateRegistry.getRegistry("127.0.0.1");

      //Lookup server object from server's registry
      IPower power_proxy = (IPower)registry.lookup("PowerObject");

      //Invoke server object's methods
      System.out.println("Fibonacci(10)=" + power_proxy.fibonacci(10));
      System.out.println("Factorial(10)=" + power_proxy.factorial(10));
    }catch(NotBoundException nbe){
      nbe.printStackTrace();
    }catch(RemoteException re){
      re.printStackTrace();
    }
  }
}

 

2 responses so far

Leave a Reply