我有一个定义的idl文件,如下所示:
module Banking {
typedef string Transactions[5];
typedef long AccountId;
interface Account {
exception InsufficientFunds {};
readonly attribute double balance;
long lodge(in double amount);
long withdraw(in double amount) raises (InsufficientFunds);
readonly attribute Transactions transactions;
};
interface Bank {
long accountCount();
double totalMoney();
Account account(in AccountId accNr);
};
};
我用idlj编译。我已经定义了一个BankServant,由客户端用来与服务器通信,我有一个工作程序,几乎所有方法都实现了。我唯一的问题是我不知道如何实现account(in AccountId accNr)
方法,而方法又会返回正确的Account对象。因为我不知道CORBA而我只是在帮朋友,所以我想请一些解决方案/例子/教程,它可以帮助我破解一个简单但工作阶级的布局来处理这种情况。
提前谢谢。
答案 0 :(得分:1)
这实际上取决于您用于POA(便携式对象适配器)的策略。假设您在服务器中使用RootPOA,则必须:
为Account对象创建一个实现对象。我在银行服务员的名字中看到的通常称为AccountImpl
或AccountServant
。
AccountServant as = new AccountServant(accNr);
您必须在POA中注册该对象。这再次与您为POA选择的政策有关。使用默认的根POA:
org.omg.CORBA.Object o = rootPOA.servant_to_reference( as );
使用生成的Account
IDL编译器将其缩小为正确的AccountHelper
类型:
Account acc = AccountHelper.narrow(o);
返回
return acc;
此代码假定您已为AccountServant
java对象编写了一个构造函数,该对象接受该帐号作为其第一个参数。您还必须为BankServant
提供对要在其中注册新创建的Account
对象的POA的引用。
有很多教程。例如,请参阅this one,因为POA的选项集非常多,需要一本书来解释所有这些:)。