Java Reference
In-Depth Information
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 4.7 SessionServer.java
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
3. public interface SessionServer extends Remote{
4. public long addContact(Contact contact, long sessionID) throws RemoteException,
SessionException;
5. public long addAddress(Address address, long sessionID) throws RemoteException,
SessionException;
6. public long removeAddress(Address address, long sessionID) throws RemoteException,
SessionException;
7. public long finalizeContact(long sessionID) throws RemoteException, SessionException;
8. }
SessionServerImpl implements the SessionServer interface, providing an RMI server. It delegates business
behavior to the class SessionServerDelegate .
Example 4.8 SessionServerImpl.java
1. import java.rmi.Naming;
2. import java.rmi.server.UnicastRemoteObject;
3. public class SessionServerImpl implements SessionServer{
4. private static final String SESSION_SERVER_SERVICE_NAME = "sessionServer";
5. public SessionServerImpl(){
6. try {
7. UnicastRemoteObject.exportObject(this);
Search WWH ::




Custom Search