A simple CORBAJava example

zhaozj2021-02-08  376

In June, we talked

Why do you want to use CORBA and Java technology. This month, I want to pass a simple example of a available examples, let you start exploring the field of CORBA technology. However, don't forget that our goal is to create such a distributed application: allowing clients residing on a computer to issue a request to the service running on another computer. We don't want to worry about details such as hardware or operating system software, but just want this service to respond to client requests.

The Idl Interface All CORBA Structures Starting from an interface, the best way to understand the interface is to imagine my car, right, my car. Although you are not familiar with it, if I said to you: "On my car, bring some sandwiches to come back", I am afraid you will not suspect that you can drive my car. You may want to know where it stops and open it is safe, but you will be sure that my car is not very different. This is because, in various cars, the interface between people and cars has been highly standardized. There may be some differences between my cars and your sports cars, but the installation of the car's accelerator pedal, brakes and steering wheel is standard, you will be easily and fast. Because CORBA is independent of language, it relies on how the client issues a request to implement the service to implement the interface. Our interface is a method: add (). This method will take two numbers (two IDL LONG models) and return these two numbers. Here is our interface computing program: Listing 1. Calcsimpl.idl

Module Corbasem {

Module Gen {

Module CalcsImpl {

Interface Calculator {

Long add (in long x, in long y);

}

}

}

}; The IDL keyword in this interface is: Module, Interface, long, and in. IDL uses keyword module to create a namespace, and this keyword is accurately mapped to Java Keyword Package. When running the IDL-To-Java compiler, the generated Java file will save to the subdirectory called CalcsImpl. The IDL keyword interface is perfectly mapped to Java interface and represents an abstract type because both only define the way you communicate with objects without involving the implementation of objects. IDL keyword long is a basic integer type, which maps at least 4 bytes, which is int for the Java code. If you want to perform a mechanism to call the remote method, you will find how significance in the direction of defining parameters (clients to servers, servers or bidirectional delivery). In the IDL operation, these directions are declared with IN, OUT, and INOUT, each parameter must declare the direction in order to enable the Object Request Agent (ORB) to know the direction of the parameter. This affects parameter packaging, parameter unpacking, and memory management for sending parameters. The more the orb has more, the higher it is efficient. Keyword IN indicates that long x and long y are passed from the client to the server.

Figure 1. Participation in the various parts of the CORBA request

IDL compiler

Need an IDL compiler? You may already have an ORB vendor and IDL-to-Java compiler. But if you haven't, where did you get it? There are many here, and some can be downloaded for free. I recommend Obect Oriented Concepts, INC. ORBACUS ORB. If you don't use it for commercial purposes, it can also be downloaded for free, and it is fully compliant with CORBA 2.3 specification. Another can try 60 days of compilers are the Visibroker of Inprise, and it is also fully compliant with CORBA 2.3 specification and downloadable. For these two products, please refer to the reference. After the interface definition, you must run on the IDL-to-Java compiler provided by the ORB vendor. The IDL compiler is a delicate utility that generates IDL's Stub and Skeleton and other support files. These source files generated, most of which will enhance the package functionality of the specific IDL type defined in the CORBA standard. The compiler will generate most of the network detection (Plumbing), which is very important in distributed systems. In the most basic level, the IDL-TO-Java compiler is just a definition of CORBA 2.3 specification to implement programs from IDL to Java language mapping. Manual generation These code is boring and time, it is easy to make an error; the IDL-to-java compiler handles this, so you don't have to worry about it; The IDL-to-Java compiler will impart the CORBA-LAND rules to your system. Enter the following command to execute the IDL-TO-JAVA compiler from ORBACUS, put all generated files in the output directory of ClassPath. Listing 2. Call IDL-to-Java compiler

JIDL --OUTPUT-DIR C: / _ WORK / CORBASEM CALCULATOR.IDL generated? This command generates all Java source files required to build implementations. The IDL-to-Java compiler ensures that the defined interface complies with the rules of the CORBA specification.

Figure 2. Idl-to-java compiler file generation

Here are these files:

Calculator.java - This file is called the interface file. The CORBA specification points out that this file must extend idlentity and is the same name with the IDL interface. This file provides type tags so that this interface can be used for other interfaces. CalculatorOperations.java - This file contains Java Public Interfaces - CalculatorOperations. The specification pointed out that this file should be with the same name with the IDL interface with the Operations suffix, and this file contains this interface mapping operation mark. The tag interface (Calculator.java) defined above can expand this interface. CalculatorHelper.java - The purpose of the design Helper class is to allow many of the completed complex to detach from our interface, but can be used at any time. Help program files contain important static Narrow methods, this method makes org.omg.corba.object contraction into a more specific type of object reference; in this case, it will be a compute program type. The CalculatorHolder.java - Holder class is a specialized class that is generated in order to deliver any data types of parameters by reference. This example will not be used in the Holder class, but we will often see it in the later columns. Calculatorpoa.java - The Skeleton class provides a part of the request-response detection for CORBA features. Generate Calculatorpoa.java because the implementation in the default mode is based on inheritance. If we choose the implementation of the commission, the output will be different. These topics will be described in detail in later columns. _CalculatorStub.java - As the name suggests, this is a STUB class. Your client will need this class to work. The files generated by the server now must start working on the server and implement our interface with this server. Fortunately, most of the probes are suitable for our request, but don't be happy too early - there are many work to do; that is, all of these documents must be used in the right place.

Let us start from the achievement of the add () method. (You can download the full SimpleCsvr.java file.)

Listing 3. SimpleCalcsvr.java - Add () method

SimpleCalcServant Extends Calculatorpoa {

Public Int Add (int x, int y) {

Return X Y;

}

}

Note that our implementation class extends the generated class CalculatorPoa. When a request is sent from the client, the request passes the ORB to Skeleton, and the Skeleton eventually calls SimpleCalcServant to complete the request and start the response. Our interface is simple, so our implementation is also very simple.

The rest of the server, involves how to achieve a CORBA architecture, many of which are executed by CORBA specifications due to portability and flexibility.

The first task we need to do is to explain which ORB you want to use, then initialize. The following code (files 18 lines to 29th lines of file SimpleCsvr.java) handles this task:

Listing 4. SimpleCalcsvr.java - Initializing ORB

Java.util.properties PROPS = System.getProperties ();

Props.Put ("Org.omg.Corba.orbclass",

"com.ooc.corba.org");

Props.Put ("Org.OMG.CORBA.ORBSINGLETONCLASS", "com.oc.corba.orbsingleton");

Org.omg.corba.orb orb = NULL;

// Initialize ORB

ORB = Org.omg.corba.orb.init (args, props);

When initializing ORB, you need to accurately tell it which class will be used as orbclass, which class will be used as the Orbsingleton class. Our implementation will not consider these, but all relevant probes will consider these. As I said earlier, in this case, I use ORBACUS ORB of Object Oriented Concepts, Inc., and the OOC class has been given in the two props.put () calls. Once the properties are filled, PROPS is only passed to the orb.init () method as a parameter. The actual situation may not be the case; if we want to move this server to another orb, you don't want to re-encode the server. So, in the ideal case, we would rather change a configuration file so that it will point to another ORB class and then restart directly.

Now, ORB has been in place and has been initialized, and the implementation has been in place, but it has not yet been created. At this time, it is necessary to create a perfect living location, and this is not as easy as it sounds, in a distributed environment, each implementation The required environment may be slightly different. Many features can be given. Implementation can be either single-threaded or multi-threaded; either a target pool having a high degree of scalability or a single element. Many different server features have produced a portable object adapter (POA). PoA allows us to create a sound environment for our implementation. All ORBs that match the 2.3 specification have a root POA, all other POAs are created from the root POA. In this simple example, I have decomposed a dedicated code to break down to its own method runcalc ().

To achieve an environment will be our first task, so you must set a POA. Originally, the CORBA server uses the basic object adapter (BOA), but each vendor's BOA is different. In the latest version of the CORBA specification, POA has completely replaced BOA.

Listing 5. SimpleCalcsvr.java - Set POA

// Rootpoa from always exists

/ / Set the portable object adapter

Org.omg.PortableServer.poa rootpoa =

Org.omg.PortableServer.poahelper.narrow

ORB.Resolve_initial_references ("rootpoa"));

Org.omg.PortableServer.poAmanager Manager =

Rootpoa.the_poamanager ();

As can be seen from title and definition, this is a simple example. Use the root POA without creating new PoA, will make things simple. The POA manager is an object that encapsulates the POA processing state, so we use the POA manager to queue the request to servant.

Recently required:

Listing 6. SimpleCalcsvr.java - instantiate implementation

// Create Servant of the Calculator Interface

SimpleCalcServant Calcsvt = new SimpleCalcServant ();

Calculator Calc = Calcsvt._THIS (ORB);

Follow the CORBA 2.3 specification, all SKELETON provides a _this () method, which allows servant to get object references for the target CORBA object, and Servant is using the target CORBA object to associate with these requests. After the implementation of the implementation, the mechanism must be placed in the appropriate location so that the client can find them. There are many different ways and services to find objects that meet the interface request. Corba Service Defines Naming Service and Trader Services to help clients look for objects to process requests. You can also transfer objects through method calls.

In this example, we will use the most straightforward one in all methods - write object references to a file, which will be selected by the client. For all ORBs, create a string representation of an object reference, or in turn, create a reference to the string to objects, is a must.

Listing 7. SimpleCalcsvr.java - Writing object reference

// Write an object reference to a file

PrintWriter Refstr = New PrintWriter

New FileWriter ("Calcref.ior");

Refstr.println (orb.Object_to_String (CALC));

REFSTR.CLOSE ();

The last thing to do is to activate POA, enable the client request to queue, and enforce the server to enter its event loop to receive these incoming requests.

Listing 8. SimpleCalcsvr.java - Activate POA

/ / Make the implementation become available

Manager.activate ();

System.out.println ("SimpleCalcsvr Is Running!");

Orb.run ();

Client If you consider the mechanism of events being happening, you will understand that the client and server are actually imaging. The client packs all the parameters to create a request, then send this request in its own way. The server is only packaged, executes the parameters, executes the return value, and output parameters, and then returns the response to the client. The client will unpack the return value and the output parameter and continue processing. In this way, what is the client packages, what is the server is unpacking, and vice versa. This means you will see a similar structure that the client and server have. The client must also create and initialize an ORB. It can be the ORB we are using, or other vendor's orb; however, it cannot be arbitrary ORB, but should be an ORB that supports IIOP, IIOP is defined by the Object Management Group, based on TCP / IP interoperability protocol. If your ORB is old, then be careful, it may not be able to talk to other ORB.

First, we create ORBs in the same way, just like creating a server. (You can download the full SimpleCalcClient.java file.)

Listing 9. SimpleCalcClient.java - Initializing ORB

Java.util.properties PROPS = System.getProperties ();

Props.Put ("Org.omg.Corba.orbclass",

"com.ooc.corba.org");

Props.put ("Org.omg.Corba.orbsingletonclass",

"com.ooc.corba.orbsingleton");

Org.omg.corba.orb orb = NULL;

// Initialize Orborb = Orb.init (Args, Props);

It looks familiar? It should be like this, it looks exactly the same as the server. Now, the client has been connected to an ORB, but our goal is to call a service, and this service is provided in other parts of the system and needs to find an object to respond to requests. In this example, this means that you have to get an object reference from the file created on the server. In order to find the compute program server, you need to get the string version of the object reference stored in this file, and then convert it into an object reference, and you can call it through this object.

Listing 10. SimpleCalcClient.java - Get object reference

System.out.println ("Getting Reference from String ...");

BufferedReader in = New BufferedReader

New FileReader ("Calcref.ior");

String iOR = in.readline ();

In.Close ();

Calculator Calc = CalculatorHelper.narrow

ORB.String_to_Object (iOR));

Note that the CalculatorHelper class generated by the IDL-To-Java compiler here is used here. The Calcref.ior file contains an object reference instead of the computing program reference. The CalculatorHelper class has a Narrow method that can be used to concentrate the abstract type to a specific calculator type.

Take a closer look at the calculation program CALC, which represents a server in another place in the computer space. One thing you must do, is to call the method add () on CALC.

Listing 11. SimpleCalcClient.java - call add ()

System.out.println (Calc.Add (2, 3));

Conclusion has discussed a lot of content, but please think about it, what you have learned. Our client and server are completely isolated, the client does not know what the server runs on what kind of hardware, what is the operating system, which is written in what language, it is not multi-thread, and it Where is it - is next door, or a distance from half an earth. It only knows, that is, if it calls the add () in CALC, it will be desirable. All the way to provide services is like this, the phone or power company is also the same. When you pick up the phone, what you expect is to hear the tuning sound, then your call can be connected, you don't care about the phone is transferred through the cable or forward the satellite, the same situation is also in the information industry. It is becoming a reality. Thanks to the OMG and this basic structure, we can also add this simple and very persuverable example. Next month, we will discover a little in depth and see the magical power of IIOP that happens in the surface phenomenon.

转载请注明原文地址:https://www.9cbs.com/read-927.html

New Post(0)