package com.danny.rim; import java.rmi.AlreadyBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; public class AppEntry { public static void main(String[] args) throws AlreadyBoundException, RemoteException { UserInfoImpl userInfoImpl = new UserInfoImpl(); //Export the remote object using the specific port provided to be able to receive incoming calls. UserInfoInterface userInfoInterface = (UserInfoInterface)UnicastRemoteObject.exportObject(userInfoImpl,0); //Create and export a Registry instance on the localhost that accepts requests from the specified port Registry registry = LocateRegistry.createRegistry(8080); //Bind a remote reference to the specified name in this registry. registry.rebind("UserInfo", userInfoInterface); System.out.println("The server is ready to be called at any time."); } }
package com.danny.rim; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class UserInfoImpl implements UserInfoInterface { public UserInfo getUserInfo() throws RemoteException { UserInfo userInfo = new UserInfo(); userInfo.setUser_id("admin"); userInfo.setUser_name("System Administrator"); userInfo.setUser_password("888888"); userInfo.setUser_email("[email protected]"); userInfo.setUser_dept("Information Suggestion Center"); return userInfo; } public String getUserName() throws RemoteException { return "System Administrator"; } public String getUserPassword() throws RemoteException { return "888888"; } /* * Get the basic information of the user, the return value of this method is a map */ public Map<String,String> getUserInfoMap() throws RemoteException{ Map<String,String> map = new HashMap<String, String>(); map.put("UserId", "admin"); map.put("UserPassword", "888888"); return map; } /* * Get the basic information of the user, the return value of this method is a List set of javabeans */ public List<UserInfo> getUserInfoListBean() throws RemoteException{ List<UserInfo> list = new ArrayList<UserInfo>(); UserInfo userOne = new UserInfo(); userOne.setUser_id("userOne"); userOne.setUser_name("User 111"); userOne.setUser_password("111111"); userOne.setUser_email("[email protected]"); userOne.setUser_dept("Administration Department"); list.add(userOne); UserInfo userTwo = new UserInfo(); userTwo.setUser_id("userTwo"); userTwo.setUser_name("用户222"); userTwo.setUser_password("222222"); userTwo.setUser_email("[email protected]"); userTwo.setUser_dept("Human Resources"); list.add(userTwo); UserInfo userThree = new UserInfo(); userThree.setUser_id("userThree"); userThree.setUser_name("User 333"); userThree.setUser_password("333333"); userThree.setUser_email("[email protected]"); userThree.setUser_dept("Sales"); list.add(userThree); return list; } /* * Get the basic information of the user, the return value of this method is a List set of maps */ public List<Map<String,String>> getCityInfoListMap() throws RemoteException{ List<Map<String,String>> list = new ArrayList<Map<String,String>>(); Map<String,String> mapSY = new HashMap<String, String>(); mapSY.put("cityName", "沈阳"); mapSY.put("cityArea", "东北"); list.add(mapSY); Map<String,String> mapBJ = new HashMap<String, String>(); mapBJ.put("cityName", "北京"); mapBJ.put("cityArea", "华北"); list.add(mapBJ); Map<String,String> mapSH = new HashMap<String, String>(); mapSH.put("cityName", "上海"); mapSH.put("cityArea", "华东"); list.add(mapSH); Map<String,String> mapGZ = new HashMap<String, String>(); mapGZ.put("cityName", "广州"); mapGZ.put("cityArea", "华南"); list.add(mapGZ); Map<String,String> mapXA = new HashMap<String, String>(); mapXA.put("cityName", "西安"); mapXA.put("cityArea", "西北"); list.add(mapXA); Map<String,String> mapCD = new HashMap<String, String>(); mapCD.put("cityName", "成都"); mapCD.put("cityArea", "西南"); list.add(mapCD); return list; } }
package com.danny.rim; import java.io.Serializable; public class UserInfo implements Serializable{ private static final long serialVersionUID = 1L; private String user_id; private String user_name; private String user_password; private String user_email; private String user_dept; public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public String getUser_email() { return user_email; } public void setUser_email(String user_email) { this.user_email = user_email; } public String getUser_dept() { return user_dept; } public void setUser_dept(String user_dept) { this.user_dept = user_dept; } }
package com.danny.rim; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.List; import java.util.Map; public interface UserInfoInterface extends Remote{ /* * Get user name */ public String getUserName() throws RemoteException; /* * Get user password */ public String getUserPassword() throws RemoteException; /* * Get basic user information, the return value of this method is a JavaBean */ public UserInfo getUserInfo() throws RemoteException; /* * Get the basic information of the user, the return value of this method is a map */ public Map<String,String> getUserInfoMap() throws RemoteException; /* * Get the basic information of the user, the return value of this method is a List set of javabeans */ public List<UserInfo> getUserInfoListBean() throws RemoteException; /* * Get the basic information of the user, the return value of this method is a List set of maps */ public List<Map<String,String>> getCityInfoListMap() throws RemoteException; }
package com.danny.rim; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.List; import java.util.Map; public class AppTest { public static void main(String[] args) { try { //Returns a reference to the remote object Registry on the specified host and port. If host is null, localhost is used. Registry registry = LocateRegistry.getRegistry("127.0.0.1", 8080); //Return the remote reference bound to the specified name in the registry UserInfoInterface userInfoInterface = (UserInfoInterface) registry.lookup("UserInfo"); System.out.println("/*****************RMI String 返回********************/"); System.out.println("User name: " + userInfoInterface.getUserName()); System.out.println("User Password: " + userInfoInterface.getUserPassword()); System.out.println("/*****************RMI JavaBean 返回********************/"); UserInfo userInfo = userInfoInterface.getUserInfo(); System.out.println("User department: " + userInfo.getUser_dept()); System.out.println("User mailbox number: " + userInfo.getUser_email()); System.out.println("/****************RMI Map return ********************/" ); Map<String,String> map = userInfoInterface.getUserInfoMap(); System.out.println("Map user ID: " + map.get("UserId")); System.out.println("Map UserPassword:" + map.get("UserPassword")); System.out.println("/*****************RMI List<Object> List套JavaBean 返回********************/"); List<UserInfo> userList = userInfoInterface.getUserInfoListBean(); for (int i = 0; i < userList.size(); i++) { UserInfo users = (UserInfo)userList.get(i); System.out.println("User name:" + users.getUser_name() + "--department:" + users.getUser_dept() + "--mail:" + users.getUser_email()); } System.out.println("/********************RMI List set Map return ******************** /"); List<Map<String,String>> cityList = userInfoInterface.getCityInfoListMap(); for (int i = 0; i < cityList.size(); i++) { Map<String,String> mapCity = (Map<String,String>)cityList.get(i); System.out.println("城市名称:" + mapCity.get("cityName") + "----区域:" + mapCity.get("cityArea")); } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace (); } catch (NotBoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } } }