Java Reference
In-Depth Information
Example A.253 RunPattern.java
1. import java.io.IOException;
2. public class RunPattern{
3. public static void main(String [] arguments){
4. System.out.println("Example for the Router pattern");
5. System.out.println("This code same will create a series of GUIs, and use");
6. System.out.println(" the Router pattern to map message notifications between");
7. System.out.println(" them. In this code example, the Router will send messages");
8. System.out.println(" between the GUI clients based on the following mapping:");
9. System.out.println();
10. System.out.println("\tGUI # 1:\tGUI #2\tGUI #3");
11. System.out.println("\tGUI # 2:\tGUI #1\tGUI #4");
12. System.out.println("\tGUI # 3:\tGUI #1\tGUI #4");
13. System.out.println("\tGUI # 4:\tGUI #1\tGUI #2\tGUI #3\tGUI #4");
14. System.out.println();
15.
16. System.out.println("Running the RMI compiler (rmic)");
17. try{
18. Process p1 = Runtime.getRuntime().exec("rmic Router");
19. Process p2 = Runtime.getRuntime().exec("rmic RouterClient");
20. p1.waitFor();
21. p2.waitFor();
22. }
23. catch (IOException exc){
24. System.err.println("Unable to run rmic utility. Exiting application.");
25. System.exit(1);
26. }
27. catch (InterruptedException exc){
28. System.err.println("Threading problems encountered while using the rmic
utility.");
29. }
30.
31.
32. System.out.println("Starting the rmiregistry");
33. System.out.println();
34. Process rmiProcess = null;
35. try{
36. rmiProcess = Runtime.getRuntime().exec("rmiregistry");
37. Thread.sleep(15000);
38. }
39. catch (IOException exc){
40. System.err.println("Unable to start the rmiregistry. Exiting application.");
41. System.exit(1);
42. }
43. catch (InterruptedException exc){
44. System.err.println("Threading problems encountered when starting the
rmiregistry.");
45. }
46.
47. System.out.println("Creating the Router object");
48. System.out.println();
49. Router mainRouter = new Router();
50.
51. InputKey keyOne = new InputKey();
52. InputKey keyTwo = new InputKey();
53. InputKey keyThree = new InputKey();
54. InputKey keyFour = new InputKey();
55.
56. System.out.println("Creating the four RouterGui objects");
57. System.out.println();
58. RouterGui first = new RouterGui(keyOne);
59. RouterGui second = new RouterGui(keyTwo);
60. RouterGui third = new RouterGui(keyThree);
61. RouterGui fourth = new RouterGui(keyFour);
62.
63. System.out.println("Creating GUI OutputChannel lists for the Router");
64. System.out.println();
65. OutputChannel [] subscriptionListOne =
{ second.getOutputChannel(),third.getOutputChannel() };
66. OutputChannel [] subscriptionListTwo =
{ first.getOutputChannel(),fourth.getOutputChannel() };
67. OutputChannel [] subscriptionListThree = { first.getOutputChannel(),
second.getOutputChannel(),
68. third.getOutputChannel(),fourth.getOutputChannel() };
69.
70. mainRouter.addRoute(keyOne, subscriptionListOne);
71. mainRouter.addRoute(keyTwo, subscriptionListTwo);
Search WWH ::




Custom Search