RMI 例子

阅读更多
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();
		//使用提供的特定端口导出远程对象,以便能够接收传入的调用。 
		UserInfoInterface userInfoInterface = (UserInfoInterface)UnicastRemoteObject.exportObject(userInfoImpl,0);
		//创建并导出接受指定 port 请求的本地主机上的 Registry 实例
		Registry registry = LocateRegistry.createRegistry(8080); 
		//绑定对此注册表中指定 name 的远程引用。 
		registry.rebind("UserInfo", userInfoInterface); 
		System.out.println("服务器已经准备就绪,可随时调用。"); 
	}
}



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("系统管理员");
		userInfo.setUser_password("888888");
		userInfo.setUser_email("[email protected]");
		userInfo.setUser_dept("信息化建议中心");
		return userInfo;
	}

	public String getUserName() throws RemoteException {
		return "系统管理员";
	}

	public String getUserPassword() throws RemoteException {
		return "888888";
	}

	/*
	 * 获取用户基本信息,此方法返回值是一个map
	 */
	public Map getUserInfoMap() throws RemoteException{
		Map map = new HashMap();
		map.put("UserId", "admin");
		map.put("UserPassword", "888888");
		return map;
	}
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套javabean
	 */
	public List getUserInfoListBean() throws RemoteException{
		List list = new ArrayList();
		UserInfo userOne = new UserInfo();
		userOne.setUser_id("userOne");
		userOne.setUser_name("用户111");
		userOne.setUser_password("111111");
		userOne.setUser_email("[email protected]");
		userOne.setUser_dept("行政部");
		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("人力资源部");
		list.add(userTwo);
		
		UserInfo userThree = new UserInfo();
		userThree.setUser_id("userThree");
		userThree.setUser_name("用户333");
		userThree.setUser_password("333333");
		userThree.setUser_email("[email protected]");
		userThree.setUser_dept("销售部");
		list.add(userThree);
		return list;
	}
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套map的方式
	 */
	public List> getCityInfoListMap() throws RemoteException{
		List> list = new ArrayList>();
		Map mapSY = new HashMap();
		mapSY.put("cityName", "沈阳");
		mapSY.put("cityArea", "东北");
		list.add(mapSY);
		Map mapBJ = new HashMap();
		mapBJ.put("cityName", "北京");
		mapBJ.put("cityArea", "华北");
		list.add(mapBJ);
		Map mapSH = new HashMap();
		mapSH.put("cityName", "上海");
		mapSH.put("cityArea", "华东");
		list.add(mapSH);
		Map mapGZ = new HashMap();
		mapGZ.put("cityName", "广州");
		mapGZ.put("cityArea", "华南");
		list.add(mapGZ);
		Map mapXA = new HashMap();
		mapXA.put("cityName", "西安");
		mapXA.put("cityArea", "西北");
		list.add(mapXA);
		Map mapCD = new HashMap();
		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{

	/*
	 * 获取用户名称
	 */
	public String getUserName() throws RemoteException;
	
	/*
	 * 获取用户密码
	 */
	public String getUserPassword() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个JavaBean
	 */
	public UserInfo getUserInfo() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个map
	 */
	public Map getUserInfoMap() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套javabean
	 */
	public List getUserInfoListBean() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套map的方式
	 */
	public List> 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 {
			//返回指定的 host 和 port 上对远程对象 Registry 的引用。如果 host 为 null,则使用本地主机。 
			Registry registry = LocateRegistry.getRegistry("127.0.0.1", 8080);
			//返回注册表中绑定到指定 name 的远程引用
			UserInfoInterface userInfoInterface = (UserInfoInterface) registry.lookup("UserInfo");
			
			System.out.println("/*****************RMI String 返回********************/");
			System.out.println("用户名称:" + userInfoInterface.getUserName());
			System.out.println("用户密码:" + userInfoInterface.getUserPassword());
			
			System.out.println("/*****************RMI JavaBean 返回********************/");
			UserInfo userInfo = userInfoInterface.getUserInfo();
			System.out.println("用户部门:" + userInfo.getUser_dept());
			System.out.println("用户邮箱号:" + userInfo.getUser_email());
			
			System.out.println("/*****************RMI Map 返回********************/");
			Map map = userInfoInterface.getUserInfoMap();
			System.out.println("Map用户编号:" + map.get("UserId"));
			System.out.println("Map用户密码:" + map.get("UserPassword"));
			
			System.out.println("/*****************RMI List List套JavaBean 返回********************/");
			List userList = userInfoInterface.getUserInfoListBean();
			for (int i = 0; i < userList.size(); i++) {
				UserInfo users = (UserInfo)userList.get(i);
				System.out.println("用户名称:" + users.getUser_name() + "--部门:" + users.getUser_dept() + "--邮箱:" + users.getUser_email());
			}
			
			System.out.println("/*****************RMI List套Map 返回********************/");
			List> cityList = userInfoInterface.getCityInfoListMap();
			for (int i = 0; i < cityList.size(); i++) {
				Map mapCity = (Map)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();
		}
	}
}


 

                            
                        
                    
                    
                    

你可能感兴趣的:(RMI)