Java IDL and CORBA

Java IDL and CORBA

参考文献

Java Network Programming and Distributed Computing
这本书我觉得对于网络编程有非常大的帮助!非常不错的书。
这本书有关Java IDL 和 CORBA的部分,大概有15页,我开始抓紧时间研究。

但是我研究到最后发现这本书是2002年的,有个bug不知道是怎么回事
余成林:该书中所介绍的实现IDL模式的方式已经过时了,但是基本思想是没有问题的。写代码的话还是参考这个链接吧。
https://docs.oracle.com/javase/7/docs/technotes/guides/idl/jidlExample.html


什么是 CORBA?
Common Object Request Broker Architecture

不懂没关系,咱们接着往下看。

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

CORBA是一个在网络中发起对象方法调用请求的标准

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

OMG是个大公司,这个公司的事情就是改进CORBA标准。

CORBA本身并不是一种语言,但是CORBA引入了一种新的语言。CORBA服务是由一个schema描述的,这个schema是一个模板for the methods an object makes available.

这样的模式是由IDL(Interface Definition Language)来表达的。Java可以实现一个IDL schema,这样就允许软件调用方法。

让我们来仔细研究一下CORBA.

12.2 Architectural View of CORBA

CORBA由许多对象和软件应用组成。一个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.

Figure 12-1 provides an overview of how clients and servants (the end implementation of the service) using an ORB.
在这里插入图片描述
即使对象被看做是本地对象,但是你可以直接调用这些对象的方法,as if 这些对象 是正常的对象。

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所示。

图12-2
在这里插入图片描述

12.2.1 CORBA服务

在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所示。
在这里插入图片描述

12.2.2 CORBA Clients

Clients不需要注册一个名字服务。Clients想使用名字服务器来查询服务(如图12-4)。这些服务器可能总是在相同的机器上,但是如果这些服务搬家了,名字服务器会帮忙找到这些服务。
在这里插入图片描述
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数据类型之间的映射
在这里插入图片描述

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(可能这本书有点老了,现在叫做_InterfaceStub.java, 但是意思是一样的)

所以,当我们编译the LightBulb schema之后,就会得到

  • 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.

下面这个例子展示了一个CORBA servant的实现。

敲代码中,稍等

代码已经敲完了,但是遇到了一个问题。
看一下下面这个例子。
https://docs.oracle.com/javase/7/docs/technotes/guides/idl/jidlExample.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chenglin_Yu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值