走在架構師的大道上 Jack.Wang's home

          Java, C++, linux c, C#.net 技術,軟件架構,領域建模,IT 項目管理 Dict.CN 在線詞典, 英語學習, 在線翻譯

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

          公告

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

          淘寶店:新開淘寶書店
          致謝:
           感謝雷老師幾年的指導
           感謝導師在學業上的關懷,
           感謝老婆的支持,
           感謝我的同學和同事,
           在我成長的路上有你

          留言簿(26)

          我參與的團隊

          隨筆分類(232)

          隨筆檔案(190)

          我的鄰居們

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

                 1)首先要熟悉idl語言,這個是專門進行接口設計的語言,它與java沒關系,有自己的語法,具體的規則需要大家自己再網上研究,這里不多說了(或者訪問如下網站詳細察看http://www.iona.com/support/docs/manuals/orbix/33/html/orbix33cxx_pguide/IDL.html)。

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

          這 里定義了一個簡單的interface, 將其保存為hello.idl, 然后再dos命令框里面輸入 idlj.exe -fall hello.idl 編譯。之后會出現一個叫做HelloApp的目錄,corba就是通過這個目錄里面的類來進行c-s之間的數據溝通。

                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的目錄。編譯這個文件就不多說了。

                 3)還記得在hello中定義的interface嗎?我們需要對自己定義的接口中的方法進行實現,因此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 //必須繼承這個類,在helloApp目錄中已自動生成

           {
            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


          這個文件最好放在一個新建的目錄,已表示和server有區別,放在一起也沒有關系。如果分開的話,記著把HelloApp這個目錄復制到client的目錄來。

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

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

          7)啟動服務器:java HelloServer –ORBInitialPort 1234 –ORBInitialHost localhost

          8)啟動客戶端:java HelloClient –ORBInitialPort 1234 –ORBInitialHost localhost

          9)嚴格執行上述過程是應該直接成功的。 已經經過測試。

          10)然后再仔細研究這段代碼,你就會發現corba的奧秘了。
           From: http://www.javaeye.com/topic/136691



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

          Feedback

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

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

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

          corba 大約還會在電信網管產品中使用,IIOP 是由 corba 衍生的一種協議,用于 Internet 的互操作協議。現在企業應用基本見不著 corba 的影子了。

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

          # re: Java Corba 2008-04-27 08:26 Jack.Wang
          學習了  回復  更多評論
            

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

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

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

          # 你們懂不懂corba?? 2009-03-15 13:32 thui
          首先corba是一個協議,和語言和平臺無關,
          不是很了解corba 。不過以前一個產品用了corba ,用了2年不是很穩定。后都改成了C++-------這句話一看就是外行

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

          # re: Java Corba 2009-06-05 14:24 wm
          @thui
          1.Corba不會過時,國內用的少,可能幾乎沒有,就感覺過時了,這是一個誤導現象。
          2.國外電信產品中使用比較廣泛,原因因為它是一個相對比較穩定的產品,一般C或者C++的通信用的比較多;還有因為國外的電信產品,他組成比較復雜,一部分是Java一部分是C/C++,那怎么通信呢?當然是Corba,因為它是跨平臺跨語言的。  回復  更多評論
            

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

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

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

          主站蜘蛛池模板: 抚远县| 嫩江县| 卢龙县| 大方县| 临邑县| 亳州市| 嘉定区| 鹿泉市| 吉木萨尔县| 内乡县| 宝清县| 沁水县| 北流市| 土默特右旗| 紫云| 潼关县| 肇东市| 仁怀市| 垣曲县| 进贤县| 金平| 杭州市| 洱源县| 平顺县| 崇阳县| 古浪县| 乐陵市| 平和县| 博兴县| 苍山县| 宁波市| 芜湖县| 昌乐县| 射阳县| 宁河县| 乌苏市| 红原县| 义乌市| 昌图县| 安宁市| 芦溪县|