J2EE之巅

 

Orbacus名字服务的使用

Orbacus IONA 提供的开源 COBRA 实现,但其相关技术文档去很不详尽,而且还有错误,以下是以举例的方式说明了如何使用其提供的名字服务。

1 启动名字服务

java -Dooc.orb.oa.endpoint="iiop --port 9998" -classpath OBNaming.jar;OB.jar com.ooc.CosNaming.Server

其中通过 ooc.orb.oa.endpoint="iiop --port 9998" 设置服务的端口

2 服务器段代码:

import helloorb.Hello;

import helloorb.Hello_Impl;

 

import org.omg.CosNaming.NameComponent;

import org.omg.CosNaming.NamingContextExt;

import org.omg.CosNaming.NamingContextExtHelper;

 

import com. ooc .CORBA.ORB;

import com.ooc.OBPortableServer.POA;

import com.ooc.OBPortableServer.POAHelper;

import com.ooc.OBPortableServer.POAManager;

 

public class ByNamingServer {

    public static void main(String[] args) {

       java.util.Properties props = System.getProperties();

       props.put( "org.omg.CORBA.ORBClass" , "com.ooc.CORBA.ORB" );

       props.put( "org.omg.CORBA.ORBSingletonClass" ,

              "com.ooc.CORBA.ORBSingleton" );

 

       org.omg.CORBA.ORB orb = null ;

       try {

           orb = ORB.init(args, props);

 

           org.omg.CORBA.Object poaObj = null ;

           try {

              poaObj = orb.resolve_initial_references( "RootPOA" );

           } catch (org.omg.CORBA.ORBPackage.InvalidName ex) {

              throw new RuntimeException();

           }

           POA rootPOA = POAHelper.narrow(poaObj);

           org.omg.PortableServer.POAManager manager = rootPOA

                  .the_POAManager();

 

           org.omg.CORBA.Object obj = null ;

           try {

              String nameService= "corbaloc::127.0.0.1:9998/NameService" ;

              obj = orb.string_to_object(nameService);

           } catch (Exception ex) {

              ex.printStackTrace();

              throw new RuntimeException();

           }

 

           if (obj == null ) {

              throw new RuntimeException();

 

           }

           NamingContextExt nc = null ;

           try {

               Hello_Impl helloImpl = new Hello_Impl();

                Hello hello = helloImpl._this(orb);

                NameComponent[] a2Name = new NameComponent[1];

                a2Name[0] = new NameComponent();

                 a2Name[0]. id = "HelloServer" ;

                 a2Name[0]. kind = "" ;

              nc = NamingContextExtHelper.narrow(obj);

              nc.rebind(a2Name, hello);

              manager.activate();

           } catch (Exception ex) {

              ex.printStackTrace();

              throw new RuntimeException();

           }

       } catch (Exception e1) {

           e1.printStackTrace();

       }

    }

}

 

3 客户端代码

import org.omg.CosNaming.NameComponent;

import org.omg.CosNaming.NamingContextExt;

import org.omg.CosNaming.NamingContextExtHelper;

 

import helloorb.Hello;

import helloorb.HelloHelper;

 

 

public class ByNamingClient {

    public static void main(String args[]) {

       java.util.Properties props = System.getProperties();

       props.put( "org.omg.CORBA.ORBClass" , "com.ooc.CORBA.ORB" );

       props.put( "org.omg.CORBA.ORBSingletonClass" ,

              "com.ooc.CORBA.ORBSingleton" );

 

       int status = 0;

       org.omg.CORBA.ORB orb = null ;

 

       try {

           orb = org.omg.CORBA.ORB.init(args, props);

           status = run(orb);

          

       } catch (Exception ex) {

           ex.printStackTrace();

           status = 1;

       }

 

       if (orb != null ) {

           try {

              orb.destroy();

           } catch (Exception ex) {

              ex.printStackTrace();

              status = 1;

           }

       }

 

       System.exit(status);

    }

 

    static int run(org.omg.CORBA.ORB orb) {

       org.omg.CORBA.Object obj = null ;

    /*  try {

           String refFile = "Hello.ref";

           java.io.BufferedReader in = new java.io.BufferedReader(

                  new java.io.FileReader(refFile));

           String ref = in.readLine();

           obj = orb.string_to_object(ref);

          

       } catch (java.io.IOException ex) {

           ex.printStackTrace();

           return 1;

       }*/

       try {

       obj = orb.string_to_object( "corbaloc::127.0.0.1:9998/NameService" );

       NamingContextExt nc = NamingContextExtHelper.narrow(obj);

    /*    NameComponent[] a2Name = new NameComponent[1];

             a2Name[0] = new NameComponent();

             a2Name[0].id = "HelloServer";

             a2Name[0].kind = "";*/

      

       Hello hello = HelloHelper.narrow(nc.resolve_str( "HelloServer" ));

       hello.say_hello();

       } catch (Exception e){

           e.printStackTrace();

       }

       return 0;

    }

}

posted on 2006-11-19 22:02 超越巅峰 阅读(1729) 评论(3)  编辑  收藏 所属分类: CORBA

评论

# re: Orbacus名字服务的使用 2007-09-14 00:19 ucs_2008

推荐一款功能强大的Corba模拟器UCS...不用不知道,用了才说妙...

Download site:
http://sourceforge.net/projects/ucs

UCS (Ultra Corba Simulator) is one more powerful corba client/servant simulator tool than other similar products(e.g. Telcopro's MtSim, or OpenFusion's Corba Explorer, or eaiBridge's CAST). It doesn't need idl-related helper class or IR service  回复  更多评论   

# re: Orbacus名字服务的使用 2009-11-24 23:04 fbin

java -Dooc.orb.oa.endpoint="iiop --port 9998" -classpath OBNaming.jar;OB.jar com.ooc.CosNaming.Server
为什么我在命令行下,出错呢  回复  更多评论   

# re: Orbacus名字服务的使用 2009-11-24 23:05 fbin

Exception in thread "main" java.lang.NoClassDefFoundError: com/ooc/CosNaming/Server  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(12)

随笔分类(54)

随笔档案(59)

文章分类(2)

文章档案(1)

相册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜