走在架構(gòu)師的大道上 Jack.Wang's home

          Java, C++, linux c, C#.net 技術(shù),軟件架構(gòu),領(lǐng)域建模,IT 項(xiàng)目管理 Dict.CN 在線詞典, 英語(yǔ)學(xué)習(xí), 在線翻譯

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            195 Posts :: 3 Stories :: 728 Comments :: 0 Trackbacks

                 1)首先要熟悉idl語(yǔ)言,這個(gè)是專門進(jìn)行接口設(shè)計(jì)的語(yǔ)言,它與java沒(méi)關(guān)系,有自己的語(yǔ)法,具體的規(guī)則需要大家自己再網(wǎng)上研究,這里不多說(shuō)了(或者訪問(wèn)如下網(wǎng)站詳細(xì)察看http://www.iona.com/support/docs/manuals/orbix/33/html/orbix33cxx_pguide/IDL.html)。

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

          這 里定義了一個(gè)簡(jiǎn)單的interface, 將其保存為hello.idl, 然后再dos命令框里面輸入 idlj.exe -fall hello.idl 編譯。之后會(huì)出現(xiàn)一個(gè)叫做HelloApp的目錄,corba就是通過(guò)這個(gè)目錄里面的類來(lái)進(jìn)行c-s之間的數(shù)據(jù)溝通。

                2)下一步,就是我們的server端:

          // A server for the Hello object

          import HelloApp.*;
          import org.omg.CosNaming.*;
          import org.omg.CosNaming.NamingContextPackage.*;
          import org.omg.CORBA.*;
          import org.omg.PortableServer.*;
          import org.omg.PortableServer.POA;
          import java.util.Properties;

          public class HelloServer {

            public static void main(String args[]) {
              try{
                // create and initialize the ORB
                ORB orb = ORB.init(args, null);

                // get reference to rootpoa & activate the POAManager
                POA rootpoa =
                  (POA)orb.resolve_initial_references("RootPOA");
                rootpoa.the_POAManager().activate();

                // create servant and register it with the ORB
                HelloImpl helloImpl = new HelloImpl();
                helloImpl.setORB(orb);

                // get object reference from the servant
                org.omg.CORBA.Object ref =
                  rootpoa.servant_to_reference(helloImpl);


                // and cast the reference to a CORBA reference
                Hello href = HelloHelper.narrow(ref);
            
                // get the root naming context
                // NameService invokes the transient name service
                org.omg.CORBA.Object objRef =
                    orb.resolve_initial_references("NameService");
                // Use NamingContextExt, which is part of the
                // Interoperable Naming Service (INS) specification.
                NamingContextExt ncRef =
                  NamingContextExtHelper.narrow(objRef);

                // bind the Object Reference in Naming
                String name = "Hello1";
                NameComponent path[] = ncRef.to_name( name );
                ncRef.rebind(path, href);

                System.out.println
                  ("HelloServer ready and waiting ...");

                // wait for invocations from clients
                orb.run();
              }
           
                catch (Exception e) {
                  System.err.println("ERROR: " + e);
                  e.printStackTrace(System.out);
                }
            
                System.out.println("HelloServer Exiting ...");
           
            } //end main
          } // end class

          將其保存為HelloServer.java.放在剛才的hello.idl的目錄。編譯這個(gè)文件就不多說(shuō)了。

                 3)還記得在hello中定義的interface嗎?我們需要對(duì)自己定義的接口中的方法進(jìn)行實(shí)現(xiàn),因此HelloImp.java

          // The servant -- object implementation -- for the Hello
          // example.  Note that this is a subclass of HelloPOA, whose
          // source file is generated from the compilation of
          // Hello.idl using j2idl.

          import HelloApp.*;
          import org.omg.CosNaming.*;
          import org.omg.CosNaming.NamingContextPackage.*;
          import org.omg.CORBA.*;
          import org.omg.PortableServer.*;
          import org.omg.PortableServer.POA;

          import java.util.Properties;

          class HelloImpl extends HelloPOA //必須繼承這個(gè)類,在helloApp目錄中已自動(dòng)生成

           {
            private ORB orb;

            public void setORB(ORB orb_val) {
              orb = orb_val;
            }
             
            // implement sayHello() method
              public String sayHello()
              {
           return "\nHello world !!\n";
              }
             
            // implement shutdown() method
            public void shutdown() {
              orb.shutdown(false);
            }
          } //end class
          同樣放在server所在目錄中。

                4)接下來(lái)是客戶端(HelloClient.java):

          // A sample Java IDL object client application.
          import HelloApp.*;
          import org.omg.CosNaming.*;
          import org.omg.CosNaming.NamingContextPackage.*;
          import org.omg.CORBA.*;

          public class HelloClient
          {
            static Hello helloImpl;
            String [] x=new String[6];
            public static void main(String args[]){
               try{
                  // create and initialize the ORB
                ORB orb = ORB.init(args, null);

                  System.out.println("ORB initialised\n");

                  // get the root naming context
                  org.omg.CORBA.Object objRef =
                orb.resolve_initial_references("NameService");

                  // Use NamingContextExt instead of NamingContext,
                  // part of the Interoperable naming Service. 
                  NamingContextExt ncRef =
                    NamingContextExtHelper.narrow(objRef);
           
                  // resolve the Object Reference in Naming
                  String name = "Hello1";
                  helloImpl =
                    HelloHelper.narrow(ncRef.resolve_str(name));

                  System.out.println
                    ("Obtained a handle on server object: "
                      + helloImpl);
                  System.out.println(helloImpl.sayHello());
                  helloImpl.shutdown();

             }
               catch (Exception e) {
                  System.out.println("ERROR : " + e) ;
                e.printStackTrace(System.out);
             }
            } //end main

          } // end class


          這個(gè)文件最好放在一個(gè)新建的目錄,已表示和server有區(qū)別,放在一起也沒(méi)有關(guān)系。如果分開的話,記著把HelloApp這個(gè)目錄復(fù)制到client的目錄來(lái)。

          5)好啦!已經(jīng)可以開始爽了,我們編譯所有的java文件

          6)再dos窗口輸入orbd.exe –ORBInitialPort 1234(端口號(hào)可以自定義,但是記得s-c要保持一致),啟動(dòng)corba服務(wù)。

          7)啟動(dòng)服務(wù)器:java HelloServer –ORBInitialPort 1234 –ORBInitialHost localhost

          8)啟動(dòng)客戶端:java HelloClient –ORBInitialPort 1234 –ORBInitialHost localhost

          9)嚴(yán)格執(zhí)行上述過(guò)程是應(yīng)該直接成功的。 已經(jīng)經(jīng)過(guò)測(cè)試。

          10)然后再仔細(xì)研究這段代碼,你就會(huì)發(fā)現(xiàn)corba的奧秘了。
           From: http://www.javaeye.com/topic/136691



          本博客為學(xué)習(xí)交流用,凡未注明引用的均為本人作品,轉(zhuǎn)載請(qǐng)注明出處,如有版權(quán)問(wèn)題請(qǐng)及時(shí)通知。由于博客時(shí)間倉(cāng)促,錯(cuò)誤之處敬請(qǐng)諒解,有任何意見(jiàn)可給我留言,愿共同學(xué)習(xí)進(jìn)步。
          posted on 2008-04-26 21:13 Jack.Wang 閱讀(8943) 評(píng)論(9)  編輯  收藏 所屬分類: 開發(fā)技術(shù)

          Feedback

          # re: Java Corba 2008-04-26 22:25 隔葉黃鶯
          第一行是 idl 語(yǔ)言,不是 idlj 語(yǔ)言,接口定義語(yǔ)言
          jdk 提供的 orb 比較簡(jiǎn)陋,學(xué)習(xí)用用,實(shí)際項(xiàng)目都會(huì)選別的 orb 產(chǎn)口。我原來(lái)用的好像是 orabcus。corba 規(guī)范定義了一系列的服務(wù),如 命名、事件、通知、生命、持久性對(duì)象、事務(wù)服務(wù)等。

          corba 中的對(duì)象理解起來(lái)并不是那么容易,它當(dāng)初由 OMG 組織提出是為異種平臺(tái)、語(yǔ)言進(jìn)行通行。但是由 idl 對(duì)于每一種語(yǔ)言要映射生成獨(dú)有的一套接口代碼來(lái)也是很煩的。

          然而現(xiàn)在一切都變得簡(jiǎn)單多了,有 webservice,就是語(yǔ)言平臺(tái)無(wú)關(guān)性的東西,還不象 corba 那樣要考慮防火墻,所以現(xiàn)在大熱的是 SOA、SCA、ESB 等。

          corba 大約還會(huì)在電信網(wǎng)管產(chǎn)品中使用,IIOP 是由 corba 衍生的一種協(xié)議,用于 Internet 的互操作協(xié)議。現(xiàn)在企業(yè)應(yīng)用基本見(jiàn)不著 corba 的影子了。

          說(shuō)真的,很多的程序員都還沒(méi)用過(guò) EJB,也不知道 EJB 長(zhǎng)什么樣子的,Hibernate/Spring 橫空出世了。更別提 cobar 那個(gè)老古懂。
            回復(fù)  更多評(píng)論
            

          # re: Java Corba 2008-04-27 08:26 Jack.Wang
          學(xué)習(xí)了  回復(fù)  更多評(píng)論
            

          # re: Java Corba 2008-04-30 13:16 java-he
          不是很了解corba 。不過(guò)以前一個(gè)產(chǎn)品用了corba ,用了2年不是很穩(wěn)定。后都改成了C++。  回復(fù)  更多評(píng)論
            

          # re: Java Corba 2009-02-05 23:35 gavin.zheng
          糾正一下
          首先:目前常用的rpc有以下幾種, corba rmi soap(soa)
          1,corba最高效,使用的是c編碼解碼,而且現(xiàn)在所有的服務(wù)器間通信基本上都是它
          2,以前的rmi最快,但不能不同種服務(wù)器通信,因?yàn)樗莻髡b的jvm內(nèi)部數(shù)據(jù)結(jié)構(gòu),為了能夠進(jìn)行任何語(yǔ)言通信,避免跟當(dāng)時(shí)的大部分企業(yè)產(chǎn)生分歧,它修改了它的地層協(xié)議,現(xiàn)在是封裝corba通信, 當(dāng)然 以前的協(xié)議照樣使用
          3,soap現(xiàn)在成了soa的事實(shí)標(biāo)準(zhǔn), 但是這只能作為不同公司直接進(jìn)行數(shù)據(jù)交流,xml解析是非常大的性能問(wèn)題,特別是在集群上,不可能允許使用soap,比如mysql jboss的集群引擎,全部是corba

          我想說(shuō)的是: corba不可能過(guò)時(shí),至少在目前,沒(méi)有任何技術(shù)在性能上可以超越它  回復(fù)  更多評(píng)論
            

          # 你們懂不懂corba?? 2009-03-15 13:32 thui
          首先corba是一個(gè)協(xié)議,和語(yǔ)言和平臺(tái)無(wú)關(guān),
          不是很了解corba 。不過(guò)以前一個(gè)產(chǎn)品用了corba ,用了2年不是很穩(wěn)定。后都改成了C++-------這句話一看就是外行

          1,corba最高效,使用的是c編碼解碼,而且現(xiàn)在所有的服務(wù)器間通信基本上都是它
          ---這句話也是,你調(diào)查過(guò),都用corba????胡說(shuō)八道  回復(fù)  更多評(píng)論
            

          # re: Java Corba 2009-06-05 14:24 wm
          @thui
          1.Corba不會(huì)過(guò)時(shí),國(guó)內(nèi)用的少,可能幾乎沒(méi)有,就感覺(jué)過(guò)時(shí)了,這是一個(gè)誤導(dǎo)現(xiàn)象。
          2.國(guó)外電信產(chǎn)品中使用比較廣泛,原因因?yàn)樗且粋€(gè)相對(duì)比較穩(wěn)定的產(chǎn)品,一般C或者C++的通信用的比較多;還有因?yàn)閲?guó)外的電信產(chǎn)品,他組成比較復(fù)雜,一部分是Java一部分是C/C++,那怎么通信呢?當(dāng)然是Corba,因?yàn)樗强缙脚_(tái)跨語(yǔ)言的。  回復(fù)  更多評(píng)論
            

          # re: Java Corba 2009-10-07 12:13 yinzhao0509
          博主,在1.6下調(diào)試通不過(guò),而且idlj.exe編譯出的文件也和文中有所不同!  回復(fù)  更多評(píng)論
            

          # re: Java Corba 2013-04-22 17:44 JAVAdel
          講的很好很清楚  回復(fù)  更多評(píng)論
            

          # re: Java Corba 2014-10-31 17:08 skc
          @java-he
          你好 能具體說(shuō)明一下,C++代替CORBA的原理嗎  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 修武县| 岗巴县| 科尔| 泰顺县| 读书| 洪雅县| 莱芜市| 绍兴县| 宣汉县| 香港 | 龙川县| 临潭县| 卢湾区| 永顺县| 玉环县| 库车县| 青岛市| 苍溪县| 洛隆县| 陇南市| 曲周县| 沙河市| 怀集县| 隆子县| 遂平县| 太康县| 蕲春县| 耒阳市| 颍上县| 额尔古纳市| 崇明县| 卓尼县| 南华县| 文成县| 达日县| 镶黄旗| 东至县| 武定县| 西乌珠穆沁旗| 元谋县| 大宁县|