Finally, the client is implemented. As shown in Code 7. The client completes the following tasks:
n Create and initialize ORB.
n Use General Name Service Corband's URL Analysis Addservant. Use this URL to find the name service running on this machine, and then run on port 2900 and listen to the request. After finding the name service, it will find the object Addserver from the name service.
n Call the ADDARRAYS method of the object and output the result. In this example, the client is called once every 6 seconds. If the server is stopped before the next call request is called, the server will automatically restart because the permanent life policy client will automatically restart.
Code Example 7: AddClient3.java
Import arithapp. *;
Import org.omg.corba.orb;
Import org.omg.corba.obj_adapter;
Import Org.omg.cosnaming.namingContext;
Import Org.omg.cosnaming.namingContextHelper;
Import org.omg.cosnaming.namecomponent;
Import Org.omg.PortableServer.poA;
Public class addclient3 {
Public static void main (string args []) {
Try {
// Create and Initialize the ORB
ORB ORB = Orb.init (args, null);
Org.omg.corba.object obj = orb.string_to_Object (
"CORBANAME :: Localhost: 2900 # addserver");
Add Impl = addHelper.narrow (OBJ);
// the arrays to beadeded
INT A [] = {6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
INT B [] = {7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
// The result will be saved in this new arch
Arithapp.addpackage.ArrayHolder C =
New arithapp.addpackage.ArrayHolder ();
While (true) {
System.out.println ("Calling
The persistent addserver3 .. ");
Impl.addarrays (A, B, C);
//print the new arch
System.out.println ("The Sum of the Two Arrays IS:");
For (int i = 0; I System.out.println (C.Value [i]); } System.out.println ("... WILL Call The Server Again in a Few Seconds .... "); System.out.println ("... if The Server is down, it will be automatically restarted ... "); Thread.sleep (6000); } } catch (exception e) { System.err.Println ("Exception in AddClient3 ..." E); E.PrintStackTrace (); } } } You can now compile Addservant, Addserver3, AddClient3 classes, as well as the framework files generated by the IDLJ compiler. You can compile using the Javac compiler: Prompt> javac * .java arithapp / *. Java Run the program to follow these steps: 1. Start ORBD Name Service: Prompt> Orbd -orbinitialPort 2900 The above command indicates that you want the ORBD service to run on a 2900 port. Need to be specified by the command line parameters -orbinitialport. 2. Start the Addserver3 server. To register a permanent service in ORBD, you need to use the command serverTool to start the server, ServerTool is a command line program for developers register, cancel, launch, and close permanent service. SERVERTOOL can be used with the following command (Note Be sure to start on the port number used by the ORBD service to run it correctly): Prompt> ServerTool -orbinitialPort 2900 You can see the command line interface of ServerTool as follows: ServerTool> In this example we make ORBD and ServerTool on the same host. If you want to run ServerTool on another host, you need to use the command line argument -orbinitialhost to indicate the host running the ORBD service. You can now register the Addserver3 service using the register command. You need to specify the service name, the program name, and the path to the implementation code store. as the picture shows: As you can see, ServerTool registers the service and assigns a unique ID number (this example is 257), which can be used for future internal service management. If you register an existing service, the ID number will return 0. Typing the Help command can see a list of all commands in ServerTool. as the picture shows. 3. Start the AddClient3 client: Prompt> Java AddClient3 -orbinitialPort 2900 You can see that the client outputs the additional sum of the two arrays. In order to display the characteristics of the permanent server, we now turn off the service. as the picture shows. First we list all the current registered services, find the ID number of the service we have written, and then use the shutdown command to close it. Even if the service has been closed, you can still observe that the client console adds a result every 6 seconds. This is because the service is a permanent service, so even if the server is stopped, the service will be automatically restarted when the customer request is requested. Of course, these are transparent to the client. We can also use the listactive command to list the current activity list: ORBD saves the server and all information about them. When you run the ORBD service, it creates a subdirectory in its launch directory. The name of the subdirectory is orb.db by default. The subdirectory contains information on the registered service and log files. If you look at the file under the subdirectorial orb.db / logs, you will find a file such as 257.out and 257.rr. These files record the launch and shutdown time of the service, and all error messages that occur. In this example, the subdirectory is under C: / AirThmetic. summary The above example introduces how to develop CORBA applications using newly defined POA, and how to develop temporary and permanent server. If you have ready-made CORBA programs written in BOA, then you need to rewrite it into the use of Poa, then your program can be used between ORBs provided by different developers. references Corba Programming with J2se 1.4, Qusay H. Mahmoud, May 2002, Developers.sun.com