轉載過來的 java RMI 實例

chyx413332087 13年前發布 | 907 次閱讀

本實例為參考多篇文章寫就而成,網上及書上各類文章介紹如何使用RMI有多種實例可參考,譬如有:

1. 用命令rmiregistry啟動RMI注冊服務的
2. 同時創建存根(stub)和骨架(skeleton)的
3. 只創建存根類的的(jdk1.2以后版本)
4. 通過RemoteRef和rmi://協議字串方式的
5. 比較少講到的用LocateRegistry直接在代碼上啟動RMI注冊服務的。

以上描述并非明顯分類,比如,你總是可以選擇用rmiregistry或者代碼LocateRegistry啟動RMI注冊服務

下面我將介紹一個完整的實例,讓初學者能快速體驗RMI的功用。

分為以下四個步驟

1. 創建遠程接口及聲明遠程方法(HelloInterface.java)
2. 實現遠程接口及遠程方法(繼承UnicastRemoteObject)(Hello.java)
3. 啟動RMI注冊服務,并注冊遠程對象(HelloServer.java)
4. 客戶端查找遠程對象,并調用遠程方法(HelloClient)
5. 執行程序:啟動服務HelloServer;運行客戶端HelloClient進行調用

具體代碼及對應步驟如下:

1. 創建遠程接口及聲明遠程方法(HelloInterface.java)

<DIV class=code_title>java 代碼</DIV>

<DIV class=dp-highlighter>

<DIV class=bar>

<OL class=dp-j>

<LI class=alt>package com.unmi;   </SPAN> </LI>

  •   
  • <LI class=alt>import java.rmi.*;    </LI>

  •   
  • <LI class=alt>/**   </LI>

  •  * 遠程接口必須擴展接口java.rmi.Remote  
  • <LI class=alt> */  </SPAN> </LI>

  • public interface HelloInterface extends Remote   
  • <LI class=alt>{    </LI>

  •    /**  
  • <LI class=alt>    * 遠程接口方法必須拋出 java.rmi.RemoteException  </SPAN> </LI>

  •     */  
  • <LI class=alt>   <SPAN class=keyword>public</SPAN> String say() <SPAN class=keyword>throws</SPAN> RemoteException;    </LI>

  • }   
  • </OL></DIV>

    2. 實現遠程接口及遠程方法(繼承UnicastRemoteObject)Hello.java

    <DIV class=code_title>java 代碼</DIV>

    <DIV class=dp-highlighter>

    <DIV class=bar>

    <OL class=dp-j>

    <LI class=alt>package com.unmi;   </SPAN> </LI>

  •   
  • <LI class=alt>import java.rmi.*;    </LI>

  • import java.rmi.server.*;   
  • <LI class=alt>   </LI>

  • /**  
  • <LI class=alt> * 擴展了UnicastRemoteObject類,并實現遠程接口 HelloInterface  </SPAN> </LI>

  •  */  
  • <LI class=alt>public <SPAN class=keyword>class</SPAN> Hello <SPAN class=keyword>extends</SPAN> UnicastRemoteObject <SPAN class=keyword>implements</SPAN> HelloInterface    </LI>

  • {   
  • <LI class=alt>   <SPAN class=keyword>private</SPAN> String message;    </LI>

  •   
  • <LI class=alt>   <SPAN class=comment>/** </SPAN>  </LI>

  •     * 必須定義構造方法,即使是默認構造方法,也必須把它明確地寫出來,因為它必須拋出出RemoteException異常  
  • <LI class=alt>    */  </SPAN> </LI>

  •    public Hello(String msg) throws RemoteException   
  • <LI class=alt>   {    </LI>

  •       message = msg;   
  • <LI class=alt>   }    </LI>

  •   
  • <LI class=alt>   <SPAN class=comment>/** </SPAN>  </LI>

  •     * 遠程接口方法的實現  
  • <LI class=alt>    */  </SPAN> </LI>

  •    public String say() throws RemoteException   
  • <LI class=alt>   {    </LI>

  •       System.out.println("Called by HelloClient");   
  • <LI class=alt>      <SPAN class=keyword>return</SPAN> message;    </LI>

  •    }   
  • <LI class=alt>}    </LI></OL></DIV>

    3. 啟動RMI注冊服務,并注冊遠程對象(HelloServer.java)

    <DIV class=code_title>java 代碼</DIV>

    <DIV class=dp-highlighter>

    <DIV class=bar>

    <OL class=dp-j>

    <LI class=alt>package com.unmi;   </SPAN> </LI>

  •   
  • <LI class=alt>import java.rmi.Naming;    </LI>

  • import java.rmi.registry.LocateRegistry;   
  • <LI class=alt>   </LI>

  • public class HelloServer   
  • <LI class=alt>{    </LI>

  •    /**  
  • <LI class=alt>    * 啟動 RMI 注冊服務并進行對象注冊  </SPAN> </LI>

  •     */  
  • <LI class=alt>   <SPAN class=keyword>public</SPAN> <SPAN class=keyword>static</SPAN> <SPAN class=keyword>void</SPAN> main(String[] argv)    </LI>

  •    {   
  • <LI class=alt>      <SPAN class=keyword>try</SPAN>   </LI>

  •       {   
  • <LI class=alt>         <SPAN class=comment>//啟動RMI注冊服務,指定端口為1099 (1099為默認端口) </SPAN>   </LI>

  •          //也可以通過命令 $java_home/bin/rmiregistry 1099啟動   
  • <LI class=alt>         <SPAN class=comment>//這里用這種方式避免了再打開一個DOS窗口 </SPAN>   </LI>

  •          //而且用命令rmiregistry啟動注冊服務還必須事先用RMIC生成一個stub類為它所用   
  • <LI class=alt>         LocateRegistry.createRegistry(<SPAN class=number>1099</SPAN>);    </LI>

  •            
  • <LI class=alt>         <SPAN class=comment>//創建遠程對象的一個或多個實例,下面是hello對象 </SPAN>   </LI>

  •          //可以用不同名字注冊不同的實例   
  • <LI class=alt>         HelloInterface hello = <SPAN class=keyword>new</SPAN> Hello(<SPAN class=string>"Hello, world!"</SPAN>);    </LI>

  •            
  • <LI class=alt>         <SPAN class=comment>//把hello注冊到RMI注冊服務器上,命名為Hello </SPAN>   </LI>

  •          Naming.rebind("Hello", hello);   
  • <LI class=alt>             </LI>

  •          //如果要把hello實例注冊到另一臺啟動了RMI注冊服務的機器上   
  • <LI class=alt>         <SPAN class=comment>//Naming.rebind("http://192.168.1.105:1099/Hello",hello); </SPAN>   </LI>

  •            
  • <LI class=alt>         System.out.println(<SPAN class=string>"Hello Server is ready."</SPAN>);    </LI>

  •       }   
  • <LI class=alt>      <SPAN class=keyword>catch</SPAN> (Exception e)    </LI>

  •       {   
  • <LI class=alt>         System.out.println(<SPAN class=string>"Hello Server failed: "</SPAN> + e);    </LI>

  •       }   
  • <LI class=alt>   }    </LI>

  • }  
  • </OL></DIV>

    4. 客戶端查找遠程對象,并調用遠程方法(HelloClient)

    <DIV class=code_title>java 代碼</DIV>

    <DIV class=dp-highlighter>

    <DIV class=bar>

    <OL class=dp-j>

    <LI class=alt>package com.unmi;   </SPAN> </LI>

  •   
  • <LI class=alt>import java.rmi.Naming;    </LI>

  •   
  • <LI class=alt>public <SPAN class=keyword>class</SPAN> HelloClient    </LI>

  • {   
  • <LI class=alt>   <SPAN class=comment>/** </SPAN>  </LI>

  •     * 查找遠程對象并調用遠程方法  
  • <LI class=alt>    */  </SPAN> </LI>

  •    public static void main(String[] argv)   
  • <LI class=alt>   {    </LI>

  •       try  
  • <LI class=alt>      {    </LI>

  •          HelloInterface hello = (HelloInterface) Naming.lookup("Hello");   
  • <LI class=alt>             </LI>

  •          //如果要從另一臺啟動了RMI注冊服務的機器上查找hello實例   
  • <LI class=alt>         <SPAN class=comment>//HelloInterface hello = (HelloInterface)Naming.lookup("http://192.168.1.105:1099/Hello"); </SPAN>   </LI>

  •             
  • <LI class=alt>         <SPAN class=comment>//調用遠程方法 </SPAN>   </LI>

  •          System.out.println(hello.say());   
  • <LI class=alt>      }    </LI>

  •       catch (Exception e)   
  • <LI class=alt>      {    </LI>

  •          System.out.println("HelloClient exception: " + e);   
  • <LI class=alt>      }    </LI>

  •    }   
  • <LI class=alt>}    </LI>

  •   
  • </OL></DIV>

    5. 執行程序:啟動服務HelloServer;運行客戶端HelloClient進行調用

    代碼如何編譯這里就不細講

    (1)打開一個Dos窗口執行命令 java com.unmi.HelloServer 啟動服務HelloServer

    E:workspaceTestRMIbin>java com.unmi.HelloServer
    Hello Server is ready.

    運行成功則可以看到 Hello Server is ready

    (2)打開另一個Dos窗口執行命令 java com.unmi.HelloClient 運行客戶端程序

    E:workspaceTestRMIbin>java com.unmi.HelloClient
    Hello, world!

    調用成功則可以看到 Hello, world!

    并且在啟動服務端的窗口中看到緊跟 Hello Server is ready. 打印出
    Called by HelloClient

    如果您能一路順暢的執行到這里,恭喜!您已度過了一個輕快的RMI之旅。

    最后來個說明:

    本實例中并沒有用到JDK所帶的命令 rmic 編譯實現類得到存根(Stub)類,也沒用命令 rmiregistry 命令來啟動RMI注冊服務。在啟動 rmiregistry之前必須能讓它加載到相應的stub類,這就是造成**_Stub 類找不到的原因。

    如果只是按上面的代碼,則服務程序 HelloServer 和客戶端程序 HelloClient 都必須運行在本機(如此則RMI有何意義呢?);別急,只要修改HelloClient類,使用第二種形式的lookup查找語句,注釋第一條 lookup語句,取消注釋第二條lookup語句

             //HelloInterface hello = (HelloInterface) Naming.lookup("Hello");
           
             //如果要從另一臺啟動了RMI注冊服務的機器上查找hello實例
             HelloInterface hello = (HelloInterface)Naming.lookup("http://192.168.1.105:1099/Hello");

    其中的IP地址和端口號1099為 RMI 注冊服務器的IP和端口號,這樣你的HelloClient就可以在另一臺機器運行了,當然HelloInterface類必須能找到(但也可指定參數- Djava.rmi.server.codebase從網絡加載HelloInterface類)。lookup("Hello")默認為從本機 127.0.0.1的1099端口上查找Hello命令對象,如果第二條語句寫成lookup("192.168.1.105/Hello")與原語句是同等的,因為默認端口號就是1099。

    代碼中 HelloServer 和 HelloClient 省略了設置安全管理器的過程 System.setSecurityManager(new RMISecurityManager()); ,如果設置的安全管理則必須編寫相應的訪問策略文件,并且在執行時指定參數

    無論是啟動服務端還是客戶端都可以用參數 -Djava.rmi.server.codebase=http://unmi.blogcn.cn/bin 的形式,像JNP一樣從網絡上加載類,這樣更方便于RMI客戶端的部署,如RMI客戶端是一個Applet

    可以拿單獨一臺機器運行 rmiregistry (它需要能加載到相應的stub類,設置classpath)或用LocateRegistry.createRegistry(port),只作為 RMI遠程對象的RMI集中注冊的服務器,真正提供服務對象只往上注冊,客戶端只需從注冊服務器上查找遠程對象引用,然后調用遠程方法,具體由誰提供服務由注冊服務器來幫助聯絡。

    還可以用 RMI Activation 編程方式來實現RMI遠程方法調用,具體請參考 http://java.sun.com/j2se/1.4.2/docs/guide/rmi/activation.html

    把HelloServer和HelloClient中的 "http://192.168.1.105:1099/Hello 寫成 rmi:/192.168.1.105:1099/Hello 感覺會好看一些,因為直接感覺就是在處理rmi協議。

    </SPAN></SPAN></DIV></SPAN></SPAN></SPAN></DIV></SPAN></SPAN></SPAN></SPAN></DIV></SPAN></SPAN></SPAN></DIV>

     本文由用戶 chyx413332087 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!