Java学习笔记----------------RMI操作
这个例子不是RMI入门级的,但是也不难
首先是两个实体类,用来保存客户信息及留言信息
User.java
/** * 保存用户的实体类 * * @author 白强 * */ public class User { private String userName; private String password; public Message msg[] = new Message[100]; // the total number of users public int num = 0; public User(String name, String psw) { userName = name; password = psw; num = 0; } public void setUserName(String name) { userName = name; } public String getUserName() { return userName; } public void setPassword(String psw) { password = psw; } public String getPassword() { return password; } }
Message.java
import java.util.Date; /** * 保存消息的实体 * * @author 白强 * */ public class Message { private String sender; private String receiver; private String msgText; private Date date; public Message(String sender, String receiver, String msgText, Date date) { super(); this.sender = sender; this.receiver = receiver; this.msgText = msgText; this.date = date; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getMsgText() { return msgText; } public void setMsgText(String msgText) { this.msgText = msgText; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
RMI的接口
import java.rmi.Remote; import java.rmi.RemoteException; /** * RMI接口 * * @author 白强 * */ public interface MessageCenter extends Remote { /** * @param userName * @param password * @return 注册结果 * @throws RemoteException */ String register(String userName, String password) throws RemoteException; /** * @return 用户列表 * @throws RemoteException */ String showUsers() throws RemoteException; /** * @param uName * @param psw * @return 留言列表 * @throws RemoteException */ String checkMessage(String uName, String psw) throws RemoteException; /** * @param sender * @param receiver * @param message * @return 留言信息 * @throws RemoteException */ String leaveMessage(String sender, String receiver, String message) throws RemoteException; }
接口的实现
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.ArrayList; import java.util.Date; /** * RMI实现类 * * @author 白强 * */ public class MessageCenterImpl extends UnicastRemoteObject implements MessageCenter { private static final long serialVersionUID = 1L; ArrayList<User> list = new ArrayList<User>(); String show = ""; public MessageCenterImpl() throws RemoteException { } /* * (non-Javadoc) * * @see MessageCenter#register(java.lang.String, java.lang.String) */ @Override public String register(String userName, String password) throws RemoteException { for (int i = 0; i < list.size(); i++) { if (list.get(i).getUserName().equals(userName)) { show = "The user existes!!!!!!"; return show; } } User newUser = new User(userName, password); list.add(newUser); show = "Register Successful"; // System.out.println(userName + " has registered"); return show; } /* * (non-Javadoc) * * @see MessageCenter#showUsers() */ @Override public String showUsers() throws RemoteException { if (list.size() == 0) { show = " There are no users utill now."; } else { show = "users :" + "\n"; for (int i = 0; i < list.size(); i++) { show += (i + 1) + " : " + list.get(i).getUserName() + "\n"; } } return show; } /* * (non-Javadoc) * * @see MessageCenter#checkMessage(java.lang.String, java.lang.String) */ @Override public String checkMessage(String uName, String psw) throws RemoteException { if (list.size() != 0) { for (int i = 0; i < list.size(); i++) { // only the registered users can check their messages. if (list.get(i).getUserName().equals(uName) && list.get(i).getPassword().equals(psw)) { int j = list.get(i).num; if (j == 0) { show = "No message for you"; } else {// show all the message for the user. show = "There are Messages :" + "\n"; for (int k = 0; k < j; k++) { show += " From : " + list.get(i).msg[k].getSender() + " Message :" + list.get(i).msg[k].getMsgText() + " Time: " + list.get(i).msg[k].getDate() + "\n"; } } } } if (show.equals("")) { show = "Wrong userName or password"; } } else { // No users show = "There are no users."; } return show; } /* * (non-Javadoc) * * @see MessageCenter#leaveMessage(java.lang.String, java.lang.String, * java.lang.String) */ @Override public String leaveMessage(String sender, String receiver, String message) throws RemoteException { show = ""; for (int i = 0; i < list.size(); i++) { if (list.get(i).getUserName().equals(sender)) { for (int j = 0; j < list.size(); j++) { if (list.get(j).getUserName().equals(receiver)) { Date d = new Date(); // the date and time the message was left. Message ml = new Message(sender, receiver, message, d); // create a new message. list.get(j).msg[list.get(j).num] = ml; list.get(j).num++; // add one to the user's total message number. show = "Leave Message Successfully!!"; } } if (show == "") { // if the receiver do not exist. show = "the receiver do not exist."; } } } if (show == "") { // if the sender have not registered. show = "Please register first!"; } return show; } }
RMIServer 用来发布远程服务
import java.rmi.Naming; import java.rmi.registry.LocateRegistry; /** * RMIServer * * @author 白强 * */ public class MessageServer { public static void main(String[] args) { try { MessageCenter messageService = new MessageCenterImpl(); String host = "localhost"; int port = 8888; if (args.length > 1) { host = args[0]; port = Integer.parseInt(args[1]); } // 注册,切记不能少 LocateRegistry.createRegistry(port); String registration = "rmi://" + host + ":" + port + "/MessageCenter"; Naming.bind(registration, messageService); System.out.println("Start RMI service Successfully!!! "); } catch (Exception e) { e.printStackTrace(); } } }
客户端,用来远程连接操作
import java.io.BufferedReader; import java.io.InputStreamReader; import java.rmi.Naming; import java.rmi.Remote; /** * RMI的客户端类 * * @author 白强 * */ public class MessageClient { public MessageClient(String registration) throws Exception { Remote remoteService = Naming.lookup(registration); MessageCenter messageService = (MessageCenter) remoteService; while (true) { displayMenu(); BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); try { int actionID = Integer.parseInt(br.readLine()); switch (actionID) { case 1: // Register System.out.println("Enter your name: "); BufferedReader bfReader1 = new BufferedReader( new InputStreamReader(System.in)); String userName = bfReader1.readLine(); System.out.println("Enter your password: "); BufferedReader bfReader2 = new BufferedReader( new InputStreamReader(System.in)); String psw = bfReader2.readLine(); System.out.println(messageService.register(userName, psw)); break; case 2: // showUser System.out.println(messageService.showUsers()); break; case 3: // checkMessage System.out.println("Enter your name: "); BufferedReader bfReader3 = new BufferedReader( new InputStreamReader(System.in)); String checkUserName = bfReader3.readLine(); // get the user's password. System.out.println("Enter your password: "); BufferedReader bfReader4 = new BufferedReader( new InputStreamReader(System.in)); String checkpsw = bfReader4.readLine(); System.out.println(messageService.checkMessage( checkUserName, checkpsw)); break; case 4: // leaveMessage System.out.println("Enter your name: "); BufferedReader bfReader5 = new BufferedReader( new InputStreamReader(System.in)); String leaveUserName = bfReader5.readLine(); // get the receiver's name. System.out .println("Enter the name which the message send to: "); BufferedReader bfReader6 = new BufferedReader( new InputStreamReader(System.in)); String receiver = bfReader6.readLine(); // get the message context. System.out.println("Enter the message: "); BufferedReader bfReader7 = new BufferedReader( new InputStreamReader(System.in)); String msg = bfReader7.readLine(); System.out.println(messageService.leaveMessage( leaveUserName, receiver, msg)); break; case 5: System.exit(0); break; default: System.out.println("Invalid input!"); } } catch (NumberFormatException nfe) { System.out.println("Invalid input!"); } catch (Exception e) { e.printStackTrace(); } } } /** * 用来显示选项 */ public void displayMenu() { System.out.println(""); System.out.println("Input a number :"); System.out.println(""); System.out.println("----Designed by BaiQiang------Start--------"); System.out.println(" 1 : Register"); System.out.println(" 2 : ShowsUsers"); System.out.println(" 3 : CheckMessage"); System.out.println(" 4 : LeaveMessage"); System.out.println(" 5 : Exit"); System.out.println("----Designed by BaiQiang-------End--------"); } public static void main(String[] args) throws Exception { String registry = "localhost"; int port = 8888; if (args.length > 1) { registry = args[0]; port = Integer.parseInt(args[1]); } String registration = "rmi://" + registry + ":" + port + "/MessageCenter"; new MessageClient(registration); } }
All Right!!!!!!!!!