Java Reference
In-Depth Information
In the
CallbackServerDelegate run
method, the object retrieves a project by calling the getProject method,
then sends it to a client with the
sendProjectToClient
method. The latter method represents the callback to the
client; the
CallbackServerDelegate
makes a call to an RMI object of type
CallbackClient
on the client
machine. The interface
CallbackClient
also defines a single RMI method,
receiveProject
.
Example 4.18
CallbackClient.java
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
3. public interface CallbackClient extends Remote{
4. public void receiveProject(Project project) throws RemoteException;
5. }
The implementer of
CallbackClient
,
CallbackClientImpl
, is both a client and a server. Its method
requestProject
looks up the
CallbackServer
and calls the remote method getProject. The class also defines
the remote method
receiveProject
, which is called by the server work thread when the project is ready for the
client.
CallbackClientImpl
has a boolean variable,
projectAvailable
, to allow a client program to determine
when the project is ready for display.
Example 4.19
CallbackClientImpl.java
1. import java.net.InetAddress;
2. import java.net.MalformedURLException;
3. import java.net.UnknownHostException;
4. import java.rmi.Naming;
5. import java.rmi.server.UnicastRemoteObject;
6. import java.rmi.NotBoundException;
7. import java.rmi.RemoteException;
8. public class CallbackClientImpl implements CallbackClient{
9. private static final String CALLBACK_CLIENT_SERVICE_NAME = "callbackClient";
10. private static final String CALLBACK_SERVER_SERVICE_NAME = "callbackServer";
11. private static final String CALLBACK_SERVER_MACHINE_NAME = "localhost";
12.
13. private Project requestedProject;
14. private boolean projectAvailable;
15.
16. public CallbackClientImpl(){
17. try {
18. UnicastRemoteObject.exportObject(this);
19. Naming.rebind(CALLBACK_CLIENT_SERVICE_NAME, this);
20. }
21. catch (Exception exc){
22. System.err.println("Error using RMI to register the CallbackClientImpl " + exc);
23. }
24. }
25.
26. public void receiveProject(Project project){
27. requestedProject = project;
28. projectAvailable = true;
29. }
30.
31. public void requestProject(String projectName){
32. try{
33. String url = "//" + CALLBACK_SERVER_MACHINE_NAME + "/" +
CALLBACK_SERVER_SERVICE_NAME;
34. Object remoteServer = Naming.lookup(url);
35. if (remoteServer instanceof CallbackServer){
36. ((CallbackServer)remoteServer).getProject(projectName,
37. InetAddress.getLocalHost().getHostName(),
38. CALLBACK_CLIENT_SERVICE_NAME);
39. }
40. projectAvailable = false;
41. }
42. catch (RemoteException exc){}
43. catch (NotBoundException exc){}
44. catch (MalformedURLException exc){}
45. catch (UnknownHostException exc){}
46. }
47.
48. public Project getProject(){ return requestedProject; }
49. public boolean isProjectAvailable(){ return projectAvailable; }
50. }
The basic sequence of action is as follows. When a client requires a project, the
CallbackClientImpl
object
calls the method
getProject
on the
CallbackServerImpl
object. The
CallbackServerImpl creates a