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

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

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

          公告

          重構(gòu)
          新浪博客:新浪 blog
          MSN: wbjeasygo@163.com
          Email:  wbjeasygo@163.com
          QQ 精英群: 47763528 
          空間QQ空間

          淘寶店:新開淘寶書店
          致謝:
           感謝雷老師幾年的指導(dǎo)
           感謝導(dǎo)師在學(xué)業(yè)上的關(guān)懷,
           感謝老婆的支持,
           感謝我的同學(xué)和同事,
           在我成長的路上有你
          。

          留言簿(26)

          我參與的團(tuán)隊(duì)

          隨筆分類(232)

          隨筆檔案(190)

          我的鄰居們

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

                 1)首先要熟悉idl語言,這個(gè)是專門進(jìn)行接口設(shè)計(jì)的語言,它與java沒關(guān)系,有自己的語法,具體的規(guī)則需要大家自己再網(wǎng)上研究,這里不多說了(或者訪問如下網(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è)簡單的interface, 將其保存為hello.idl, 然后再dos命令框里面輸入 idlj.exe -fall hello.idl 編譯。之后會(huì)出現(xiàn)一個(gè)叫做HelloApp的目錄,corba就是通過這個(gè)目錄里面的類來進(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è)文件就不多說了。

                 3)還記得在hello中定義的interface嗎?我們需要對自己定義的接口中的方法進(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)接下來是客戶端(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ū)別,放在一起也沒有關(guān)系。如果分開的話,記著把HelloApp這個(gè)目錄復(fù)制到client的目錄來。

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

          6)再dos窗口輸入orbd.exe –ORBInitialPort 1234(端口號可以自定義,但是記得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í)行上述過程是應(yīng)該直接成功的。 已經(jīng)經(jīng)過測試。

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



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

          Feedback

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

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

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

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

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

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

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

          # 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)行任何語言通信,避免跟當(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解析是非常大的性能問題,特別是在集群上,不可能允許使用soap,比如mysql jboss的集群引擎,全部是corba

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

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

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

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

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

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

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

          主站蜘蛛池模板: 合作市| 宜丰县| 新野县| 泰宁县| 泰顺县| 甘孜县| 偏关县| 沂源县| 凤台县| 来安县| 敖汉旗| 巴塘县| 齐齐哈尔市| 铅山县| 巴楚县| 同德县| 松阳县| 阆中市| 元氏县| 桂平市| 潼关县| 抚松县| 南投县| 孟村| 永嘉县| 双牌县| 昆明市| 吉隆县| 蓬安县| 邛崃市| 新兴县| 乡城县| 南城县| 嘉祥县| 连云港市| 舒城县| 古浪县| 井研县| 涟水县| 大化| 中方县|