Java Reference
In-Depth Information
9. name = newName;
10. timeRequired = newTimeRequired;
11. }
12.
13. public String getName(){ return name; }
14. public ArrayList getProjectItems(){ return projectItems; }
15. public double getTimeRequired(){ return timeRequired; }
16.
17. public void setName(String newName){ name = newName; }
18. public void setTimeRequired(double newTimeRequired){ timeRequired = newTimeRequired; }
19.
20. public void addProjectItem(ProjectItem element){
21. if (!projectItems.contains(element)){
22. projectItems.add(element);
23. }
24. }
25.
26. public void removeProjectItem(ProjectItem element){
27. projectItems.remove(element);
28. }
29.
30. }
RunPattern creates a demonstration RMI client and server object. In the example, the main program thread uses
the CallbackClientImpl object to request a project from the server, then enters a wait loop until the project is
returned.
Example A.235 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 Callback pattern");
5. System.out.println("This code will run two RMI objects to demonstrate");
6. System.out.println(" callback capability. One will be CallbackClientImpl,");
7. System.out.println(" which will request a project from the other remote");
8. System.out.println(" object, CallbackServerImpl.");
9. System.out.println("To demonstrate how the Callback pattern allows the");
10. System.out.println(" client to perform independent processing, the main");
11. System.out.println(" progam thread will go into a wait loop until the");
12. System.out.println(" server sends the object to its client.");
13. System.out.println();
14.
15. System.out.println("Running the RMI compiler (rmic)");
16. System.out.println();
17. try {
18. Process p1 = Runtime.getRuntime().exec("rmic CallbackServerImpl");
19. Process p2 = Runtime.getRuntime().exec("rmic CallbackClientImpl");
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. System.out.println("Starting the rmiregistry");
32. System.out.println();
33. Process rmiProcess = null;
34. try{
35. rmiProcess = Runtime.getRuntime().exec("rmiregistry");
36. Thread.sleep(15000);
37. }
38. catch (IOException exc){
39. System.err.println("Unable to start the rmiregistry. Exiting application.");
40. System.exit(1);
41. }
42. catch (InterruptedException exc){
43. System.err.println("Threading problems encountered when starting the
rmiregistry.");
44. }
45.
46. System.out.println("Creating the client and server objects");
Search WWH ::




Custom Search