Distributed programming based on Java IDL

zhaozj2021-02-08  511

Distributed programming based on Java IDL

(This article is reproduced from Software Engineering Expert Network www.21cmm.com)

Java IDL introduction

Javaidl is a CORBA feature extension in the Java 2 development platform. Javaidl is introduced in Java 2 so that the OMG IDL can define the basic functions of the service object, and map IDL to the Java language according to the requirements of the CORBA specification, and develop standard interoperability and connectivity distributions. Application. Javaidl makes a distributed, supporting Web's Java applications can call remote services in transparently based on IIOP protocol.

The Javaidl runtime component includes a fully compatible object requesting agent - Java ORB for communication between distributed objects based on the IIOP protocol. The ORB supports transient CORBA objects and transient name servers, and the ORB survival period is limited by the run ORB process.

In the programming, first systematically analyze the service object function to be implemented, and create an IDL interface description file to describe the function. Then use the IDL to Javaidl to the Java language mapping tool to map the IDL file to the client pile (Skeleton) file.

In implemented client applications, including resolution processing such as a reference, service function request, and resolution of the service object return result. Typically, client applications use naming services to implement the binding of the remote object, and link the client with the service object through the client ORB to implement the remote call of the method.

At the server side, the ORB uses the service object skeleton to convert the data format of the call request and parameters to convert the remote call to the call to the method in the local object. When the method returns, the skeleton converts and encapsulates the calculation result, and the result is returned to the client through the ORB.

Run the process of establishing a CORBA application

The main problem with distributed application design is to determine the relationship between customers and service objects established on the object level. From its most fundamental function, the service object provides remote interface, customer object calls remote interface, customer objects do not need to understand remote The location of the CORBA object and details, nor does it need to know which ORB is used for interaction between objects.

According to the basic procedure of the implementation, the implementation of the CORBA object service is divided into two: objects' named reference methods and characters string object reference. The process of Corba Creating a Distributed Application is generally as follows:

● Systematic analysis, determine the functionality that the service object needs to be implemented;

● Write the IDL interface description file according to the system analysis results;

● Compile the interface description file, generate the backbone of the service object and the pile of the customer object (optional);

● Write customer object programs based on the pile of customer objects;

● Write a service object program based on the backbone or dynamic request of the service object;

● Compile customer objects and service objects separately;

● Start the service object program;

● Start customer object programs.

Distributed application example

The process of establishing a distributed application with one routine:

1. Summary function description and system brief design

Assign a string object on the service object side, the client gets the value of the string by calling the service object method. According to the instructions of the object function, use UML to describe the functions that the service object needs to be implemented:

Getit (): String []

2. Service object interface definition

According to the system analysis result, write the service object method description program getMessage.IDL with IDL:

Module GetMessage

{Interface Getit

{String ReturnObject ();

}

}

3. Compile GetMessage.IDL

IDLTOJAVA-FNO-CPP GetMessage.IDL.

4. Write client programs

// introduce related categories

Import org.omg.cosnaming. *; import org.omg.corba. *;

// Client object method

Public Class Client

{public static void main (string args [])

{// creation and initialize ORB

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

// Get root naming service context objects

Org.omg.corba.object naming =

Orb.resolve_initial_references ("Nameservice");

NamingContext NamingContext = NamingContextHelper.Narrow (Naming);

// Analyze object reference in naming

NameComponent NC = New NameComponent ("GetMessage", ""

Namecomponent path [] = {nc};

GetMessage.Getit Method = getMessage-

Helper.narrow (NamingContext.Resolve (PATH));

// Call service object method

String result = method () ;.returnObject ();

}

}

5. Write the service object program

// introduce related categories

Import org.omg.cosnaming. *;

Import Org.omg.cosnaming.namingContext

Package. *;

Import org.omg.corba. *;

// Service method

Class ReturnMethod Extends_GetMessage-

Implbase

{Public String Getit ()

{String Result = "How About IT";

Return Result;

}

}

// server-side method

Public Class Server

{public static void main (string args [])

{// creation and initialize ORB

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

// Create a service object and register it to ORB

ReturnMethod Obj = new returnMethod ();

ORB.CONNECT (RETURNMETHOD);

// Get root name context

Org.omg.corba.Object objref =

ORB.RESOLVE_INITIAL_REFERENCES

("NameService");

NamingContext ncref = naming

Contexthelper.narrow (Objref);

/ / Bind object reference in the name

NameComponent NC = New NameComponent ("GetMessage", ""

Namecomponent path [] = {nc};

Ncref.rebind (path, objref);

// Wait for call from the client

Java.lang.Object sync = new java.lang

.Object ();

SYNCHRONIZED (SYNC)

{sync.wait ();

}

}

}

6. Compile server-side and client programs separately

(1) Compile server-side program:

Javac getMessage / Server.java

(2) Compiling the client program:

Javac getMessage / Client.java

7. run

(1) Open an emulation terminal window, start the naming service, where 3388 is the communication port number:

TNAMESERV -ORBINITIALPORT 3388 (2) Enter the following command in another window, run the server program:

Java Server -orbinitialPort 3388

(3) Enter the following command in another window to run the client program:

Java Client -orbinitialPort 3388

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

New Post(0)