Java Reference
In-Depth Information
46. }
Two classes, AddressRetriever and ContactRetriever , implement the RunnableTask interface in this
example. The classes are very similar; both use RMI to request that a business object be retrieved from a server.
As their names suggest, each class retrieves a specific kind of business object, making Address and Contact
objects from the server available to clients.
Example A.218 AddressRetriever.java
1. import java.rmi.Naming;
2. import java.rmi.RemoteException;
3. public class AddressRetriever implements RunnableTask{
4. private Address address;
5. private long addressID;
6. private String url;
7.
8. public AddressRetriever(long newAddressID, String newUrl){
9. addressID = newAddressID;
10. url = newUrl;
11. }
12.
13. public void execute(){
14. try{
15. ServerDataStore dataStore = (ServerDataStore)Naming.lookup(url);
16. address = dataStore.retrieveAddress(addressID);
17. }
18. catch (Exception exc){
19. }
20. }
21.
22. public Address getAddress(){ return address; }
23. public boolean isAddressAvailable(){ return (address == null) ? false : true; }
24. }
Example A.219 ContractRetriever.java
1. import java.rmi.Naming;
2. import java.rmi.RemoteException;
3. public class ContactRetriever implements RunnableTask{
4. private Contact contact;
5. private long contactID;
6. private String url;
7.
8. public ContactRetriever(long newContactID, String newUrl){
9. contactID = newContactID;
10. url = newUrl;
11. }
12.
13. public void execute(){
14. try{
15. ServerDataStore dataStore = (ServerDataStore)Naming.lookup(url);
16. contact = dataStore.retrieveContact(contactID);
17. }
18. catch (Exception exc){
19. }
20. }
21.
22. public Contact getContact(){ return contact; }
23. public boolean isContactAvailable(){ return (contact == null) ? false : true; }
24. }
The RMI server in this example is defined by the ServerDataStore interface and its implementer,
ServerDataStoreImpl .
Example A.220 ServerDataStore.java
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
3. public interface ServerDataStore extends Remote{
4. public Address retrieveAddress(long addressID) throws RemoteException;
5. public Contact retrieveContact(long contactID) throws RemoteException;
6. }
Example A.221 ServerDataStoreImpl.java
1. import java.rmi.Naming;
2. import java.rmi.server.UnicastRemoteObject;
Search WWH ::




Custom Search