Download presentation
Presentation is loading. Please wait.
Published byOscar Underwood Modified over 8 years ago
1
Java RMI
2
RMI Any object whose methods can be invoked from another Java VM is called a remote object.
3
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.
4
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
5
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.
6
RMI
7
You write the code for your distributed object. A tool (rmic) generates the needed stubs and skeletons.
8
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.
9
Example Import java.rmi.*; public interface PerfectTimeI extends Remote { long getPerfectTime() throws RemoteException; }`
10
Remote Object Implementation The server should have a class that –extends UnicastRemoteObject and –implements the remote interface
11
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 {… } }
12
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
13
Example System.setSecurityManager( new RMISecurityManager()); PerfectTime pt = new PerfectTime(); Naming.bind("//peppy:2005/PerfectTime", pt);
14
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.
15
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()); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.