Simple examples of RMI, CORBA, IIOP - 2. CORBA

xiaoxiao2021-03-06  13

CORBA Basic Introduction

CORBA (Public Object Request Agent System) is an Obital-based distributed computing application software architecture based on Obital-based technology in 1991 in 1991. The CORBA standard is mainly divided into three parts: Interface Definition Language (IDL), Object Request Agent (ORB), and ORB Interoperability IIOP, the core is an object request agent. CORBA can abstract system platform, network communication, and programming language differences. During the CORBA Technical Specification, such as Naming Service, Transaction Service, Temperature Service (Lifecle Service), Concurrence Control Service, Time Service (Time Service and other functions provide application software developers with a comprehensive, robust, scalable distributed object computing platform that makes it easy to reuse, easy transplantation, and interoperability in distributed heterogeneous environments.

Compare with RMI

Unlike RMI, CORBA does not belong to the Java platform itself. OMG (Object Management Group, Object Management Organization) invented CORBA specifications, CORBA is designed to be independent of platforms and languages. Therefore, the CORBA object can be run on any platform, and can be written in any location of the network, including Java, C, C, C, C, C, C and SmallTalk, etc., as long as the language has IDL (Interface Definition Language, Interface Definition Language) Mapping.

Compared to RMI, CORBA is prepared for larger and scalable systems, and there may be thousands of objects in these systems; CORBA programming and deployment is more complex than RMI, but the allowable transaction needs transaction, security Sexual supported enterprise-class systems; CORBA naming services are also more powerful and flexible than RMI naming registration functions.

CORBA implementation is called ORB (Object Request Broker, Object Request Agent). Java IDL is an implementation of CORBA, one of the core packages of JDK1.3 or higher, defined in org.omg.corba and its sub-package. With the support of Java IDL, developers can use the following two ways to integrate Java and CORBA:

· ?? Creating a Java object and expands in Corba ORB,

· Create a Java class and a customer who expands with other ORBs. This approach provides another way, through which Java can be used to integrate your new application and previously legacy systems.

Create a CORBA app with Java

The implementation of CORBA object services is divided into two types: object naming reference methods and characters string object reference. Regardless of the advanced language, the process of creating a CORBA application is generally as follows:

● Write an IDL interface definition file;

● Compile the interface definition file to the corresponding advanced language source code, generate server framework and client repayment;

● Based on server framework, write service object implementation procedures;

● Based on the client memory, write customer object calls;

● Compile customer objects and service objects separately;

● Run the service object and the customer object program;

CORBA instance analysis

By an example below, describe how to create a CORBA application via Java.

1. Interface definition (Hello.idl) Hello.idl

Module HelloApp

{

Interface Hello

{

String Sayhello (in string message);

}

}

By compiling the IDL file into a Java source code, the SUN is included to generate a client stub and server framework for the interface definition file. The specific operation is as follows:

IDLJ -OLDIMPLBASE-FALL HELLO.IDL

After compiling will form the following six files in the HelloApp subdirectory:

n_HelloImplbase.java

The abstract class is a server Skeleton that provides basic CORBA feature for the server. It implements a Hello.java interface. Server class Helloservant extension _helloIMPLBase.

n_hellostub.java

This class is client STUB that provides a CORBA feature for the client. It implements a Hello.java interface.

n hello.java

This interface contains Java versions of the IDL interface. Hello.java interface extends org.omg.corba.object and provides standard CORBA object capabilities.

n hellohelper.java

This is a normal class that provides auxiliary functions, especially providing a Narrow () method required to convert CORBA object references to an appropriate type.

n helloholder.java

This is a terminal class, which contains a public example member of the Hello type. It provides operations for "OUT" and "inout" variables. CORBA has these variables, but it is not easy to map semantics in Java.

n hellooperations.java

This is an interface class, which contains SayHello ().

To complete the app, just provide the implementation of the server and client in the file helloServer.java and HelloClient.java.

2. Interface implementation

HelloImpl.java is an implementation of the Hello IDL interface; each Hello instance is implemented by a HelloIMPL instance. HelloIMPL is the subclass of _HelloImplbase, _HelloImplBase is generated by the IDLJ compiler from the sample IDL.

HelloIMPL.JAVA

/ *

* @Author javamxj (9cbs blog) creation date 2004-12-27

* /

Import helloApp. *;

Public class helloimpl extends _helloimplbase {

/* Constructor */

Public helloimpl () {

Super ();

}

/ * Implement interface declaration method Sayhello * /

Public String Sayhello (String Message) {

System.out.println ("I am in the server side of CORBA, the client is calling the 'SayHello' method.");

System.out.println ("Hello" Message);

Return Message;

}

}

3. Server

Server class contains server

MAIN () method, you can complete the following tasks:

· Create an ORB instance

· Create a HelloImpl instance (implementation of CORBA Hello object) and notify ORB

· Get ​​a CORBA object reference to a named context, register a new CORBA object in this name context

· Register the new object under the "Hello" name in the name context

· Waiting for the call to the new object

Hellosever.java

/ *

* @author javamxj (9cbs blog) creation date 2004-12-27 * /

Import org.omg.cosnaming. *;

Import org.omg.corba. *;

Public class helloserver {

Public static void main (string args []) {

Try {

/ * Create and initialize ORB * /

ORB ORB = Orb.init (args, null);

System.out.println ("Start ORB Server ...");

/ * Create an instance and register it to orb * /

HelloIMPL HelloImpl = New HelloIMPL ();

Orb.connect (HelloIMPL);

System.out.println ("Registering an Instance to ORB");

/ * Get root name context * /

Org.omg.corba.Object objref = orb.resolve_initial_references ("nameservice");

NamingContext ncref = NamingContextHelper.narrow (Objref);

/ * Object reference in binding name * /

NameComponent NC = New NameComponent ("Hello", ");

Namecomponent path [] = {nc};

ncref.rebind (path, helloimpl);

/ * Wait to call from the client * /

Java.lang.object sync = new java.lang.Object ();

Synchronized (sync) {

Sync.wait ();

}

System.out.println ("Waiting for CORBA Clients ...");

} catch (exception e) {

System.err.println ("Error:" E);

E.PrintStackTrace (System.out);

}

}

}

4. Client

The following application client will complete the following tasks:

· Create an ORB

· Get ​​a reference to named a named context

· Find "Hello" in the name context and get a reference to the CORBA object

· Call the SayHello () operation of the object and print the result

HelloClient.java

/ *

* @Author javamxj (9cbs blog) creation date 2004-12-27

* /

Import helloApp. *;

Import org.omg.cosnaming. *;

Import org.omg.corba. *;

Public class helloclient {

Public static void main (string args []) {

Try {

/ * Create and initialize ORB * /

ORB ORB = Orb.init (args, null);

/ * Get root name context * /

Org.omg.corba.Object objref = orb.resolve_initial_references ("nameservice");

NamingContext ncref = NamingContextHelper.narrow (Objref);

/ * Analyze object reference in naming * /

NameComponent NC = New NameComponent ("Hello", ");

Namecomponent path [] = {nc}; hello h = hellohelper.narrow (ncref.resolve (path));

/ * Call the Hello server object and print the result * /

System.out.println ("I am at the client, start calling the 'SayHello' method of the CORBA server);

System.out.println ("Welcome," H.SAYHELLO ("JavaMxj Blog");

} catch (exception e) {

System.out.println ("Error:" E);

E.PrintStackTrace (System.out);

}

}

}

Compilation and operation of CORBA Server / Client

• Copy the 4 files to the D: / Corbasample directory, create a client and server directory in this directory, assuming they are the client and the server.

· Compiling Hello.idl

D: / corbasample>

IDLJ -OLDIMPLBASE-FALL HELLO.IDL

This will generate a HelloApp's directory

· Compile all Java files:

D: / corbasample>

Javac * .java helloApp / *.

· Establish a HelloApp subdirectory in the client and server directory, in the D: / Corbasample / HelloApp directory

_Hellostub.class

Hello.class

HelloHelper.class

HelloHolder.class

HelloOperations.class

Copy to the D: / CORBASAMPLE / Client / HelloApp directory, then copy the helloclient.class in the D: / Corbasample directory to the D: / CORBASAMPLE / CLIENT directory.

In the D: / CORBASAMPLE / HELLOAPP directory

_HelloImplbase.class

Hello.class

HelloOperations.class

Copy to the D: / Corbasample / Server / HelloApp directory, then copy the helloServer.class and HelloImpl.class in the D: / CORBASAMPE directory to the D: / CORBASAMPLE / SERVER directory

(

note:

Of course, you can do not have to build a server and client directory and the above copy files, you can do it directly in the D: / Corbasample directory, I do this is mainly to distinguish client and server)

· Make sure the name server is running:

D: / corbasample / server>

TNameServ -orbinitialPort 1050

· Start Hello Server:

D: / corbasample / server>

Java HelloServer -orbinitialPort 1050

· CORBA client calls CORBA services:

D: / corbasample / client>

Java Helloclient -orbinitialPort 1050

(Local call, do not need to specify remote IP addresses with -orbinitialhost parameters)

D: / corbasample / server>

Java HelloClient -orbinitialHost Localhost -orbinitialPort 1050

(Remote call CORBA service, you can replace localhost to remote IP address) Figure 1. Before the client does not call CORBA services

Figure 2. After the client calls the CORBA service

A little added:

I don't know if you don't pay attention to it, use it when you compile IDL files in JDK1.4.

IDLJ -OLDIMPLBASE-FALL HELLO.IDL

This command comes with the "-oldimplbase" parameter indicating that this is an old version. Under the JDK1.4, you can use it directly.

IDLJ-Fall Hello.idl command, so

IDLJ

The compiler generates a lightweight object adapter to the server side.

Poa

).

(Direct command line knocking IDLJ can get its instructions)

Here I will not explain it, and the operation is basically similar to the previous example.

First prepare the following 4 files under the D: / CORBASAMPLE2 directory, and then do the following:

D: / corbasample2> idlj-fall hello.idl

D: / corbasample2> javac * .java helloApp / *. Java

D: / corbasample2> TNameServ -orbinitialPort 1050

D: / corbasample2> Java HelloServer -orbinitialport 1050

D: / corbasample2> Java HelloClient -orbinitialport 1050

(Note: Here is for convenience. I have no further division client and server. Perform comparison, you can find that the files generated by this IDLJ compile and different files generated in the previous example.)

Hello.idl

Module HelloApp

{

Interface Hello

{

String Sayhello (in string message);

}

}

HelloIMPL.JAVA

/ *

* @Author javamxj (9cbs blog) creation date 2004-12-27

* /

Import org.omg.corba. *;

Import helloapp.hellopoa;

Public class helloimpl extends hellopoa {

Private Orb ORB;

Public void setorb (orb orb_val) {

ORB = ORB_VAL;

}

/ * Implement interface declaration method Sayhello * /

Public String Sayhello (String Message) {

System.out.println ("I am in the server side of CORBA, the client is calling the 'SayHello' method.");

System.out.println ("Hello" Message);

Return Message;

}

}

Helloserver.java

/ *

* @Author javamxj (9cbs blog) creation date 2004-12-27

* /

Import helloApp. *;

Import org.omg.cosnaming. *;

Import org.omg.corba. *;

Import Org.omg.PortablesRver. *;

Import Org.omg.PortableServer.poA;

Public class helloserver {

Public static void main (string args []) {

Try {

/ * Create and initialize ORB * /

ORB ORB = Orb.init (args, null);

/ * Get reference to rootpoa, start poamanager * / poa rootpoa = poahelper.narrow (ORB

.resolve_initial_references ("rootpoa"));

Rootpoa.The_Poamanager (). Activate ();

/ * Create an instance and register it to orb * /

HelloIMPL H = New HelloIMPL ();

H.setorb (ORB);

System.out.println ("Registering an Instance to ORB");

/ * Get object references to the server * /

Org.omg.Corba.Object Ref = rootpoa.servant_to_reference (h);

Hello href = hellohelper.narrow (REF);

/ * Get the root element name context from the name service * /

Org.omg.corba.object Objref = ORB

.resolve_initial_references ("NameService");

NamingContexText ncref = namingcontextexthelper.narrow (objref);

/ * Register a new object under the Hello name * /

String name = "Hello";

NameComponent Path [] = ncref.to_name (name);

NCREF.Rebind (path, href);

/ * Wait for the client's call. * /

Orb.run ();

System.out.println ("Waiting for CORBA Clients ...");

}

Catch (Exception E) {

System.err.println ("Error:" E);

E.PrintStackTrace (System.out);

}

System.out.println ("HelloServer EXITING ...");

}

}

HelloClient.java

/ *

* @Author javamxj (9cbs blog) creation date 2004-12-27

* /

Import helloApp. *;

Import org.omg.cosnaming. *;

Import org.omg.corba. *;

Public class helloclient {

Public static void main (string args []) {

Try {

/ * Create and initialize ORB * /

ORB ORB = Orb.init (args, null);

/ * Get root name context * /

Org.omg.corba.object Objref = ORB

.resolve_initial_references ("NameService");

NamingContexText ncref = namingcontextexthelper.narrow (objref);

/ * Find the Hello object in the name context to get a reference to it. * /

String name = "Hello";

Hello h = hellohelper.narrow (ncref.resolve_str (name));

System.out.println ("I am at the client, start calling the 'SayHello' method of the CORBA server);

System.out.println ("Welcome," H.SAYHELLO ("JavaMxj Blog");

} catch (exception e) {system.out.println ("error:" e);

E.PrintStackTrace (System.out);

}

}

}

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

New Post(0)