Java Reference
In-Depth Information
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 231), which is the
ClientPullRequester object. This object resides on the client, and regularly issues a request to the server for
updated task information.
Example 4.21 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();
10. private int pollingInterval = DEFAULT_POLLING_INTERVAL;
11.
12. public ClientPullRequester(PullClient newParent, ClientPullServer newUpdateServer,
13. String newTaskID){
14. parent = newParent;
15. taskID = newTaskID;
16. updateServer = newUpdateServer;
17. processingThread = new Thread(this);
18. processingThread.start();
19. }
20.
21. public void run(){
22. while (!isShutdown()){
23. try{
24. currentTask = updateServer.getTask(taskID, currentTask. getLastEditDate());
25. parent.setUpdatedTask(currentTask);
26. }
27. catch (RemoteException exc){ }
28. catch (UpdateException exc){
29. System.out.println(" " + parent + ": " + exc.getMessage());
30. }
31. try {
32. Thread.sleep(pollingInterval);
33. }
34. catch (InterruptedException exc){ }
35. }
36. }
37.
38. public void updateTask(Task changedTask){
39. try{
Search WWH ::




Custom Search