Lecture 07 Remote Method Invocation in Java
Lecture 07 Remote Method Invocation in Java
Lecture 07
Ayesha Afzal
NFC-IET
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.
SampleServer
remoteObject;
int s;
… 1,2
public int sum(int a,int b)
s = {
remoteObject.sum(1,2); return a + b;
}
3
System.out.println(s);
The General RMI Architecture
Remote Machine
Registry
• The client lookup the server
skeleton
name in the registry to
establish remote references.
return call lookup
• The Stub serializing the
parameters to skeleton
stub
skeleton
Stub
RMI Client RMI Server
return
• The stub is responsible for sending the remote call over to the
server-side skeleton
/* SampleServer.java */
import java.rmi.*;
/* SampleServerImpl.java */
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
• The server must bind its name to the registry, the client will
look up the server name.
• Use java.rmi.Naming class to bind the server name to
registry. In this example the name call “SAMPLE-SERVER”.
Step 2: Develop the remote object and its interface
continued…
/* SampleServerImpl.java */
public static void main(String args[])
{
try
{
//create a local instance of the object
SampleServerImpl Server = new SampleServerImpl();
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
javac SampleServer.java
javac SampleServerImpl.java
rmic SampleServerImpl
javac SampleClient.java
• Copy the Stub and Interface class files to the client folder
Step 6: Start the RMI registry
• Before starting the server set the classpath variable to the path
where your server is located.
• Now run client and you will see it will calculate the sum from the
remote server object