Information Technology Reference
In-Depth Information
public class DateClient {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
Registry registry = LocateRegistry.getRegistry();
IDateServer dateServer =
(IDateServer) registry.lookup( "DateServer" );
System.out.println( "Date on server: " +
dateServer.getDate().toString());
} catch (RemoteException e) {
System.err.println( "DateServer Exception:" );
e.printStackTrace();
} catch (NotBoundException e) {
System.err.println( "Cannot bind to server" );
e.printStackTrace();
}
}
}
Figure 2.14. AnexampleofanRMIclient. Codeintheshaded
area implements the RMI framework.
In the example in Figure 2.13, our server explicitly exports a remote object,
which returns an object of type Date to the client. The date object is serialized
into a byte stream and passed to the client application, where it is deserialized
and accessed by the client, as shown by the client implementation in Figure 2.14:
As can be seen in our simple example, the RMI framework is highly
intrusive as it requires programmers to define an interface that extends the
java.rmi.Remote interface (Figure 2.12) and to implement the interface in the
server (Figure 2.13) code. The client code, illustrated in Figure 2.14, contains
RMI specific code to locate the server and execute the remote method. In addi-
tion, the client is required to be aware of the distributed nature of the application
byensuringthatitcatchesa RemoteException exception should one occur.
2.5.2
Summary
RMI provides a sophisticated environment for distributed computing. However,
as Hicks et al. [49] point out, programmers need to take special care to distinguish
between remote and local method invocation as the argument passing conven-
Search WWH ::




Custom Search