Java Reference
In-Depth Information
40. updateServer.updateTask(taskID, changedTask);
41. }
42. catch (RemoteException exc){ }
43. catch (UpdateException exc){
44. System.out.println(" " + parent + ": " + exc.getMessage());
45. }
46. }
47.
48. public int getPollingInterval(){ return pollingInterval; }
49. public boolean isShutdown(){ return shutdown; }
50.
51. public void setPollingInterval(int newPollingInterval){ pollingInterval =
newPollingInterval; }
52. public void setShutdown(boolean isShutdown){ shutdown = isShutdown; }
53. }
The RMI server's behavior is defined by the ClientPullServer interface and managed by the
ClientPullServerImpl class. Two methods allow clients to interact with a server, getTask and updateTask .
Example 4.22 ClientPullServer.java
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
3. import java.util.Date;
4. public interface ClientPullServer extends Remote{
5. public Task getTask(String taskID, Date lastUpdate) throws RemoteException, UpdateException;
6. public void updateTask(String taskID, Task updatedTask) throws RemoteException,
UpdateException;
7. }
Example 4.23 ClientPullServerImpl.java
1. import java.util.Date;
2. import java.rmi.Naming;
3. import java.rmi.server.UnicastRemoteObject;
4. public class ClientPullServerImpl implements ClientPullServer{
5. private static final String UPDATE_SERVER_SERVICE_NAME = "updateServer";
6. public ClientPullServerImpl(){
7. try {
8. UnicastRemoteObject.exportObject(this);
9. Naming.rebind(UPDATE_SERVER_SERVICE_NAME, this);
10. }
11. catch (Exception exc){
12. System.err.println("Error using RMI to register the ClientPullServerImpl " + exc);
13. }
14. }
15.
16. public Task getTask(String taskID, Date lastUpdate) throws UpdateException{
17. return UpdateServerDelegate.getTask(taskID, lastUpdate);
18. }
19.
20. public void updateTask(String taskID, Task updatedTask) throws UpdateException{
21. UpdateServerDelegate.updateTask(taskID, updatedTask);
22. }
23. }
The class UpdateServerDelegate performs the server-side behavior for ClientPullServerImpl . Specifically, it
retrieves Task objects, and ensures that up-to-date copies of Tasks are provided to clients by comparing the last
update Date .
Example 4.24 UpdateServerDelegate.java
1. import java.util.Date;
2. import java.util.HashMap;
3. public class UpdateServerDelegate{
4. private static HashMap tasks = new HashMap();
5.
6. public static Task getTask(String taskID, Date lastUpdate) throws UpdateException{
7. if (tasks.containsKey(taskID)){
8. Task storedTask = (Task)tasks.get(taskID);
9. if (storedTask.getLastEditDate().after(lastUpdate)){
10. return storedTask;
11. }
12. else {
13. throw new UpdateException("Task " + taskID + " does not need to be updated",
UpdateException.TASK_UNCHANGED);
14. }
Search WWH ::




Custom Search