Java Reference
In-Depth Information
Session
In this example, the client requester uses the server to perform a series of operations for updating contact
information in a shared address book. A user can perform four operations:
Add a contact
Add an address (associated with the current contact)
Remove an address (associated with the current contact)
Save the contact and address changes
These operations are defined in the class SessionClient .
Example A.205 SessionClient.java
1. import java.net.MalformedURLException;
2. import java.rmi.Naming;
3. import java.rmi.NotBoundException;
4. import java.rmi.RemoteException;
5. public class SessionClient{
6. private static final String SESSION_SERVER_SERVICE_NAME = "sessionServer";
7. private static final String SESSION_SERVER_MACHINE_NAME = "localhost";
8. private long sessionID;
9. private SessionServer sessionServer;
10.
11. public SessionClient(){
12. try{
13. String url = "//" + SESSION_SERVER_MACHINE_NAME + "/" +
SESSION_SERVER_SERVICE_NAME;
14. sessionServer = (SessionServer)Naming.lookup(url);
15. }
16. catch (RemoteException exc){}
17. catch (NotBoundException exc){}
18. catch (MalformedURLException exc){}
19. catch (ClassCastException exc){}
20. }
21.
22. public void addContact(Contact contact) throws SessionException{
23. try{
24. sessionID = sessionServer.addContact(contact, 0);
25. }
26. catch (RemoteException exc){}
27. }
28.
29. public void addAddress(Address address) throws SessionException{
30. try{
31. sessionServer.addAddress(address, sessionID);
32. }
33. catch (RemoteException exc){}
34. }
35.
36. public void removeAddress(Address address) throws SessionException{
37. try{
38. sessionServer.removeAddress(address, sessionID);
39. }
40. catch (RemoteException exc){}
41. }
42.
43. public void commitChanges() throws SessionException{
44. try{
45. sessionID = sessionServer.finalizeContact(sessionID);
46. }
47. catch (RemoteException exc){}
48. }
49. }
Each client method calls a corresponding method on the remote server. SessionServer defines the four methods
available to the clients through RMI.
Example A.206 SessionServer.java
 
Search WWH ::




Custom Search