生活随笔
收集整理的這篇文章主要介紹了
Java RMI(2):项目中使用RMI
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載地址:?http://6221123.blog.51cto.com/6211123/1112619?點擊打開鏈接
RMI網絡編程開發之二 如何搭建基于JDK1.5的分布式JAVA RMI 程序
2013-01-09 15:59:47
標簽:JAVA?分布式?網絡編程?JDK1.5?RMI
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章?原始出處?、作者信息和本聲明。否則將追究法律責任。http://6221123.blog.51cto.com/6211123/1112619
這里講述的是基于JDK1.5的RMI程序搭建,更簡單的說是一個 HelloWorld RMI。
1. 這里是基于JDK1.5的,節省了繁瑣的手工編譯(生成樁和骨架)。不像1.4之前的RMI。
2. 這里是把客戶端和服務器端的兩個程序,分布在兩個獨立的程序里面,而不是同一個package下面。是真正的分布式。
3. 這里不過多闡述原理,這只是一個Hello World!!
好,以下是步驟:
1. 在Eclipse里面創建一個server 端的project。然后,創建一個接口,這個接口是你要向client端開放的方法定義。它叫做:UserManagerInterface,而且必須繼承Remote接口。
package?dataserver.rmi.stub;??import?java.rmi.Remote;?import?java.rmi.RemoteException;??import?dataserver.rmi.bean.Account;??public?interface?UserManagerInterface?extends?Remote{?????public?String?getUserName()?throws?RemoteException;?????public?Account?getAdminAccount()?throws?RemoteException;?}? 2. 為了證明RMI中,“面向對象”或者是“無縫傳遞JAVA Object”是何等簡單,我們需要定義一個Account類,該類是一個Bean,必須實現implements Serializable序列化接口。這是一個可以在client和server傳輸的可序列化對象。
package?dataserver.rmi.bean;??import?java.io.Serializable;??public?class?Account?implements?Serializable,Cloneable{?????????????private?static?final?long?serialVersionUID?=?-1858518369668584532L;?????private?String?username;?????private?String?password;??????????public?String?getUsername()?{?????????return?username;?????}?????public?void?setUsername(String?username)?{?????????this.username?=?username;?????}?????public?String?getPassword()?{?????????return?password;?????}?????public?void?setPassword(String?password)?{?????????this.password?=?password;?????}??????
}?
3. 此時,需要實現你已經開放的接口:
package?dataserver.rmi;??import?java.rmi.RemoteException;??import?dataserver.rmi.bean.Account;?import?dataserver.rmi.stub.UserManagerInterface;??public?class?UserManagerImpl?implements?UserManagerInterface?{??????public?UserManagerImpl()?throws?RemoteException?{????????????????????????????????}?????????????private?static?final?long?serialVersionUID?=?-3111492742628447261L;??????public?String?getUserName()?throws?RemoteException?{??????????????????return?"Tommy?Lee";?????}??????public?Account?getAdminAccount()?throws?RemoteException?{??????????????????Account?account=new?Account();?????????account.setUsername("admin");?????????account.setPassword("admin");?????????return?account;?????}??}? 4. 定義一個主程序入口,注冊你已經實現的RMI接口,包括開放端口等。其實很簡單:
把我們的接口名稱,命名為“userManager”,方便client進行調用
package?dataserver.entry;??import?java.rmi.AlreadyBoundException;?import?java.rmi.RemoteException;?import?java.rmi.registry.LocateRegistry;?import?java.rmi.registry.Registry;?import?java.rmi.server.UnicastRemoteObject;??import?dataserver.rmi.UserManagerImpl;?import?dataserver.rmi.stub.UserManagerInterface;??public?class?Entry?{??????public?static?void?main(String?[]args)?throws?AlreadyBoundException,?RemoteException{???????????????????UserManagerImpl?userManager=new?UserManagerImpl();?????????????UserManagerInterface?userManagerI=(UserManagerInterface)UnicastRemoteObject.exportObject(userManager,0);??????????????????????????Registry?registry?=?LocateRegistry.createRegistry(2001);?????????????registry.rebind("userManager",?userManagerI);?????????????System.out.println("server?is?ready");?????}?}? ?
5. Server端的代碼已經全部寫完,但是還要把bean類(Account)和接口類(UserMangerInterface)打包成jar,以便可以在下面導入進client端的項目中。
項目--》右鍵--》導出--》jar--》選擇bean和interface--》命名為RmiServerInterface.jar--》finish
6.? 開始創建client端的程序。新建一個project。創建完成后,把剛才jar包導入進client的項目中。
7.? 導入我們的接口jar以后,可以開始編寫一個client端的主程序,并調用server端的方法。
package?weiblog.rmi;?import?java.rmi.NotBoundException;?import?java.rmi.RemoteException;?import?java.rmi.registry.LocateRegistry;?import?java.rmi.registry.Registry;??import?dataserver.rmi.stub.UserManagerInterface;??public?class?Entry2?{??????public?static?void?main(String?[]args){??????????????????try?{?????????????Registry?registry?=?LocateRegistry.getRegistry("localhost",2001);?????????????UserManagerInterface?userManager?=?(UserManagerInterface)?registry.lookup("userManager");?????????????System.out.println(""+userManager.getAdminAccount().getUsername()?????????????????????+userManager.getAdminAccount().getPassword());?????????}?catch?(RemoteException?e)?{??????????????????????????e.printStackTrace();?????????}?catch?(NotBoundException?e)?{??????????????????????????e.printStackTrace();?????????}??????????????}?}? 8. 啟動server端的主程序,然后啟動client端的主程序。
server控制臺打印:server is ready
client控制臺打印:adminadmin
大功告成!!
總結
以上是生活随笔為你收集整理的Java RMI(2):项目中使用RMI的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。