CORBA介绍及编程实例

一. CORBA的介绍

CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构)是由OMG组织制订的一种标准的面向对象应用程序体系规范。或者说 CORBA体系结构是对象管理组织(OMG)为解决分布式处理环境中,硬件和软件系统的互连而提出的一种解决方案。

二. CORBA 体系结构概述

CORBA规范充分利用了现今软件技术发展的最新成果,在基于网络的分布式应用环境下实现应用软件的集成,使得面向对象的软件在分布、异构环境下实现可重用、可移植和互操作。其特点可总结为如下几个方面:

  • 引入中间件作为事务代理,以完成客户机向服务对象方提出的业务请求。
  • 实现客户与服务对象的完全分开,客户不需要了解服务对象的实现过程以及具体位置。
  • 提供软总线机制,使得在任何环境下、采用任何语言开发的软件只要符合接口规范的定义,均能够集成到分布式系统中。
  • CORBA 规范软件采用面向对象的软件实现方法开发应用系统,实现对象内部细节的完整封装,保留对象方法的对外接口定义。

三. IDL(接口定义语言)

  IDL是用来描述软件组件接口的一种计算机语言。IDL通过一种中立的方式来描述接口,使得在不同平台上运行的对象和用不同语言编写的程序可以相互通信交流;比如,一个组件用C++写成,另一个组件用Java写成。
  IDL通常用于远程调用软件。 在这种情况下,一般是由远程客户终端调用不同操作系统上的对象组件,并且这些对象组件可能是由不同计算机语言编写的。IDL建立起了两个不同操作系统间通信的桥梁。
  从本质上讲,OMG IDL接口定义语言不是作为程序设计语言体现在CORBA体系结构中的,而是用来描述产生对象调用请求的客户对象和服务对象之间的接口的语言。OMG IDL文件描述数据类型和方法框架,而服务对象则为一个指定的对象实现提供上述数据和方法。
 
四. 实例1 
  服务对象接口定义,用IDL编写服务对象方法Hello.idl的描述程序:
  
1 module HelloApp{
2     interface Hello{
3       string sayHello(in string name);
4     };
5 };
View Code

   编译Hello.idl。Java JDK提供了对CORBA的支持,Java IDL,即idlj编译器就是一个ORB,可用来在Java语言中定义、实现和访问CORBA对象。

      具体步骤,在cmd中执行下面命令为接口定义文件生成客户端本地代理和服务器框架。

  idlj -oldImplBase -fall Hello.idl   (注意:在执行命令之前要将目录转移到该文件所在位置,或将该idl文件拷贝到Java jdk的bin目录下。)

  然后,将生成的HelloAPP文件夹内所有文件导入到IDE编辑器。如图:

  

  编写服务对象程序HelloImpl.java,该类继承了生成文件中的类_HelloImplBase。

 1 package org.ngweg.corba;
 2 
 3 import HelloApp._HelloImplBase;
 4 
 5 public class HelloImpl extends _HelloImplBase {
 6     private static final long serialVersionUID = 1L;
 7 
 8     @Override
 9     public String sayHello(String name) {
10         System.out.println("Acceptin client call: " + name);
11         return "Hello, " + name;
12     }
13 }
View Code

  服务端程序HelloServer

 1 package org.ngweg.corba;
 2 
 3 import org.omg.CORBA.ORB;
 4 import org.omg.CosNaming.NameComponent;
 5 import org.omg.CosNaming.NamingContext;
 6 import org.omg.CosNaming.NamingContextHelper;
 7 
 8 public class HelloServer {
 9 
10     public static void main(String args[]) {
11         try {
12             String[] init = {"-ORBInitialPort","1234"};
13             // ORB initial
14             ORB orb = ORB.init(init, null);
15             
16             // Register a instance of HelloImpl
17             HelloImpl helloImpl = new HelloImpl();
18             orb.connect(helloImpl);
19             
20             // Get the context
21             org.omg.CORBA.Object obRef = orb
22                     .resolve_initial_references("NameService");
23             NamingContext ncRef = NamingContextHelper.narrow(obRef);
24             
25             // Binding
26             NameComponent nc = new NameComponent("Hello", "");
27             NameComponent path[] = { nc };
28             ncRef.rebind(path, helloImpl);
29             
30             // waiting for client call.
31             System.out.println("Starting CORBA success.");
32             java.lang.Object sync = new java.lang.Object();
33             synchronized (sync) {
34                 sync.wait();
35             }
36         } catch (Exception e) {
37             System.err.println("Starting CORBA fails. " + e);
38         }
39     }
40 }
View Code

   客户端程序HelloClient

 1 package org.ngweg.corba;
 2 
 3 import org.omg.CORBA.ORB;
 4 import org.omg.CosNaming.NameComponent;
 5 import org.omg.CosNaming.NamingContext;
 6 import org.omg.CosNaming.NamingContextHelper;
 7 
 8 import HelloApp.Hello;
 9 import HelloApp.HelloHelper;
10 
11 public class HelloClient {
12     public static void main(String args[]) {
13         try {
14             String[] init = {"-ORBInitialPort","1234"};
15             // ORB initial
16             ORB orb = ORB.init(init, null);
17 
18             // Get the context
19             org.omg.CORBA.Object obRef = orb
20                     .resolve_initial_references("NameService");
21             NamingContext ncRef = NamingContextHelper.narrow(obRef);
22 
23             NameComponent nc = new NameComponent("Hello", "");
24             NameComponent path[] = { nc };
25             
26             Hello hello = HelloHelper.narrow(ncRef.resolve(path));
27             System.out.println(hello.sayHello("ZhangZijian"));
28         } catch (Exception e) {
29             System.out.println("Calling CORBA fails." + e);
30         }
31 
32     }
33 }
View Code

 

   至此,程序部分编写完毕,但是要执行该实例,必须要启动命名服务。

  根据代码中设置的端口号,启动该服务,在命令行输入:tnameserv -ORBInitialPort 1234 即可。

  然后分别运行Server端和Client端程序。

五. 实例2

定义一个接口StudentManager,该接口有long类型的属性id(只读)、string类型 name和一个返回类型为string的方法QueryStudentStatus,该方法包含一个参数studentID, 参数值由客户端给入。

编写IDL文件:

1 module StudentSystem{
2     interface StudentManager{
3     readonly attribute long Id;
4     typedef string Name;
5     string QueryStudentStatus(in long studentId);
6     };
7 };
View Code

 

 使用idlj编译器生成相应文件,执行命令为:idlj -oldImplBase -fall student.idl

拷贝相应文件到编辑器,编写实现类的代码:

 1 package org.corba;
 2 
 3 import StudentSystem._StudentManagerImplBase;
 4 
 5 public class studentImpl extends _StudentManagerImplBase{
 6 
 7     private static final long serialVersionUID = 1L;
 8     private int id;
 9     private String name;
10     
11     public studentImpl(int id){
12         this.id = id;
13     }
14     
15     
16     public int getId() {
17         return this.id;
18 
19     }
20 
21     public String getName() {
22         return this.name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27 
28     }
29 
30     @Override
31     public int Id() {
32         return 0;
33     }
34 
35     @Override
36     public String QueryStudentStatus(int studentId) {
37         if(this.getId() == studentId)
38         {
39             return this.getName();
40         }
41         else{
42             return null;
43         }
44     }
45 
46 
47 }
View Code

 

服务器端:

 1 package org.corba;
 2 
 3 import org.omg.CORBA.ORB;
 4 import org.omg.CosNaming.NameComponent;
 5 import org.omg.CosNaming.NamingContext;
 6 import org.omg.CosNaming.NamingContextHelper;
 7 
 8 public class server {
 9 
10     public static void main(String[] args) {
11         try{
12                 String[] init = {"-ORBInitialPort","1234"};
13                 ORB orb = ORB.init(init,null);
14                 studentImpl stu = new studentImpl(12);
15                 stu.setName("zhangsan");
16                 orb.connect(stu);
17                 org.omg.CORBA.Object obRef = orb
18                         .resolve_initial_references("NameService");
19                 NamingContext ncRef = NamingContextHelper.narrow(obRef);
20                 NameComponent nc = new NameComponent("StudentManager","");
21                 NameComponent path[] = { nc };
22                 ncRef.rebind(path, stu);
23                 System.out.println("Starting CORBA success." );
24                 java.lang.Object sync = new java.lang.Object();
25                 synchronized (sync) {
26                     sync.wait();
27                 }
28                 
29             }catch(Exception e){
30                 System.err.println("Starting CORBA fails. " + e);
31             }
32             
33 
34         }
35 
36 
37 }
View Code

 

客户端:

 1 package org.corba;
 2 
 3 import org.omg.CORBA.ORB;
 4 import org.omg.CosNaming.NameComponent;
 5 import org.omg.CosNaming.NamingContext;
 6 import org.omg.CosNaming.NamingContextHelper;
 7 
 8 import StudentSystem.StudentManager;
 9 import StudentSystem.StudentManagerHelper;
10 
11 public class client {
12 
13     public static void main(String[] args) {
14         try{
15             String[] init = {"-ORBInitialPort","1234"};
16             ORB orb = ORB.init(init, null);
17             org.omg.CORBA.Object obRef = orb
18                     .resolve_initial_references("NameService");
19             NamingContext ncRef = NamingContextHelper.narrow(obRef);
20             NameComponent nc = new NameComponent("StudentManager", "");
21             NameComponent path[] = { nc };
22             StudentManager sm = StudentManagerHelper.narrow(ncRef.resolve(path));
23             System.out.println(sm.QueryStudentStatus(12));
24             
25         }catch(Exception e){
26             System.out.println("Calling CORBA fails." + e);
27         }
28 
29     }
30 
31 }
View Code

 

 项目结构图如下:

然后,通过相应命令启动服务,执行代码得出运行结果。

 

 

 

 

 

 

 

 

 

 

 

posted @ 2016-05-04 00:19  小z1情调  阅读(1099)  评论(0编辑  收藏  举报