一、原理和概念
1)CORBA独立于任何编程语言,独立于操作系统
2)采用客户端和服务端模式,客户端和服务端分别配置存根和框架代理
3)ORB(Object Request Broker)充当客户端与服务端之间的中间件
4)IIOP(Internet Inter-Broker Protocol)网络ORB交换协议,不同的ORB通过IIOP通信
5)IDL(Interface Define Language),CORBA采用专门的IDL语言来定义接口。
二、创建CORBA程序的步骤:
1)使用IDL语言定义接口
2)编译接口,生成用目标语言编写的存根类以及其他辅助类的源文件。(java中通过bin/idij.exe来编译,编译后生成POA,Stub,Helper,Holder,Operations)
3)用目标语言来编写接口的实现类(继承*POA)
4)编写服务端程序,创建和注册CORBA对象
5)编写客户端程序,获取CORBA的远程调用
三、服务端创建注册CORBA的步骤
//1.创建和初始化ORB
ORB orb = ORB.init(args, null);
//2.获得根POA的引用,并且激活POAManager
POA rootpoa =POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
//3.创建一个HelloServiceImpl对象,并且把它与ORB关联
HelloServiceImpl helloServiceImpl = new HelloServiceImpl();
helloServiceImpl.setORB(orb);
//4.获得HelloServiceImpl对象的CORBA类型的对象引用
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloServiceImpl);
HelloService href = HelloServiceHelper.narrow(ref);
//5.获得命名服务的Context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
//6.把HelloService对象与“HelloService”名字绑定
String name = "HelloService";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
System.out.println("HelloServer ready and waiting ...");
//7.等待客户端访问HelloService对象
orb.run();
四、客户端调用步骤
//1.创建和初始化ORB
ORB orb = ORB.init(args, null);
//2.获得命名服务的Context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
//3.获得名为“HelloService”的HelloService对象的远程引用
String name = "HelloService";
helloServiceImpl = HelloServiceHelper.narrow(ncRef.resolve_str(name));
//4.调用HelloService对象的远程方法
System.out.println("Obtained a handle on server object: " + helloServiceImpl);
System.out.println(helloServiceImpl.sayHello());
helloServiceImpl.shutdown();