Java IDL and CORBA

Java IDL and CORBA

references

Java Network Programming and Distributed Computing
book for network programming, I think there is a very big help! Very good book.
This book is the section on Java IDL and CORBA, about 15, I began to seize the time to study.

But I found this book to the last study in 2002, there is a bug not know how it
Yucheng Lin: the way the book describes the implementation of IDL model is outdated, but the basic idea is not a problem. Or write code, then refer to this link now.
https://docs.oracle.com/javase/7/docs/technotes/guides/idl/jidlExample.html


What is CORBA ?
The Common Object Request Broker Architecture

I do not understand it does not matter, let's read on.

CORBA is a standard for making object method invocation requests over a network.

CORBA is a standard object method invocation request initiated in the network

The Object Management Group (OMG), a large consortium of companies devoted to improving remote object method invocation, developed by the CORBA specification.

OMG is a big company, this thing is to improve the company's CORBA standard.

CORBA itself is not a language, but CORBA introduces a new language. CORBA service described by a schema, the schema is a template for the methods an object makes available.

This model is expressed by the IDL (Interface Definition Language) is. Java can achieve a IDL schema, thus allowing the software to call method.

Let's take a closer look at CORBA.

12.2 Architectural View of CORBA

CORBA objects by a number of software applications and components. A Schema specification, written in IDL, describes the objects that are exported for remote usage.

An object request broker (ORB) implements the interface between remote object and software client. The ORB handles requests for an object’s services, as well as sending a response.

AN 12-1 the Provides the Overview of Figure How Clients and servants (at The End at The Implementation of Service ) a using AN ORB.
Here Insert Picture Description
The method even if the object is seen as a local object, but you can directly call these objects, as if these objects are normal objects .

ORB is acting as an intermediary between client and servant.

Communication between client and servant occurs over the network via the Internet Inter-ORB Protocol(IIOP), 如图12-2所示。

Figure 12-2
Here Insert Picture Description

12.2.1 CORBA Services

在CORBA体系结构中, software services are described by a schema and implemented by a servant. The servant is a special piece of software that registers itself with a lookup service, so that other CORBA software can locate and access its services

Typically, a CORBA server will create a CORBA servant, and a CORBA server is then responsible for creating an ORB for the servant and registering the service for access by other clients. CORBA服务器如何注册服务如图12-3所示。
Here Insert Picture Description

12.2.2 CORBA Clients

Clients不需要注册一个名字服务。Clients想使用名字服务器来查询服务(如图12-4)。这些服务器可能总是在相同的机器上,但是如果这些服务搬家了,名字服务器会帮忙找到这些服务。
Here Insert Picture Description
CORBA一直被设计的很健壮,所以服务搬到新机器并不会使那些依赖于名字服务器的客户端感到困惑。下一次,客户端查询某些服务的位置的时候,这些被搬家的服务会被定位到一个新的机器。

12.3 Interface Definition Language (IDL)

IDL 是一个非常重要的语言,我们需要学习和掌握,因为所有的CORBA服务都是由IDL描述的。

12.3.1 Overview the language

IDL定义了许多语言结构,使得a rich schema of objects can be described. 我们会学习一些基本的语言结构。

12.3.2 IDL数据类型

IDL定义了一些基本数据类型,例如numbers and text。IDL也可以创建一些更加强大的结构,例如数组,序列 和 数据结构。我们只研究基本的。
图12-1展示了Java数据类型和IDL数据类型之间的映射
Here Insert Picture Description

12.3.3 IDL Interfaces

Interface definitions describe a remote object, the methods that the object exposes, as well as member variables, such as constants. The interface keyword is used to describe an interface. 下面是个IDL中接口的例子。

interface UserAccount {
	float getBalance();
	void setBalance(in float amount); 
};

12.3.4 IDL Modules

Modules are a convenient way of grouping logically related interfaces for convenience. modules和java中的package是对应的。相关的类被放在一个包里。下面是个例子。

module AccountTypes {
interface UserAccount
{ // code for account would go here
};
interface UserID
{ // code for userid would go here
};
interface Subscriptions
{ // code for subscriptions would go here
}
}

12.3.5 IDL Attributes

暂时不研究

12.3.6 IDL Operations

IDL schema中最重要的部分就是定义操作了。这些操作可能会被远程请求,这些操作可以完成各种各样的任务。

在IDL操作中,有3中类型的参数可以被指定。

  1. in - parameter used for input only, and is immutable(不可改变的)
  2. out - parameter whose contents may be modified ,and is not immutable (可以改变的)
  3. inout - parameter that combines the properties of in and out. Such parameters may be used as input and may also be modified.

A cleaner implementation will result when you use input parameters only, and a single return value. However, sometimes it may be necessary to use the other modifiers; 请读者自行决定(Readers should be advised to use discretion)。

to use one’s own discretion, 由某人自行决定。(学点英语呵呵呵)

下面这个接口有3个简单的操作,有2个操作是接收一个输入参数。每个操作都被指定了返回类型(在这里,是float)

interface UserBalance{
	float getBalance();
	float addBalance(in float amount);
	float subtractBalance(in float amount);
};

注意,如果一个方法没有返回值,和java类似,idl使用void关键字。

12.3.7 IDL Exception Handling

暂不研究

12.4 From IDL to Java

到现在为止,我们已经cover了IDL的基本语法。The next step is to write a CORBA service described by an IDL schema and implemented in Java.
我们也需要写一个CORBA客户端来访问我们的服务。

12.4.1 A Sample Schema

The following schema describes our distributed lightbulb service, which will be saved as a text file called LightBulb.idl.

exception BulbBrokenException
{
};
interface LightBulb
{
	void on() raises (BulbBrokenException);
	void off();
	boolean isOn() raises (BulbBrokenException);
};

下一步就是将这个IDL schema映射为Java文件

12.4.2 Mapping an IDL Schema to Java

我们并不是手工将这个IDL转换为java文件,我们使用了一个简单的工具来自动化这个过程。这个工具就是idlj.exe。这个是java自带的。(但是java 11把这个工具给移除了)。
假设你已经将这个schema保存成了LightBulb.idl了,你就可以使用下面这个命令进行映射。

idlj -fall LightBulb.idl

这里我们使用了一个参数**-fall**. You can specify that you want to generate only the client mapping by using -fclient, and only the server mapping by using fserver. For convenience, you’ll usually do all mappings at once.

然后你就会发现多了许多文件,这些都是通过idlj自动创建的。你千万不要去直接修改这位文件。因为你修改了也是没有用的,为什么呢?
你将来一旦重新run上面的那个idlj命令,你所有的修改都会被overwritten. 正确的做法是,使用这些类as the scaffolding for a CORBA servant and a CORBA client.

12.4.3 Writing the Servant Code

写代码实现一个CORBA服务is fairly straightforward. 你要知道,大多数复杂的工作都由idlj帮我们做了。

当你在idl中定义接口并使用idlj进行mapping之后,你就会得到许多文件。对writing a servant来说,两个最重要的文件就是下面这两个:

  • InterfaceOperations.java
  • _InterfaceImplBase.java (probably a little old book, now called _InterfaceStub.java, but the meaning is the same)

So, when we compile the LightBulb schema, you get

  • LightBulbOperations.java
    The Operations source file 提供了the Java mapping of operations a CORBA servant must implement. You can copy and paste these method signatures into your servant, and then provide an implementation.
  • _LightBulbImplBase.java
    The second file, ImplBase, should be extended using class inheritance by your servant, and should then implement the methods of the Operations file.

The following example shows the implementation of a CORBA servant.

Knock code, wait

Code has Qiaowan, but encountered a problem.
Look at the following example.
https://docs.oracle.com/javase/7/docs/technotes/guides/idl/jidlExample.html

Guess you like

Origin blog.csdn.net/ChenglinBen/article/details/91562864