qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java中RMI遠程調用

           Java遠程方法調用,即Java RMI(Java Remote Method Invocation)是Java編程語言里,一種用于實現遠程過程調用的應用程序編程接口。它使客戶機上運行的程序可以調用遠程服務器上的對象。遠程方法調用特性使Java編程人員能夠在網絡環境中分布操作。RMI全部的宗旨就是盡可能簡化遠程接口對象的使用。
            Java RMI極大地依賴于接口。在需要創建一個遠程對象的時候,程序員通過傳遞一個接口來隱藏底層的實現細節。客戶端得到的遠程對象句柄正好與本地的根代碼連接,由后者負責透過網絡通信。這樣一來,程序員只需關心如何通過自己的接口句柄發送消息。
            服務端新建接口:
          import java.rmi.Remote;
          import java.rmi.RemoteException;
          public interface RmiTestInterface extends Remote{
          public String getTest() throws RemoteException;
          }
            接口的實現:
          import java.rmi.RemoteException;
          public class RmiTestImpl implements RmiTestInterface {
          public RmiTestImpl() throws RemoteException {
          //super();
          // TODO Auto-generated constructor stub
          //UnicastRemoteObject.exportObject(this);
          }
          /**
          *
          */
          public String getTest() throws RemoteException {
          // TODO Auto-generated method stub
          return "Hello,Test";
          }
          }
            定義一個main方法,注冊你已經實現的RMI接口,包括開放端口等:
          public static void main(String []args) throws AlreadyBoundException, RemoteException{
          RmiTestImpl t=new RmiTestImpl();
          RmiTestInterface tt=(RmiTestInterface)UnicastRemoteObject.exportObject(t,0);
          // Bind the remote object's stub in the registry
          Registry registry = LocateRegistry.createRegistry(2001);
          registry.rebind("test", tt);
          System.out.println("server is start");
          }
           Server端的代碼已經全部寫完,但是還要把接口類(RmiTestInterface)打包成jar,導入進client端的項目中。
            運行服務端后控制臺輸出:
            server is start
            導入服務端的接口jar以后,可以開始編寫一個client端的主程序:
          public static void main(String []args){
          try {
          Registry registry = LocateRegistry.getRegistry("localhost",2001);
          RmiTestInterface t= (RmiTestInterface) registry.lookup("test");
          System.out.println("client:"+t.getTest());
          } catch (RemoteException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          } catch (NotBoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
          }
            運行客戶端main方法后,控制臺輸出:
            Hello,Test
          English »
           
          English »
           

          posted on 2014-06-09 10:36 順其自然EVO 閱讀(153) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2014年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 凯里市| 株洲县| 楚雄市| 新邵县| 孝义市| 三穗县| 新余市| 临颍县| 阳城县| 东兰县| 察雅县| 阳泉市| 铁力市| 梨树县| 沾化县| 鹤峰县| 岐山县| 拉萨市| 仙居县| 阿城市| 丰镇市| 尼木县| 麻城市| 永济市| 昆明市| 梨树县| 得荣县| 公主岭市| 甘德县| 禹城市| 化德县| 上虞市| 万山特区| 天镇县| 肥城市| 德庆县| 崇信县| 禄劝| 洛浦县| 乌恰县| 旬阳县|