Java Remote Object Invocation (RMI)
Java Remote Object Invocation (RMI)
Java Remote Object Invocation (RMI)
• Overview of RMI
• Java RMI allowed programmer to execute remote function
class using the same semantics as local functions calls.
Local Machine (Client) Remote Machine (Server)
SampleServer remoteObject;
int s;
…
s = remoteObject.sum(1,2);
1,2
public int sum(int a,int b)
{
3 }
return a + b;
System.out.println(s);
The General RMI Architecture
Remote Machine
• The server must first bind
bind
its name to the registry RMI Server
skeleton
Stub
RMI Client RMI Server
return
/* SampleServer.java */
import java.rmi.*;
/* SampleServerImpl.java */
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
System.out.println("Server waiting.....");
}
catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString()); }
catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString()); }
}
Step 3: Develop the client program
• In order for the client object to invoke methods on the
server, it must first look up the name of server in the
registry. You use the java.rmi.Naming class to
lookup the server name.
• The server name is specified as URL in the from
( rmi://host:port/name )
• Default RMI port is 1099.
• The name specified in the URL must exactly match the
name that the server has bound to the registry. In this
example, the name is “SAMPLE-SERVER”
• The remote method invocation is programmed using the
remote interface name (remoteObject) as prefix and
the remote method name (sum) as suffix.
Step 3: Develop the client program
import java.rmi.*;
import java.rmi.server.*;
public class SampleClient
{
public static void main(String[] args)
{
// set the security manager for the client
System.setSecurityManager(new RMISecurityManager());
//get the remote object from the registry
try
{
System.out.println("Security Manager loaded");
String url = "//localhost/SAMPLE-SERVER";
SampleServer remoteObject = (SampleServer)Naming.lookup(url);
System.out.println("Got remote object");
System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) );
}
catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString()); }
}
}
Step 4 & 5: Compile the Java source files &
Generate the client stubs and server skeletons
• Once the interface is completed, you need to generate
stubs and skeleton code. The RMI system provides an RMI
compiler (rmic) that takes your generated interface class
and procedures stub code on its self.
~/rmi> rmiregistry