Java Reference
In-Depth Information
Example A.226 RunPattern.java
1. import java.io.IOException;
2. public class RunPattern{
3. private static final String WORKER_SERVER_URL = "//localhost/ workerThread-
Server";
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);
55.
56. workQueue.put(firstAddr);
57. workQueue.put(firstContact);
58. workQueue.put(secondAddr);
59.
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());
Search WWH ::




Custom Search