走在架構(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

                 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 編譯。之后會出現(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目錄中已自動生成

           {
            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要保持一致),啟動corba服務(wù)。

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

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

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

          10)然后再仔細(xì)研究這段代碼,你就會發(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 閱讀(8936) 評論(9)  編輯  收藏 所屬分類: 開發(fā)技術(shù)

          Feedback

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

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

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

          corba 大約還會在電信網(wǎng)管產(chǎn)品中使用,IIOP 是由 corba 衍生的一種協(xié)議,用于 Internet 的互操作協(xié)議。現(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é)議,和語言和平臺無關(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不會過時(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)樗强缙脚_跨語言的。  回復(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ù)  更多評論
            

          主站蜘蛛池模板: 都昌县| 阳春市| 犍为县| 洪洞县| 商都县| 泸水县| 东港市| 青岛市| 长寿区| 汉阴县| 山西省| 肇源县| 拉孜县| 武强县| 大厂| 姜堰市| 微山县| 万安县| 同江市| 泽州县| 平定县| 临城县| 两当县| 汉源县| 青州市| 海安县| 长治市| 筠连县| 龙川县| 靖宇县| 措勤县| 磐石市| 阳江市| 包头市| 遂平县| 沙湾县| 襄汾县| 太和县| 普格县| 黄骅市| 阳泉市|