Java Reference
In-Depth Information
Successive Update
The example code shows a simple client pull solution for the Personal Information Manager. Clients use the
server to centralize information about tasks they are working on. Each client stays up-to-date by periodically
requesting updates from the server.
In the sample code, the PullClient class retrieves a task for a client. Its responsibility is to locate the RMI server
so that it can request tasks on a regular basis.
Example A.236 PullClient.java
1. import java.net.MalformedURLException;
2. import java.rmi.Naming;
3. import java.rmi.NotBoundException;
4. import java.rmi.RemoteException;
5. import java.util.Date;
6. public class PullClient{
7. private static final String UPDATE_SERVER_SERVICE_NAME = "updateServer";
8. private static final String UPDATE_SERVER_MACHINE_NAME = "localhost";
9. private ClientPullServer updateServer;
10. private ClientPullRequester requester;
11. private Task updatedTask;
12. private String clientName;
13.
14. public PullClient(String newClientName){
15. clientName = newClientName;
16. try{
17. String url = "//" + UPDATE_SERVER_MACHINE_NAME + "/" + UPDATE_SERVER_SERVICE_NAME;
18. updateServer = (ClientPullServer)Naming.lookup(url);
19. }
20. catch (RemoteException exc){}
21. catch (NotBoundException exc){}
22. catch (MalformedURLException exc){}
23. catch (ClassCastException exc){}
24. }
25.
26. public void requestTask(String taskID){
27. requester = new ClientPullRequester(this, updateServer, taskID);
28. }
29.
30. public void updateTask(Task task){
31. requester.updateTask(task);
32. }
33.
34. public Task getUpdatedTask(){
35. return updatedTask;
36. }
37.
38. public void setUpdatedTask(Task task){
39. updatedTask = task;
40. System.out.println(clientName + ": received updated task: " + task);
41. }
42.
43. public String toString(){
44. return clientName;
45. }
46. }
When the client wants to receive updates on a task, it calls the method requestTask on the PullClient . The
PullClient object creates a worker thread (see “ Worker Thread ” on page 517), which is the
ClientPullRequester object. This object resides on the client, and regularly issues a request to the server for
updated task information.
Example A.237 ClientPullRequester.java
1. import java.rmi.RemoteException;
2. public class ClientPullRequester implements Runnable{
3. private static final int DEFAULT_POLLING_INTERVAL = 10000;
4. private Thread processingThread;
5. private PullClient parent;
6. private ClientPullServer updateServer;
7. private String taskID;
8. private boolean shutdown;
9. private Task currentTask = new TaskImpl();
 
Search WWH ::




Custom Search