目录:
概述
开发步骤
创建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 文件修改为如下内容:
/*
* IDL helloworld demo
* author by micmiu.com
*/
module com
{
module micmiu
{
module idl
{
module hello
{
interface HelloService {
string sayHello(in string msg);
};
};
};
};
};
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
*/
modulecom
{
modulemicmiu
{
moduleidl
{
modulehello
{
interfaceHelloService{
stringsayHello(instringmsg);
};
};
};
};
};
选中创建好的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 文件,添加如下方法:
@Override
public String sayHello(String msg) {
System.out.println("[服务端] 接收的参数 : " + msg);
String ret = "Hi," + msg + " welcome to CORBA";
System.out.println("[服务端] 返回信息 : " + ret);
return ret;
}
1
2
3
4
5
6
7
@Override
publicStringsayHello(Stringmsg){
System.out.println("[服务端] 接收的参数 : "+msg);
Stringret="Hi,"+msg+" welcome to CORBA";
System.out.println("[服务端] 返回信息 : "+ret);
returnret;
}
【4】、创建客户端
在 src 文件夹上右键依次选择 New → Other… → CORBA Wizard → Client→ Simple implementation 如下图:
点击Next ,弹出新的对话框,如下图:
IDL filename :选中创建的Hello.idl
Interface :选择IDL文件里定义的接口名
Package :输入包名
Client classesname :客户端类的名称
点击 Finish 按钮完成客户端类的初步创建,把客户端类
HelloServiceClientImpl.java 中的main方法修改成如下:
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();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
publicstaticvoidmain(String[]args){
try{
HelloServiceClientImpltest=newHelloServiceClientImpl();
// test.getORBInterface().operation1("A message in the bottle...");
Stringret=test.getORBInterface().sayHello("micmiu.com");
System.out.println("[客户端] 调用结果 : "+ret);
test.shutdown();
}catch(IOExceptionex){
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代码修改
注释调以下代码:
//PrintWriter ps = new PrintWriter(new FileOutputStream(new File("server.ior")));
//ps.println(orb.object_to_string(obj));
//ps.close();
1
2
3
//PrintWriter ps = new PrintWriter(new FileOutputStream(new File("server.ior")));
//ps.println(orb.object_to_string(obj));
//ps.close();
取消以下代码注释:
// ---- 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
// ---- Uncomment below to enable Naming Service access. ----
org.omg.CORBA.Objectncobj=orb.resolve_initial_references("NameService");
NamingContextExtnc=NamingContextExtHelper.narrow(ncobj);
nc.bind(nc.to_name("MyServerObject"),obj);
导入相关类的引用。
修改后完整代码如下:
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();
}
}
}
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
packagecom.micmiu.idl.server;
importjava.util.Properties;
importorg.omg.CosNaming.NamingContextExt;
importorg.omg.CosNaming.NamingContextExtHelper;
importorg.omg.PortableServer.IdAssignmentPolicyValue;
importorg.omg.PortableServer.LifespanPolicyValue;
importorg.omg.PortableServer.POA;
importorg.omg.PortableServer.POAHelper;
importorg.omg.PortableServer.ThreadPolicyValue;
publicclassServer_AOM{
publicstaticvoidmain(String[]args){
Propertiesprops=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.ORBorb=org.omg.CORBA.ORB.init(args,props);
// get a reference to the root POA
org.omg.CORBA.Objectobj=orb.resolve_initial_references("RootPOA");
POApoaRoot=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
POApoa=poaRoot.create_POA("HelloServiceServerImpl_poa",poaRoot.the_POAManager(),policies);
// Create the servant
HelloServiceServerImplservant=newHelloServiceServerImpl();
// 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.Objectncobj=orb.resolve_initial_references("NameService");
NamingContextExtnc=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(Exceptionex){
ex.printStackTrace();
}
}
}
第二步:客户端 HelloServiceClientImpl.java代码的修改
在main 方法中注释调如下代码:
//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
//LineNumberReader input = new LineNumberReader(new FileReader(
//"server.ior"));
//String ior = input.readLine();
//org.omg.CORBA.Object obj = orb.string_to_object(ior);
取消下面代码的注释:
// ---- 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
// ---- Uncomment below to enable Naming Service access. ----
org.omg.CORBA.Objectncobj=orb.resolve_initial_references("NameService");
NamingContextExtnc=NamingContextExtHelper.narrow(ncobj);
org.omg.CORBA.Objectobj=nc.resolve_str("MyServerObject");
并且修改成如下:
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");
1
2
3
4
org.omg.CORBA.Objectncobj=
orb.string_to_object("corbaloc::1.2@127.0.0.1:1234/NameService");
NamingContextExtnc=NamingContextExtHelper.narrow(ncobj);
org.omg.CORBA.Objectobj=nc.resolve_str("MyServerObject");
并且把客户端中 IOException 全部改成 Exception。
ps: “corbaloc::1.2@127.0.0.1:1234/NameService” 这个和服务端启动的参数-ORBInitRef NameService 一致.
修改后客户端的完整代码如下:
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();
}
}
}
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
packagecom.micmiu.idl.client;
/**
* The client implementation is generated by the ORB Studio.
*/
importjava.io.IOException;
importjava.util.Properties;
importorg.omg.CosNaming.NamingContextExt;
importorg.omg.CosNaming.NamingContextExtHelper;
classHelloServiceClientImpl{
privatecom.micmiu.idl.hello.HelloServicetarget=null;
privateorg.omg.CORBA.ORBorb=null;
/**
* Constructor for HelloServiceClientImpl
*
* @throws IOException
*/
publicHelloServiceClientImpl()throwsException{
initORB(null);
}
/**
* Constructor for HelloServiceClientImpl
*
* @throws IOException
* @see java.lang.Object#Object()
*/
publicHelloServiceClientImpl(String[]args)throwsException{
initORB(args);
}
/**
* Initialize ORB.
*
* @param args
* @throws IOException
*/
publicvoidinitORB(String[]args)throwsException{
Propertiesprops=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.Objectncobj=
orb.string_to_object("corbaloc::1.2@127.0.0.1:1234/NameService");
NamingContextExtnc=NamingContextExtHelper.narrow(ncobj);
org.omg.CORBA.Objectobj=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
*/
publiccom.micmiu.idl.hello.HelloServicegetORBInterface(){
returntarget;
}
/**
* Shutdown ORB.
*/
publicvoidshutdown(){
orb.shutdown(true);
}
/**
* Test driver for HelloServiceClientImpl.
*
* @param args
*/
publicstaticvoidmain(String[]args){
try{
HelloServiceClientImpltest=newHelloServiceClientImpl();
// test.getORBInterface().operation1("A message in the bottle...");
Stringret=test.getORBInterface().sayHello("micmiu.com");
System.out.println("[客户端] 调用结果 : "+ret);
test.shutdown();
}catch(Exceptionex){
ex.printStackTrace();
}
}
}
第三步:测试命名服务实现远程调用
先启动命名服务的监听,以端口 1234 为例:
%OPENORB_HOME%/NamingService/bin/ins -ORBPort=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 测试调用,运行结果和上面本地调用测试的结果一样。