Information Technology Reference
In-Depth Information
public interface IDateServer extends Remote {
public Date getDate() throws RemoteException;
}
Figure 2.12. RMI interface. RMI requires an interface to be
defined listing the methods that are available to remote clients.
public class DateServer implements IDateServer {
public DateServer() {
super();
}
public Date getDate() throws RemoteException {
return new Date();
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
IdateServer server = new DateServer();
IdateServer stub =
(IdateServer) UnicastRemoteObject.exportObject(server, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind( "DateServer" ,stub);
System.out.println( "Server Ready" );
} catch (RemoteException e) {
System.err.println( "DateServer exception:" );
e.printStackTrace();
}
}
}
Figure 2.13. An example of an RMI server. Code in the shaded
area implements the RMI framework.
To implement our server application we can either extend the
java.rmi.server.UnicastRemoteObject , if we would like the remote object to be
implicitly exported, or we can explicitly export the object using the exportObject
method of the same class.
Search WWH ::




Custom Search