Rmi
Rmi
Rmi
Invocation
AnotherClass
returned object
computer 2
SampleServer remoteObject;
int s;
s = remoteObject.sum(1,2);
1,2
3
System.out.println(s);
The
The
Terminology
RMI Components
RMI registry
RMIRegistry
2. lookup
client
3. call
1. bind
server
Interfaces
Therefore,
In
In short,
The
Classes
an object is to be serialized:
The
Must be public
Must extend the interface java.rmi.Remote
Every method in the interface must declare that it
throws java.rmi.RemoteException (other
exceptions may also be thrown)
The
RMI Architecture
Remote Machine
bind
RMI Server
Registry
skeleton
return
call
stub
RMI Client
Local Machine
lookup
RMI Client
skeleton
call
RMI Server
return
Example
rmic RMILightBulbImpl
creates the classes:
RMILightBulbImpl_Stub.class
RMILightBulbImpl_Skeleton.class
Client stub
Server skeleton
import java.rmi.*;
import java.rmi.server.*;
public class LightBulbServer {
public static void main(String args[]) {
try {
RMILightBulbImpl bulbService = new RMILightBulbImpl();
RemoteRef location = bulbService.getRef();
System.out.println (location.remoteToString());
String registry = "localhost";
if (args.length >=1) {
registry = args[0];
}
String registration = "rmi://" + registry + "/RMILightBulb";
Naming.rebind( registration, bulbService );
} catch (Exception e) { System.err.println ("Error - " + e); } } }
import java.rmi.*;
public class LightBulbClient {
public static void main(String args[]) {
try { String registry = "localhost";
if (args.length >=1) { registry = args[0]; }
String registration = "rmi://" + registry + "/RMILightBulb";
Remote remoteService = Naming.lookup ( registration );
RMILightBulb bulbService = (RMILightBulb) remoteService;
bulbService.on();
System.out.println ("Bulb state : " + bulbService.isOn() );
System.out.println ("Invoking bulbservice.off()");
bulbService.off();
System.out.println ("Bulb state : " + bulbService.isOn() );
} catch (NotBoundException nbe) {
System.out.println ("No light bulb service available in registry!");
} catch (RemoteException re) { System.out.println ("RMI - " + re);
} catch (Exception e) { System.out.println ("Error - " + e); }
}
}
rmiregistry
Copy, if necessary
References
Java Network Programming and Distributed Computing, David Reilly & Michael
Reilly, Addison Wesley.
CS587x Remote Method Invocation, Ying Cai, Department of Computer Science,
Iowa State University.