Java Reference
In-Depth Information
26. return new Project(projectID, "Test project");
27. }
28.
29. private void sendProjectToClient(Project project){
30. try{
31. String url = "//" + callbackMachine + "/" + callbackObjectName;
32. Object remoteClient = Naming.lookup(url);
33. if (remoteClient instanceof CallbackClient){
34. ((CallbackClient)remoteClient).receiveProject(project);
35. }
36. }
37. catch (RemoteException exc){}
38. catch (NotBoundException exc){}
39. catch (MalformedURLException exc){}
40. }
41. }
In the CallbackServerDelegaterun method, the object retrieves a project by calling the getProject method,
then sends it to a client with the send-ProjectToClient 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 A.230 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 A.231 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,
Search WWH ::




Custom Search