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:
- Define the remote interface
- Develop the remote object by implementing the remote interface.
- Develop the client program.
- Compile the Java source files.
- Generate the client stubs and server skeletons.
- Start the RMI registry.
- Start the remote server objects.
- Run the client
Example of rmi application:
- Define the remote interface
Code:
[java]
import java.rmi.*;
public interface HelloInterface extends Remote{
public String getMessage() throws RemoteException;
}
[/java]
- 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]
- 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]
- Compile the Java source files.
javac HomeInterface.java
javac HelloServerImple.java
javac HelloClient.java
- Generate the client stubs and server skeletons.
rmic HelloServerImpl
- Start the RMI registry.
start rmiregistry
- Start the remote server objects.
java HelloServerImpl
- Run the client
open another cmd and run
java HelloClient
Add Comment