Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
198
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

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;

339

3.public class ServerDataStoreImpl implements ServerDataStore{

4.private static final String WORKER_SERVER_SERVICE_NAME = "workerThreadServer";

6.public ServerDataStoreImpl(){

7.try {

8.

UnicastRemoteObject.exportObject(this);

9.

Naming.rebind(WORKER_SERVER_SERVICE_NAME, this);

10.}

11.catch (Exception exc){

12.System.err.println("Error using RMI to register the ServerDataStoreImpl " + exc);

13.}

14.}

15.

16.public Address retrieveAddress(long addressID){

17.if (addressID == 5280L){

18. return new AddressImpl("Fine Dining", "416 Chartres St.", "New Orleans", "LA", "51720");

19.}

20.else if (addressID == 2010L){

21. return new AddressImpl("Mystic Yacht Club", "19 Imaginary Lane", "Mystic", "CT", "46802");

22.}

23.else{

24. return new AddressImpl();

25.}

26.}

27.public Contact retrieveContact(long contactID){

28.if (contactID == 5280L){

29.return new ContactImpl("Dwayne", "Dibley", "Accountant", "Virtucon");

30.}

31.else{

32. return new ContactImpl();

33.}

34.}

36.}

The Address and Contact interfaces define the business objects used in this example, and the implementers AddressImpl and ContactImpl provide underlying functional behavior.

Example A.222 Address.java

1.import java.io.Serializable;

2.public interface Address extends Serializable{

3.public static final String EOL_STRING = System.getProperty("line.separator");

4.public static final String SPACE = " ";

5.public static final String COMMA = ",";

6.public String getType();

7.public String getDescription();

8.public String getStreet();

9.public String getCity();

10.public String getState();

11.public String getZipCode();

12.

13.public void setType(String newType);

14.public void setDescription(String newDescription);

15.public void setStreet(String newStreet);

16.public void setCity(String newCity);

17.public void setState(String newState);

18.public void setZipCode(String newZip);

19.}

Example A.223 AddressImpl.java

1.public class AddressImpl implements Address{

2.private String type;

3.private String description;

4.private String street;

5.private String city;

6.private String state;

7.private String zipCode;

8.

9.public AddressImpl(){ }

10.public AddressImpl(String newDescription, String newStreet,

11.String newCity, String newState, String newZipCode){

12.description = newDescription;

13.street = newStreet;

14.city = newCity;

340

15.state = newState;

16.zipCode = newZipCode;

17.}

18.

19.public String getType(){ return type; }

20.public String getDescription(){ return description; }

21.public String getStreet(){ return street; }

22.public String getCity(){ return city; }

23.public String getState(){ return state; }

24.public String getZipCode(){ return zipCode; }

25.

26.public void setType(String newType){ type = newType; }

27.public void setDescription(String newDescription){ description = newDescription; }

28.public void setStreet(String newStreet){ street = newStreet; }

29.public void setCity(String newCity){ city = newCity; }

30.public void setState(String newState){ state = newState; }

31.public void setZipCode(String newZip){ zipCode = newZip; }

32.

33.public String toString(){

34.return street + EOL_STRING + city + COMMA + SPACE +

35. state + SPACE + zipCode + EOL_STRING;

36.}

37.}

Example A.224 Contact.java

1.import java.io.Serializable;

2.public interface Contact extends Serializable{

3.public static final String EOL_STRING = System.getProperty("line.separator");

4.public static final String SPACE = " ";

5.public String getFirstName();

6.public String getLastName();

7.public String getTitle();

8.public String getOrganization();

9.

public void setFirstName(String newFirstName);

10.

11.

public void setLastName(String newLastName);

12.

public void setTitle(String newTitle);

13.

public void setOrganization(String newOrganization);

14.

}

 

 

Y

 

 

 

 

Example A.225 ContactImpl.java

 

 

L

 

 

F

1.

 

 

 

public class ContactImpl implements Contact{

2.

private String firstName;

 

 

M

3.

private String lastName;

 

A

4.

private String title;

E

 

5.

private String organization;T

 

 

6.

 

 

 

 

7.public ContactImpl(){}

8.public ContactImpl(String newFirstName, String newLastName,

9.String newTitle, String newOrganization){

10. firstName = newFirstName;

11. lastName = newLastName;

12. title = newTitle;

13. organization = newOrganization;

14. }

15.

16.public String getFirstName(){ return firstName; }

17.public String getLastName(){ return lastName; }

18.public String getTitle(){ return title; }

19.public String getOrganization(){ return organization; }

21.public void setFirstName(String newFirstName){ firstName = newFirstName; }

22.public void setLastName(String newLastName){ lastName = newLastName; }

23.public void setTitle(String newTitle){ title = newTitle; }

24.public void setOrganization(String newOrganization){ organization = newOrganization; }

26.public String toString(){

27.return firstName + SPACE + lastName + EOL_STRING;

28.}

29.}

RunPattern creates a ConcreteQueue, then uses it to retrieve a sample Contact and two Addresses. The worker

thread in the queue processes these requests as

queue. The ConcreteQueue and its

associated worker thread can be used throughout

client application, performing any background

TEAM FLY PRESENTS

task required by the client as RunnableTask objects are added to its queue.

341

Example A.226 RunPattern.java

1.import java.io.IOException;

2.public class RunPattern{

3.private static final String WORKER_SERVER_URL = "//localhost/ workerThreadServer";

4.public static void main(String [] arguments){

5.System.out.println("Example for the WorkerThread pattern");

6.System.out.println("In this example, a ConcreteQueue object which uses a");

7.System.out.println(" worker thread, will retrieve a number of objects from");

8.System.out.println(" the server.");

9.System.out.println();

10.

11.System.out.println("Running the RMI compiler (rmic)");

12.System.out.println();

13.try{

14. Process p1 = Runtime.getRuntime().exec("rmic ServerDataStoreImpl");

15. p1.waitFor();

16.}

17.catch (IOException exc){

18.System.err.println("Unable to run rmic utility. Exiting application.");

19. System.exit(1);

20.}

21.catch (InterruptedException exc){

22.System.err.println("Threading problems encountered while using the rmic utility.");

23.}

24.

25.System.out.println("Starting the rmiregistry");

26.System.out.println();

27.Process rmiProcess = null;

28.try{

29. rmiProcess = Runtime.getRuntime().exec("rmiregistry");

30. Thread.sleep(15000);

31.}

32.catch (IOException exc){

33.System.err.println("Unable to start the rmiregistry. Exiting applica-

tion.");

34. System.exit(1);

35.}

36.catch (InterruptedException exc){

37.System.err.println("Threading problems encountered when starting the rmiregistry.");

38.}

39.

40.System.out.println("Creating the queue, which will be managed by the worker thread");

41.System.out.println();

42.ConcreteQueue workQueue = new ConcreteQueue();

43.

44.System.out.println("Creating the RMI server object, ServerDataStoreImpl");

45.System.out.println();

46.ServerDataStore server = new ServerDataStoreImpl();

47.

48.System.out.println("Creating AddressRetrievers and ContactRetreivers.");

49.System.out.println(" These will placed in the queue, as tasks to be");

50.System.out.println(" performed by the worker thread.");

51.System.out.println();

52.AddressRetriever firstAddr = new AddressRetriever(5280L, WORKER_SERVER_URL);

53.AddressRetriever secondAddr = new AddressRetriever(2010L, WORKER_SERVER_URL);

54.ContactRetriever firstContact = new ContactRetriever(5280L, WORKER_SERVER_URL);

56.workQueue.put(firstAddr);

57.workQueue.put(firstContact);

58.workQueue.put(secondAddr);

60.while (!secondAddr.isAddressAvailable()){

61.

try{

62.

Thread.sleep(1000);

63.

}

64.

catch (InterruptedException exc){}

65.

}

66.

 

67.System.out.println("WorkerThread completed the processing of its Tasks");

68.System.out.println("Printing out the retrieved objects now:");

69.System.out.println();

70.System.out.println(firstAddr.getAddress());

342

71.System.out.println(firstContact.getContact());

72.System.out.println(secondAddr.getAddress());

74. }

75.

76.}

343