1. 对象User、Person、TestModel1、TestModel2、ComplexModel
User.java
public class User implements java.io.Serializable{ private String id; private String userName; private String password; private int age; private List<String> friends; public String getid() { return id; } public void setId(String id) { this.id = id; } 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getFriends() { return friends; } public void setFriends(List<String> friends) { this.friends = friends; } }
Person.java
public class Person implements java.io.Serializable{ }
TestModel1.java
public class TestModel1 implements java.io.Serializable{ private User user; private String keys; private Map<String,String> params; public String getKeys() { return keys; } public void setKeys(String keys) { this.keys = keys; } public Map<String, String> getParams() { return params; } public void setParams(Map<String, String> params) { this.params = params; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
TestModel2.java
public class TestModel2 implements java.io.Serializable{ private User user; private int quantity; public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
ComplexModel.java
public class User implements java.io.Serializable{ private String id; private String userName; private String password; private int age; private List<String> friends; public String getid() { return id; } public void setId(String id) { this.id = id; } 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getFriends() { return friends; } public void setFriends(List<String> friends) { this.friends = friends; } }
2.接口 PeopleService 实现PeopleServiceImpl
PeopleService.java
public interface PeopleService extends Remote{ public String showMessage(String name) throws RemoteException; public User getUser() throws RemoteException; public ComplexModel getModel() throws RemoteException; public void compareModel(ComplexModel model) throws RemoteException; }
PeopleServiceImpl.java
public class PeopleServiceImpl extends UnicastRemoteObject implements PeopleService{ public PeopleServiceImpl() throws RemoteException { super(); // TODO Auto-generated constructor stub } public User getUser() throws RemoteException{ User result = new User(); result.setId("007"); result.setUserName("zhangsan"); result.setPassword("123456"); List<String> fs = new ArrayList<String>(); fs.add("sdsfdsfsd"); fs.add("sfsdfdsgsdf"); result.setFriends(fs); return result; } public ComplexModel getModel() throws RemoteException{ ComplexModel model = new ComplexModel(); User user = new User(); user.setId("007"); user.setUserName("zhangsan"); user.setPassword("123456"); List<String> fs = new ArrayList<String>(); fs.add("sdsfdsfsd"); fs.add("sfsdfdsgsdf"); user.setFriends(fs); TestModel1 m1 = new TestModel1(); m1.setUser(user); TestModel2 m2 = new TestModel2(); m2.setUser(user); model.setModel1(m1); model.setModel2(m2); System.out.println("....." + (model.getModel1().getUser() == model.getModel2().getUser())); return model; } public void compareModel(ComplexModel model) throws RemoteException{ System.out.println("....." + (model.getModel1().getUser() == model.getModel2().getUser())); } public String showMessage(String name) throws RemoteException{ // TODO Auto-generated method stub return null; } }
3 RMI绑定服务器 RMIServer
RMIServer.java
public class RMIServer { /** * @param args */ public static void main(String[] args) { try { LocateRegistry.createRegistry(9099); PeopleServiceImpl rmi = new PeopleServiceImpl(); try { Naming.bind("//127.0.0.1:9099/RMI_SERVER",rmi); System.out.println("rmi对象已经注册到服务器上"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (AlreadyBoundException e) { e.printStackTrace(); } } catch (RemoteException e) { e.printStackTrace(); } } }
4.RMI访问客户端 RMIClient
RMIClient.java
public class RMIClient { /** * @param args */ public static void main(String[] args) { for (int index = 0; index < 1000; index++) { try { PeopleService service = (PeopleService) Naming .lookup("//127.0.0.1:9099/RMI_SERVER"); ComplexModel cm = service.getModel(); System.out.println(cm.getModel1().getUser() == cm.getModel2() .getUser()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (NotBoundException e) { e.printStackTrace(); } } } }
5.服务端核心代码
注册端口 LocateRegistry.createRegistry(9099);
绑定远程对象 Naming.bind("//127.0.0.1:9099/RMI_SERVER",rmi);
6.客户端核心代码
访问远程对象 PeopleService service = (PeopleService) Naming
.lookup("//127.0.0.1:9099/RMI_SERVER");
7.测试结果: RMI能够实现不同版本的JVM之间传递复杂对象,并且能够传递引用参数。传输的对象必须实现java.io.Serializable接口。