欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RMI 例子

程序员文章站 2022-05-12 18:52:19
...
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("admin@zyujie.com");
		userInfo.setUser_dept("信息化建议中心");
		return userInfo;
	}

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

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

	/*
	 * 获取用户基本信息,此方法返回值是一个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;
	}
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套javabean
	 */
	public List<UserInfo> getUserInfoListBean() throws RemoteException{
		List<UserInfo> list = new ArrayList<UserInfo>();
		UserInfo userOne = new UserInfo();
		userOne.setUser_id("userOne");
		userOne.setUser_name("用户111");
		userOne.setUser_password("111111");
		userOne.setUser_email("userOne@zyujie.com");
		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("userTwo@zyujie.com");
		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("userThree@zyujie.com");
		userThree.setUser_dept("销售部");
		list.add(userThree);
		return list;
	}
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套map的方式
	 */
	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{

	/*
	 * 获取用户名称
	 */
	public String getUserName() throws RemoteException;
	
	/*
	 * 获取用户密码
	 */
	public String getUserPassword() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个JavaBean
	 */
	public UserInfo getUserInfo() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个map
	 */
	public Map<String,String> getUserInfoMap() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套javabean
	 */
	public List<UserInfo> getUserInfoListBean() throws RemoteException;
	
	/*
	 * 获取用户基本信息,此方法返回值是一个List套map的方式
	 */
	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 {
			//返回指定的 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<String,String> map = userInfoInterface.getUserInfoMap();
			System.out.println("Map用户编号:" + map.get("UserId"));
			System.out.println("Map用户密码:" + 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("用户名称:" + users.getUser_name() + "--部门:" + users.getUser_dept() + "--邮箱:" + users.getUser_email());
			}
			
			System.out.println("/*****************RMI List套Map 返回********************/");
			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();
		}
	}
}


相关标签: RMI