目录:
- 概述
- 开发步骤
- 创建Java项目,配置Lib
- 创建IDL文件
- 创建服务单
- 创建客户端
- 测试本地调用
- NamingService命名服务实现远程调用
[一]、概述
本文主要是图文介绍 Eclipse+OpenORB 开发CORBA应用的详细步骤,以供初学者借鉴。有关Eclipe中CORBA开发环境的配置详见:
http://www.micmiu.com/opensource/corba/corba-eclipse-env-config/
[二]、开发步骤
【1】、创建Java项目,配置lib
在Eclipe中首先创建一个Java Project 取名为:Corba-demo;在刚创建好的项目上右击,选择 Build Path → Configure Build Path … ,然后在右侧选中页签 Libraries ,点击 Add Library… 添加已经配置好的OpenORB相关的lib库,如下图:
【2】、创建IDL文件
在 src 文件夹上右键依次选择 New → Other… → CORBA Wizard → IDL files → Simple IDL 如下图:
点击 Next 按钮,在File Name:输入Hello.idl 如下图:
点击 Finish 即可。
Hello.idl 文件修改为如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* * IDL helloworld demo * author by micmiu.com */ module com { module micmiu { module idl { module hello { interface HelloService { string sayHello(in string msg); }; }; }; }; }; |
选中创建好的Hello.idl文件,右键 ORB Menu → Compile,就自动编译生成Java文件,类似下图:
【3】、创建服务端
在 src 文件夹上右键依次选择 New → Other… → CORBA Wizard → Server → Active object map 如下图:
点击Next ,弹出新的对话框,如下图:
- IDL filename :选中创建的Hello.idl
- Interface :选择IDL文件里定义的接口名
- Package :输入包名
- Server classesname :服务端类的名称
然后点击 Next,在弹出对话框里选中 Create server class 项,如下图:
点击Finish完成服务端类的初步创建,然后需要在生成的服务端类中添加还未具体实现的方法,
打开 HelloServiceServerImpl.java 文件,添加如下方法:
1 2 3 4 5 6 7 |
@Override public String sayHello(String msg) { System.out.println("[服务端] 接收的参数 : " + msg); String ret = "Hi," + msg + " welcome to CORBA"; System.out.println("[服务端] 返回信息 : " + ret); return ret; } |
【4】、创建客户端
在 src 文件夹上右键依次选择 New → Other… → CORBA Wizard → Client → Simple implementation 如下图:
点击Next ,弹出新的对话框,如下图:
- IDL filename :选中创建的Hello.idl
- Interface :选择IDL文件里定义的接口名
- Package :输入包名
- Client classesname :客户端类的名称
点击 Finish 按钮完成客户端类的初步创建,把客户端类
HelloServiceClientImpl.java 中的main方法修改成如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void main(String[] args) { try { HelloServiceClientImpl test = new HelloServiceClientImpl(); // test.getORBInterface().operation1("A message in the bottle..."); String ret = test.getORBInterface().sayHello("micmiu.com"); System.out.println("[客户端] 调用结果 : " + ret); test.shutdown(); } catch (IOException ex) { ex.printStackTrace(); } } |
【5】、测试本地调用
首先运行服务端:Server_AOM.java 然后再运行客户端类:HelloServiceClientImpl.java
服务端日志如下:
CORBA Server ready…
[服务端] 接收的参数 : micmiu.com
[服务端] 返回信息 : Hi,micmiu.com welcome to CORBA
客户端日志如下:
[客户端] 调用结果 : Hi,micmiu.com welcome to CORBA
【6】.NamingService 命名服务实现远程调用
第一步: 服务端Server_AOM.java代码修改
注释调以下代码:
1 2 3 |
//PrintWriter ps = new PrintWriter(new FileOutputStream(new File("server.ior"))); //ps.println(orb.object_to_string(obj)); //ps.close(); |
取消以下代码注释:
1 2 3 4 |
// ---- Uncomment below to enable Naming Service access. ---- org.omg.CORBA.Object ncobj = orb.resolve_initial_references("NameService"); NamingContextExt nc = NamingContextExtHelper.narrow(ncobj); nc.bind(nc.to_name("MyServerObject"), obj); |
导入相关类的引用。
修改后完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.micmiu.idl.server; import java.util.Properties; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; import org.omg.PortableServer.IdAssignmentPolicyValue; import org.omg.PortableServer.LifespanPolicyValue; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; import org.omg.PortableServer.ThreadPolicyValue; public class Server_AOM { public static void main(String[] args) { Properties props = System.getProperties(); //props.setProperty("org.omg.CORBA.ORBClass", "org.openorb.CORBA.ORB"); //props.setProperty("org.omg.CORBA.ORBSingletonClass", "org.openorb.CORBA.ORBSingleton"); // OpenORB 1.4.X props.setProperty("org.omg.CORBA.ORBClass", "org.openorb.orb.core.ORB"); props.setProperty("org.omg.CORBA.ORBSingletonClass", "org.openorb.orb.core.ORBSingleton"); try { // Initialize the ORB. org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, props); // get a reference to the root POA org.omg.CORBA.Object obj = orb.resolve_initial_references("RootPOA"); POA poaRoot = POAHelper.narrow(obj); // Create policies for our persistent POA org.omg.CORBA.Policy[] policies = { poaRoot.create_lifespan_policy(LifespanPolicyValue.PERSISTENT), poaRoot.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID), poaRoot.create_thread_policy(ThreadPolicyValue.ORB_CTRL_MODEL) }; // Create myPOA with the right policies POA poa = poaRoot.create_POA("HelloServiceServerImpl_poa", poaRoot.the_POAManager(), policies); // Create the servant HelloServiceServerImpl servant = new HelloServiceServerImpl(); // Activate the servant with the ID on myPOA byte[] objectId = "AnyObjectID".getBytes(); poa.activate_object_with_id(objectId, servant); // Activate the POA manager poaRoot.the_POAManager().activate(); // Get a reference to the servant and write it down. obj = poa.servant_to_reference(servant); // ---- Uncomment below to enable Naming Service access. ---- org.omg.CORBA.Object ncobj = orb.resolve_initial_references("NameService"); NamingContextExt nc = NamingContextExtHelper.narrow(ncobj); nc.bind(nc.to_name("MyServerObject"), obj); //PrintWriter ps = new PrintWriter(new FileOutputStream(new File("server.ior"))); //ps.println(orb.object_to_string(obj)); //ps.close(); System.out.println("CORBA Server ready..."); // Wait for incoming requests orb.run(); } catch(Exception ex) { ex.printStackTrace(); } } } |
第二步:客户端 HelloServiceClientImpl.java 代码的修改
在main 方法中注释调如下代码:
1 2 3 4 |
//LineNumberReader input = new LineNumberReader(new FileReader( // "server.ior")); //String ior = input.readLine(); //org.omg.CORBA.Object obj = orb.string_to_object(ior); |
取消下面代码的注释:
1 2 3 4 |
// ---- Uncomment below to enable Naming Service access. ---- org.omg.CORBA.Object ncobj = orb.resolve_initial_references("NameService"); NamingContextExt nc = NamingContextExtHelper.narrow(ncobj); org.omg.CORBA.Object obj = nc.resolve_str("MyServerObject"); |
并且修改成如下:
1 2 3 4 |
org.omg.CORBA.Object ncobj = orb.string_to_object("corbaloc::1.2@127.0.0.1:1234/NameService"); NamingContextExt nc = NamingContextExtHelper.narrow(ncobj); org.omg.CORBA.Object obj = nc.resolve_str("MyServerObject"); |
并且把客户端中 IOException 全部改成 Exception。
ps: “corbaloc::1.2@127.0.0.1:1234/NameService” 这个和服务端启动的参数-ORBInitRef NameService 一致.
修改后客户端的完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
package com.micmiu.idl.client; /** * The client implementation is generated by the ORB Studio. */ import java.io.IOException; import java.util.Properties; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; class HelloServiceClientImpl { private com.micmiu.idl.hello.HelloService target = null; private org.omg.CORBA.ORB orb = null; /** * Constructor for HelloServiceClientImpl * * @throws IOException */ public HelloServiceClientImpl() throws Exception { initORB(null); } /** * Constructor for HelloServiceClientImpl * * @throws IOException * @see java.lang.Object#Object() */ public HelloServiceClientImpl(String[] args) throws Exception { initORB(args); } /** * Initialize ORB. * * @param args * @throws IOException */ public void initORB(String[] args) throws Exception { Properties props = System.getProperties(); // props.setProperty("org.omg.CORBA.ORBClass", "org.openorb.CORBA.ORB"); // props.setProperty("org.omg.CORBA.ORBSingletonClass", // "org.openorb.CORBA.ORBSingleton"); // OpenORB 1.4.X props.setProperty("org.omg.CORBA.ORBClass", "org.openorb.orb.core.ORB"); props.setProperty("org.omg.CORBA.ORBSingletonClass", "org.openorb.orb.core.ORBSingleton"); // Initialize the ORB orb = org.omg.CORBA.ORB.init((String[]) args, props); // ---- Uncomment below to enable Naming Service access. ---- org.omg.CORBA.Object ncobj = orb.string_to_object("corbaloc::1.2@127.0.0.1:1234/NameService"); NamingContextExt nc = NamingContextExtHelper.narrow(ncobj); org.omg.CORBA.Object obj = nc.resolve_str("MyServerObject"); // LineNumberReader input = new LineNumberReader(new FileReader( // "server.ior")); // String ior = input.readLine(); // org.omg.CORBA.Object obj = orb.string_to_object(ior); target = com.micmiu.idl.hello.HelloServiceHelper.narrow(obj); } /** * Obtain ORB Interface. * * @return */ public com.micmiu.idl.hello.HelloService getORBInterface() { return target; } /** * Shutdown ORB. */ public void shutdown() { orb.shutdown(true); } /** * Test driver for HelloServiceClientImpl. * * @param args */ public static void main(String[] args) { try { HelloServiceClientImpl test = new HelloServiceClientImpl(); // test.getORBInterface().operation1("A message in the bottle..."); String ret = test.getORBInterface().sayHello("micmiu.com"); System.out.println("[客户端] 调用结果 : " + ret); test.shutdown(); } catch (Exception ex) { ex.printStackTrace(); } } } |
第三步:测试命名服务实现远程调用
先启动命名服务的监听,以端口 1234 为例:
1 |
%OPENORB_HOME%/NamingService/bin/ins -ORBPort=1234 |
micmiu-mbp:OpenORB-1.4.0 micmiu$ NamingService/bin/ins -ORBPort=1234
[main] [INFO ] (ins): NameService 1.4.0 Copyright (c) 2002-2005 The Community OpenORB
[main] [INFO ] (ins): calling ORB.init
[main] [INFO ] (ins): Service started. Press CTRL-C to stop the service!
然后启动服务端:Server_AOM.java 需要配置启动参数:
-ORBInitRef NameService=corbaloc::1.2@127.0.0.1:1234/NameService
最后启动客户端 HelloServiceClientImpl.java 测试调用,运行结果和上面本地调用测试的结果一样。
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/opensource/corba/corba-openorb-dev-demo/
写的很详细,顶!d=====( ̄▽ ̄*)b
另外想问下命名服务和ior两种方式有什么不同,
有没有事件的实例
楼主,麻烦问一下 Server_AOM.java 这个是怎么生成的
不好意思 看错了
LZ你好,你这网站风格看着很舒适。请问主要是用什么框架写的呢
这个是php的 wordpress, 找一些自己喜欢得主题即可,一步一步操作即可