到http://www.jacorb.org/download.html下载JacORB3.0Beta。
下面开发一个简单的应用。
Event Service => http://blog.csdn.net/fw0124/article/details/7192305
Notification Service => http://blog.csdn.net/fw0124/article/details/7197609
首先需要定义IDL文件。例如:
module learning {
module corba {
module bookstore {
struct Author {
string firstName;
string lastName;
};
struct Book {
string name;
double price;
};
typedef sequence<Book> BookList;
struct SearchCondition {
string name;
any value;
};
exception NotFound{};
interface BookQuery {
void search(in SearchCondition condition, out BookList bookList)
raises (NotFound);
};
interface ClientCallback;
interface CallbackServer {
void registerCallback(in ClientCallback cc);
void callback_notifyMsg(in string msg);
};
interface ClientCallback {
void notifyMsg(in string msg);
};
};
};
};
在Eclipse里面,可以通过下面的XML配置执行ANT Build编译IDL文件。
<project name="common-demo" default="idl">
<property name="idl.dir" value="." />
<property name="gen.dir" value="../../.." />
<property name="resources.dir" value="resources" />
<property name="jacorb.dir" value="../../../../../jacorb-3.0beta1" />
<path id="jacorb.classpath" >
<fileset dir="${jacorb.dir}/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="idl.taskdef">
<taskdef name="jacidl" classname="org.jacorb.idl.JacIDL" classpathref="jacorb.classpath"/>
</target>
<target name="idl" depends="idl.taskdef">
<jacidl srcdir="${idl.dir}" destdir="${gen.dir}" includes="*.idl" helpercompat="jacorb"
includepath="${jacorb.dir}/idl/omg" />
</target>
</project>
对于struct,生成3个文件。以Author为例,
Author.java
AuthorHelper.java -> 用来从Any对象中extract出Author对象/向Any对象中insert Author对象
AuthorHolder.java -> 用于输出参数
如果定义了sequence,还会得到*ListHelper.java和*ListHolder.java文件。
对于interface,除了上面的3个文件,还生成3个文件。以BookQuery为例:
BookQueryOperations.java ->定义我们的interface
BookQueryPOA.java ->BookQuery服务的提供者(Servant),我们的代码需要继承这个类
BookQueryPOATie.java ->如果我们的服务实现类没有没有继承BookQueryPOA类,直接实现BookQueryOperations接口,需要通过BookQueryPOATie来转换为一个Servant对象。
接下来可以分别进行Server端和Client端的编码。
Server
Server端的一般步骤如下:
1)新建1个类,继承自*POA类。在其中实现所有提供的接口。
接下来在Server端的main方法中,
2)调用ORB.init()这个静态方法,初始化ORB并得到一个ORB对象。
3)在ORB对象上调用resolve_initial_references("RootPOA"))方法,获得root POA,然后激活它。或者创建1个新的POA,激活这个POA。
4) 在ORB对象上调用resolve_initial_references("NameService"))方法,获得NameService的引用。
5)创建我们的服务提供对象,向NameService注册,注册时提供一个服务名。
6)调用ORB对象的run方法。
Client
Client端的一般步骤如下:
在Client端的main方法中,
1)调用ORB.init()这个静态方法,初始化ORB并得到一个ORB对象。
2) 在ORB对象上调用resolve_initial_references("NameService"))方法,获得NameService的引用。
3)提供服务名,向NameService查询并得到服务提供者的代理。
4)调用服务。
Any对象
对于Any对象,首先使用ORB对象的create_any方法生成1个Any对象,然后可以使用*Helper类的extract/insert方法抽取/插入一个对象。
可以使用Any对象的type方法获得这个Any对象中存放的对象的类型。
回调
可以通过CORBA实现回调。
实际上回调等同于Client提供一个服务,Server端调用Client提供的服务。
因此,客户端需要实现回调服务,并且需要获得root POA,然后激活它。或者创建1个新的POA,激活这个POA。
下面是提供的例子的代码。
运行此例子的步骤:
1)启动NameService。
Windows上到JacORB的安装目录下的bin,执行以下命令来启动NameService。需要指定一个端口,这里是7980。
注意,如果启动中出现错误,可能需要修改etc/jacorb.properties文件中的某些设置。
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;../lib/slf4j-api-1.6.4.jar
set CLASSPATH=%CLASSPATH%;../lib/slf4j-jdk14-1.6.4.jar
set CLASSPATH=%CLASSPATH%;../lib/jacorb.jar
set CLASSPATH=%CLASSPATH%;../lib/jacorb-services.jar
set SYSTEM_PROPS=-Djacorb.home=..
set SYSTEM_PROPS=%SYSTEM_PROPS% -Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB
set SYSTEM_PROPS=%SYSTEM_PROPS% -Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton
set SYSTEM_PROPS=%SYSTEM_PROPS% -DOAPort=7980
java %SYSTEM_PROPS% -cp %CLASSPATH% org.jacorb.naming.NameServer
2)分别启动服务端和客户端,需要指定如下的JVM参数:
-Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB
-Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton
-DORBInitRef.NameService=corbaloc::localhost:7980/NameService
Server代码
1)BookQueryImpl.java
package learning.corba.bookstore.server;
import org.omg.CORBA.TCKind;
import learning.corba.bookstore.Author;
import learning.corba.bookstore.AuthorHelper;
import learning.corba.bookstore.Book;
import learning.corba.bookstore.BookListHolder;
import learning.corba.bookstore.BookQueryPOA;
import learning.corba.bookstore.NotFound;
import learning.corba.bookstore.SearchCondition;
public class BookQueryImpl extends BookQueryPOA {
public void search(SearchCondition condition, BookListHolder bookList)
throws NotFound {
if (condition.name.equals("author")
&& condition.value.type().kind().value() == TCKind._tk_struct
&& condition.value.type().equivalent(AuthorHelper.type())) {
Author author = AuthorHelper.extract(condition.value);
StringBuilder sb = new StringBuilder(128);
sb.append("Search Condition:");
sb.append(condition.name);
sb.append("=");
sb.append(author.firstName);
sb.append(" ");
sb.append(author.lastName);
System.out.println(sb.toString());
}
Book[] books = new Book[2];
books[0] = new Book("Thinking in C++", 35.62);
books[1] = new Book("Thinking in Java", 59.87);
bookList.value = books;
}
}
2)CallbackServerImpl.java
package learning.corba.bookstore.server;
import learning.corba.bookstore.CallbackServerOperations;
import learning.corba.bookstore.ClientCallback;
public class CallbackServerImpl implements CallbackServerOperations {
public void registerCallback(ClientCallback cc) {
clientCallback = cc;
}
public void callback_notifyMsg(String msg) {
clientCallback.notifyMsg(msg);
}
private ClientCallback clientCallback;
}
3)BookStoreServer.java
package learning.corba.bookstore.server;
import java.util.Properties;
import learning.corba.bookstore.CallbackServerPOATie;
import org.omg.BiDirPolicy.BIDIRECTIONAL_POLICY_TYPE;
import org.omg.BiDirPolicy.BOTH;
import org.omg.BiDirPolicy.BidirectionalPolicyValueHelper;
import org.omg.CORBA.Any;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Policy;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.ImplicitActivationPolicyValue;
import org.omg.PortableServer.LifespanPolicyValue;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
public class BookStoreServer {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put("org.omg.PortableInterceptor.ORBInitializerClass.bidir_init",
"org.jacorb.orb.giop.BiDirConnectionInitializer");
ORB orb = ORB.init(args, props);
POA rootPoa = POAHelper.narrow(orb
.resolve_initial_references("RootPOA"));
rootPoa.the_POAManager().activate();
BookQueryImpl bookQueryServant = new BookQueryImpl();
NamingContextExt ncRef = NamingContextExtHelper.narrow(orb
.resolve_initial_references("NameService"));
ncRef.rebind(ncRef.to_name("BookStore.BookQuery"), rootPoa
.servant_to_reference(bookQueryServant));
Policy[] policies = new Policy[4];
Any any = orb.create_any();
BidirectionalPolicyValueHelper.insert (any, BOTH.value);
policies[0] = rootPoa.create_lifespan_policy (LifespanPolicyValue.TRANSIENT);
policies[1] = rootPoa.create_id_assignment_policy (IdAssignmentPolicyValue.SYSTEM_ID);
policies[2] = rootPoa.create_implicit_activation_policy (ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION);
policies[3] = orb.create_policy (BIDIRECTIONAL_POLICY_TYPE.value, any);
POA biPoa = rootPoa.create_POA("BiPOA", rootPoa.the_POAManager(), policies);
biPoa.the_POAManager().activate();
CallbackServerImpl callbackServerServant = new CallbackServerImpl();
CallbackServerPOATie callbackServerPOATie = new CallbackServerPOATie(callbackServerServant);
ncRef.rebind(ncRef.to_name("BookStore.CallbackServer"), rootPoa.servant_to_reference(callbackServerPOATie));
orb.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client代码
1)ClientCallbackPOA.java
package learning.corba.bookstore.client;
import learning.corba.bookstore.ClientCallbackPOA;
public class ClientCallbackImpl extends ClientCallbackPOA {
public void notifyMsg(String msg) {
System.out.println("Callback message from server: " + msg);
}
}
2)BookStoreClient.java
package learning.corba.bookstore.client;
import java.util.Properties;
import learning.corba.bookstore.Author;
import learning.corba.bookstore.AuthorHelper;
import learning.corba.bookstore.Book;
import learning.corba.bookstore.BookListHolder;
import learning.corba.bookstore.BookQuery;
import learning.corba.bookstore.BookQueryHelper;
import learning.corba.bookstore.CallbackServer;
import learning.corba.bookstore.CallbackServerHelper;
import learning.corba.bookstore.ClientCallback;
import learning.corba.bookstore.ClientCallbackHelper;
import learning.corba.bookstore.SearchCondition;
import org.omg.BiDirPolicy.BIDIRECTIONAL_POLICY_TYPE;
import org.omg.BiDirPolicy.BOTH;
import org.omg.BiDirPolicy.BidirectionalPolicyValueHelper;
import org.omg.CORBA.Any;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Policy;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.ImplicitActivationPolicyValue;
import org.omg.PortableServer.LifespanPolicyValue;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
public class BookStoreClient {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put("org.omg.PortableInterceptor.ORBInitializerClass.bidir_init",
"org.jacorb.orb.giop.BiDirConnectionInitializer");
ORB orb = ORB.init(args, props);
NamingContextExt ncExt = NamingContextExtHelper.narrow(
orb.resolve_initial_references("NameService"));
BookQuery bookQuery = BookQueryHelper.narrow(
ncExt.resolve_str("BookStore.BookQuery"));
SearchCondition condition = new SearchCondition();
condition.name = "author";
condition.value = orb.create_any();
AuthorHelper.insert(condition.value, new Author("Bill", "Gates"));
BookListHolder bookListHolder = new BookListHolder();
bookQuery.search(condition, bookListHolder);
Book[] books = bookListHolder.value;
System.out.println("Search Result:");
for (Book book : books) {
System.out.println(book.name + " " + book.price);
}
POA rootPoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
Policy[] policies = new Policy[4];
Any any = orb.create_any();
BidirectionalPolicyValueHelper.insert(any, BOTH.value);
policies[0] = rootPoa.create_lifespan_policy(LifespanPolicyValue.TRANSIENT);
policies[1] = rootPoa.create_id_assignment_policy(
IdAssignmentPolicyValue.SYSTEM_ID);
policies[2] = rootPoa.create_implicit_activation_policy(
ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION);
policies[3] = orb.create_policy (BIDIRECTIONAL_POLICY_TYPE.value, any);
POA biPoa = rootPoa.create_POA("BiPOA", rootPoa.the_POAManager(), policies);
biPoa.the_POAManager().activate();
ClientCallback cc = ClientCallbackHelper.narrow(
biPoa.servant_to_reference(new ClientCallbackImpl()));
CallbackServer cs = CallbackServerHelper.narrow(
ncExt.resolve_str("BookStore.CallbackServer"));
cs.registerCallback(cc);
cs.callback_notifyMsg("Hello!");
} catch (Exception e) {
e.printStackTrace();
}
}
}