150 likes | 325 Views
Java RMI. RMI. Any object whose methods can be invoked from another Java VM is called a remote object. RMI. How can a client call a remote object? The client calls a local object. This local object is called a stub , which is a client-side proxy object.
E N D
RMI • Any object whose methods can be invoked from another Java VM is called a remote object.
RMI • How can a client call a remote object? • The client calls a local object. This local object is called a stub, which is a client-side proxy object. • It has the same interface as the remote object. • Also it knows how to call over the network using sockets.
RMI • The stub calls over the network to a skeleton, which is a server-side proxy object. • The skeleton masks network communication form the distributed object. The skeleton knows how to receive calls on a socket
RMI • The skeleton delegates the call to the remote object . The remote object does its works, and then returns control to the skeleton, which returns to the stub, which then returns to the client. • Both the stub and the remote object implement the same interface, called the remote interface.
RMI • You write the code for your distributed object. • A tool (rmic) generates the needed stubs and skeletons.
Remote Interface • It should extend the interface java.rmi.Remote • Remote objects must be passed as arguments or return values as (upcast) remote interface references.
Example Import java.rmi.*; public interface PerfectTimeI extends Remote { long getPerfectTime() throws RemoteException; }`
Remote Object Implementation • The server should have a class that • extends UnicastRemoteObject and • implements the remote interface
Example import java.rmi.*; import java.rmi.server.*; import java.net.*; public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI { public long getPerfectTime() throws RemoteException { …. } // Must implement constructor to throw RemoteException: public PerfectTime() throws RemoteException {… } }
Setting up the Server • Create and install a security manager • Create one or more instances of a remote object. • Register at least one of the remote objects with the RMI remote object registry • Before running the program, you should run rmiregistry
Example System.setSecurityManager( new RMISecurityManager()); PerfectTime pt = new PerfectTime(); Naming.bind("//peppy:2005/PerfectTime", pt);
rmic • You should run rmic to generate the stub and skeleton. • The stub and skeleton are responsible for marshalling and unmarshalling the arguments and return values passed over the network.
Client Code import java.rmi.*; import java.rmi.registry.*; public class DisplayPerfectTime { public static void main(String[] args) throws Exception { System.setSecurityManager( new RMISecurityManager()); PerfectTimeI t = (PerfectTimeI)Naming.lookup( "//peppy:2005/PerfectTime"); t.getPerfectTime()); }