首页 > 代码库 > RMI网络编程: 如何搭建基于JDK1.5的分布式JAVA RMI
RMI网络编程: 如何搭建基于JDK1.5的分布式JAVA RMI
1. 在Eclipse里面创建一个server 端的project。然后,创建一个接口,这个接口是你要向client端开放的方法定义。它叫做:UserManagerInterface,而且必须继承Remote接口。
1 package dataserver.rmi.stub; 2 3 import java.rmi.Remote; 4 import java.rmi.RemoteException; 5 6 import dataserver.rmi.bean.Account; 7 8 public interface UserManagerInterface extends Remote{ 9 public String getUserName() throws RemoteException; 10 public Account getAdminAccount() throws RemoteException; 11 }
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 { //super(); // TODO Auto-generated constructor stub //UnicastRemoteObject.exportObject(this); } /** * */ private static final long serialVersionUID = -3111492742628447261L; public String getUserName() throws RemoteException { // TODO Auto-generated method stub return "Tommy Lee"; } public Account getAdminAccount() throws RemoteException { // TODO Auto-generated method stub Account account=new Account(); account.setUsername("admin"); account.setPassword("admin"); return account; } }
4. 定义一个主程序入口,注册你已经实现的RMI接口,包括开放端口等。其实很简单:
把我们的接口名称,命名为“userManager”,方便client进行调用
1 package dataserver.entry; 2 3 import java.rmi.AlreadyBoundException; 4 import java.rmi.RemoteException; 5 import java.rmi.registry.LocateRegistry; 6 import java.rmi.registry.Registry; 7 import java.rmi.server.UnicastRemoteObject; 8 9 import dataserver.rmi.UserManagerImpl; 10 import dataserver.rmi.stub.UserManagerInterface; 11 12 public class Entry { 13 14 public static void main(String []args) throws AlreadyBoundException, RemoteException{ 15 16 17 UserManagerImpl userManager=new UserManagerImpl(); 18 UserManagerInterface userManagerI=(UserManagerInterface)UnicastRemoteObject.exportObject(userManager,0); 19 // Bind the remote object‘s stub in the registry 20 Registry registry = LocateRegistry.createRegistry(2001); 21 registry.rebind("userManager", userManagerI); 22 System.out.println("server is ready"); 23 } 24 }
5. Server端的代码已经全部写完,但是还要把bean类(Account)和接口类(UserMangerInterface)打包成jar,以便可以在下面导入进client端的项目中。
项目--》右键--》导出--》jar--》选择bean和interface--》命名为RmiServerInterface.jar--》finish
6. 开始创建client端的程序。新建一个project。创建完成后,把刚才jar包导入进client的项目中。
7. 导入我们的接口jar以后,可以开始编写一个client端的主程序,并调用server端的方法。
1 package weiblog.rmi; 2 import java.rmi.NotBoundException; 3 import java.rmi.RemoteException; 4 import java.rmi.registry.LocateRegistry; 5 import java.rmi.registry.Registry; 6 7 import dataserver.rmi.stub.UserManagerInterface; 8 9 public class Entry2 { 10 11 public static void main(String []args){ 12 13 try { 14 Registry registry = LocateRegistry.getRegistry("localhost",2001); 15 UserManagerInterface userManager = (UserManagerInterface) registry.lookup("userManager"); 16 System.out.println(""+userManager.getAdminAccount().getUsername() 17 +userManager.getAdminAccount().getPassword()); 18 } catch (RemoteException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } catch (NotBoundException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 26 } 27 }
8. 启动server端的主程序,然后启动client端的主程序。
server控制台打印:server is ready
client控制台打印:adminadmin
可以参考:http://www.cnblogs.com/end/archive/2012/11/16/2772812.html
RMI网络编程: 如何搭建基于JDK1.5的分布式JAVA RMI