Corba第一个例子,VisiBroker实现
1、 编写接口Bank.idl
module Bank{
interface Account{¢
float balance();
};
interface AccountManager{
Account open(in string name);
};
};
2、用idl2java命令编译Bank.idl
命令:idl2java Bank.idl
命令:javac -classpath c:/bdp/lib/vbjorb.jar *.java
会生成Bank文件夹及相关的Java文件,然后编译这些文件,c:/bdp为VisiBroker的安装路径。
3、实现Server.java
import org.omg.PortableServer.*;
import Bank.*;
import java.util.*;
public class Server {
public static void main(String[] args) {
try {
Properties properties=new Properties();
properties.setProperty("vbroker.agent.addr", "localhost");
properties.setProperty("vbroker.agent.port", "14000");
properties.put("org.omg.CORBA.ORBClass", "com.inprise.vbroker.orb.ORB");
properties.put("org.omg.CORBA.ORBSingletonClass", "com.inprise.vbroker.orb.ORBSingleton");
properties.put("javax.rmi.CORBA.StubClass", "com.inprise.vbroker.rmi.CORBA.StubImpl");
properties.put("javax.rmi.CORBA.UtilClass", "com.inprise.vbroker.rmi.CORBA.UtilImpl");
properties.put("javax.rmi.CORBA.PortableRemoteObjectClass", "com.inprise.vbroker.rmi.CORBA.PortableRemoteObjectImpl");?????
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, properties);
POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
org.omg.CORBA.Policy[] policies = {
rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT)
};
POA myPOA = rootPOA.create_POA( "bank_agent_poa", rootPOA.the_POAManager(),
policies );
AccountManagerImpl managerServant = new AccountManagerImpl();
byte[] managerId = "BankManager".getBytes();
rootPOA.the_POAManager().activate();
System.out.println(myPOA.servant_to_reference(managerServant) +
" is ready.");
orb.run();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
4、实现Client.java
import Bank.*;
import java.util.*;
public class Client {
public static void main(String[] args) {
Properties properties=new Properties();
properties.setProperty("vbroker.agent.addr", "localhost");
properties.setProperty("vbroker.agent.port", "14000");
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,properties);
byte[] managerId = "BankManager".getBytes();
Bank.AccountManager manager =
Bank.AccountManagerHelper.bind(orb, "/bank_agent_poa", managerId);
String name = args.length > 0 ? args[0] : " Tom ";
Bank.Account account = manager.open(name);
float balance = account.balance();
System.out.println("The balance in " + name + "'s account is $" + balance);
}
}
5、实现AccountImpl
import Bank.*;
public class AccountImpl extends Bank.AccountPOA {
public AccountImpl(float balance) {
_balance = balance;
}
public float balance() {
return _balance;
}
private float _balance;
}
6、实现AccountManagerImpl
import org.omg.PortableServer.*;
import Bank.*;
import java.util.*;
public class AccountManagerImpl extends Bank.AccountManagerPOA {
public synchronized Bank.Account open(String name) {
Bank.Account account = (Bank.Account) _accounts.get(name);
if(account == null) {
float balance = Math.abs(_random.nextInt()) % 100000 / 100f ;
AccountImpl accountServant = new AccountImpl(balance);
try {
account = Bank.AccountHelper.narrow(_default_POA().servant_to_reference(accountServant));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Created " + name + "'s account: " + account);
_accounts.put(name, account);
}
return account;
}
private Dictionary _accounts = new Hashtable();
private Random _random = new Random();
}
7、编译服务器与客户端
用javac *.java命令编译所有文件,生成class。
命令:javac -classpath c:/bdp/lib/vbjorb.jar;. *.java
8、运行SmartAgent
命令:osagent
9、运行服务器
命令:vbj Server或者start vbj Server,start用于开启一个新的窗口
10、运行客户端
命令:vbj Client
运行结果:The balance in Tom's account is $992.8,搞定。