RMI Tutorial

RMI TUTORIAL

Remote Method Invocation (RMI)

Description:

Java RMI allowed programmer to execute remote function class using the same semantics as local functions calls.

  • To transfer data from one host to another.
  • RMI is a specification that enables one JVM  to invoke methods in an object located in another JVM.
  • These JVMs could be running on different computers or running as separate pocesses on the same computer

  • The server must first bind its name to the registry
  • The client lookup the server name in the registry to establish remote references.
  • The Stub serializing the parameters to skeleton, the skeleton invoking the remote method and serializing the result back to the stub.
  • A client invokes a remote method; the call is first forwarded to stub.
  • The stub is responsible for sending the remote call over to the server-side skeleton
  • The stub opening a socket to the remote server, marshaling the object parameters and forwarding the data stream to the skeleton.
  • A skeleton contains a method that receives the remote calls, unmarshals the parameters, and invokes the actual remote object implementation.

 

Marshaling

The process of gathering data and transforming it into a standard format before it is transmitted over a network so that the data can transcend network boundaries.

UnicastRemoteObject

All remote objects must extend UnicastRemoteObject, which provides functionality that is needed to make objects available for remote machines.

  • Default RMI port is 1099.

Step of developing an RMI System:

  1. Define the remote interface
  2. Develop the remote object by implementing the remote interface.
  3. Develop the client program.
  4. Compile the Java source files.
  5. Generate the client stubs and server skeletons.
  6. Start the RMI registry.
  7. Start the remote server objects.
  8. Run the client

Example of rmi application:

  1. Define the remote interface

Code:

[java]

import java.rmi.*;

public interface HelloInterface extends Remote{

public String getMessage() throws RemoteException;

}

[/java]

  1. Develop the remote object by implementing the remote interface.

[java]
import java.rmi.*;

import java.rmi.server.*;

import java.net.MalformedURLException;

public class HelloServerImpl extends UnicastRemoteObject implements
HelloInterface

{

public HelloServerImpl() throws RemoteException

{

System.out.println("Creating server object");

}

public String getMessage() throws RemoteException

{

return "HelloWorld";

}

public static void main(String[] helloworld)

{

try {

HelloServerImpl hsi = new HelloServerImpl();

Naming.rebind("rmi://localhost/ref", hsi);

System.out.println("ServerReady");

}

catch (RemoteException ex)

{

System.out.println("Error" + ex.getMessage());

}

catch (MalformedURLException ex) {

System.out.println("Error" + ex.getMessage());

}

}

}

[/java]

  1. Develop the client program.

[java]

import java.rmi.*;

import java.net.MalformedURLException;

public class HelloClient

{

public static void main(String[] helloClient)

{

try

{

HelloInterface server = (HelloInterface) Naming
.lookup("rmi://localhost/ref");

String msg = server.getMessage();

System.out.println(msg);

}

catch (RemoteException ex)

{

System.out.println("Error" + ex.getMessage());

} catch (MalformedURLException ex)

{

System.out.println("Error" + ex.getMessage());

}

catch (NotBoundException ex)

{

System.out.println("Error" + ex.getMessage());

}

}

}

[/java]

  1. Compile the Java source files.

javac HomeInterface.java

javac HelloServerImple.java

javac HelloClient.java

  1. Generate the client stubs and server skeletons.

rmic HelloServerImpl

  1. Start the RMI registry.

start rmiregistry

  1. Start the remote server objects.

java HelloServerImpl

  1. Run the client

open another cmd and run

java HelloClient

About the author

Mathan Lal

Add Comment

Click here to post a comment