CORBA java命名服务示例

corba本来就是与语言无关的,这里为方便测试,client和server都用java编程。客户端可以使用服务端生成的IOR文件与服务端通信,这里使用更简单的,客户端和服务端使用NameService机制。

1、首先编写idl

module HelloApp{
 interface Hello{
  string sayHello();
  oneway void shutdown();
 };
};

关于idl的详细介绍,请参考相关资料。实际上,和java的接口有一定的类似之处。

然后使用idlj.exe -fall hello.idl会生成服务端存根代码和客户端桩代码。直接执行idlj.exe可显示更详细的参数

2、服务端代码

 

import java.util.Properties; import org.omg.CORBA.ORB; import org.omg.CORBA.ORBPackage.InvalidName; import org.omg.CosNaming.NameComponent; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; import org.omg.CosNaming.NamingContextPackage.CannotProceed; import org.omg.CosNaming.NamingContextPackage.NotFound; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAManagerPackage.AdapterInactive; import org.omg.PortableServer.POAPackage.ServantNotActive; import org.omg.PortableServer.POAPackage.WrongPolicy; import HelloApp.Hello; import HelloApp.HelloHelper; import HelloApp.HelloPOA; public class HelloServer { public static void main(String args[]){ //initialize the orb Properties pro = new Properties(); pro.setProperty("org.omg.CORBA.ORBInitialPort", "1234"); pro.setProperty("org.omg.CORBA.ORBInitialHost", "localhost"); ORB orb = ORB.init(args,pro); //get rootpoa,active poamanager try { POA rootpoa = (POA)orb.resolve_initial_references("RootPOA"); rootpoa.the_POAManager().activate(); //create servant and register it with the orb HelloImp helloimp = new HelloImp(); helloimp._this(orb); //get object reference from the servant org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloimp); //and cast the reference to a CORBA reference Hello href = HelloHelper.narrow(ref); // org.omg.CORBA.Object nameobj = orb.resolve_initial_references("NameService"); NamingContextExt nc = NamingContextExtHelper.narrow(nameobj); String name = "Hello"; NameComponent[] ncs = nc.to_name(name); nc.rebind(ncs, href); System.out.println("start run orb..."); orb.run(); } catch (InvalidName e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AdapterInactive e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ServantNotActive e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WrongPolicy e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NotFound e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CannotProceed e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class HelloImp extends HelloPOA{ @Override public String sayHello() { System.out.println("invoke sayHello start..."); return "hello ,i have received"; } @Override public void shutdown() { System.out.println("start shutdown..."); } }

 

3、客户端代码

import java.util.Properties; import org.omg.CORBA.ORB; import org.omg.CORBA.ORBPackage.InvalidName; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; import org.omg.CosNaming.NamingContextPackage.CannotProceed; import org.omg.CosNaming.NamingContextPackage.NotFound; import HelloApp.Hello; import HelloApp.HelloHelper; public class Client { /** * @param args */ public static void main(String[] args) { Properties pro = new Properties(); pro.setProperty("org.omg.CORBA.ORBInitialPort", "1234"); pro.setProperty("org.omg.CORBA.ORBInitialHost", "localhost"); ORB orb = ORB.init(args, pro); try { org.omg.CORBA.Object ns = orb.resolve_initial_references("NameService"); NamingContextExt nc = NamingContextExtHelper.narrow(ns); org.omg.CORBA.Object href = nc.resolve_str("Hello"); Hello hello = HelloHelper.narrow(href); String ret = hello.sayHello(); System.out.println("client:get ret{"+ret); } catch (InvalidName e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NotFound e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CannotProceed e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

4、运行服务端程序之前首先运行NameService服务程序

orbd.exe -ORBInitialPort 1234

然后运行服务端程序和客户端程序,输出如下:

服务端

start run orb...
invoke sayHello start...

客户端

client:get ret{hello ,i have received

 

你可能感兴趣的:(程序开发)