Java Reference
In-Depth Information
Callback
In the Personal Information Manager, one of the items that can vary most in size is a project. A project might
consist of only a few tasks, or it could be made up of hundreds or even thousands of individual work steps. This
example demonstrates how the Callback pattern could be used to retrieve a project object stored on a server
machine.
The interface CallbackServer defines a single server-side method, getProject . Note that the method requires
callback information—the client machine name and the name of the RMI client object—in addition to the project
ID. The class CallbackServerImpl implements this interface.
Example A.227 CallbackServer.java
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
3. public interface CallbackServer extends Remote{
4. public void getProject(String projectID, String callbackMachine,
5. String callbackObjectName) throws RemoteException;
6. }
Example A.228 CallbackServerImpl.java
1. import java.rmi.Naming;
2. import java.rmi.server.UnicastRemoteObject;
3. public class CallbackServerImpl implements CallbackServer{
4. private static final String CALLBACK_SERVER_SERVICE_NAME = "callbackServer";
5. public CallbackServerImpl(){
6. try {
7. UnicastRemoteObject.exportObject(this);
8. Naming.rebind(CALLBACK_SERVER_SERVICE_NAME, this);
9. }
10. catch (Exception exc){
11. System.err.println("Error using RMI to register the CallbackServerImpl " + exc);
12. }
13. }
14.
15. public void getProject(String projectID, String callbackMachine,
16. String callbackObjectName){
17. new CallbackServerWorkThread(projectID, callbackMachine, callbackObjectName);
18. }
19.
20. }
In the getProject method, CallbackServerImpl delegates the task of retrieving the project to a worker object,
CallbackServerDelegate . This object runs on its own thread and does the work of retrieving a project and
sending it to a client.
Example A.229 CallbackServerDelegate.java
1. import java.net.MalformedURLException;
2. import java.rmi.Naming;
3. import java.rmi.NotBoundException;
4. import java.rmi.RemoteException;
5. public class CallbackServerDelegate implements Runnable{
6. private Thread processingThread;
7. private String projectID;
8. private String callbackMachine;
9. private String callbackObjectName;
10.
11. public CallbackServerDelegate(String newProjectID, String newCallbackMachine,
12. String newCallbackObjectName){
13. projectID = newProjectID;
14. callbackMachine = newCallbackMachine;
15. callbackObjectName = newCallbackObjectName;
16. processingThread = new Thread(this);
17. processingThread.start();
18. }
19.
20. public void run(){
21. Project result = getProject();
22. sendProjectToClient(result);
23. }
24.
25. private Project getProject(){
 
Search WWH ::




Custom Search