初识RMI基本原理
2016-09-23 21:12
106 查看
RMI分为客户端和服务器端,和socket类似。
客户端:
package it.york.rmi.client;
import it.york.rmi.stub.UserManagerInterface;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Entry {//客户端
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 2001);
UserManagerInterface userManager =
(UserManagerInterface) registry.lookup("userManager");
System.out.println(""+userManager.getAdminAccount().getUsername()
+"\r\n\t" +userManager.getAdminAccount().getPassword());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器端:
package it.york.rmi.bean;
import java.io.Serializable;
public class Account implements Serializable, Cloneable {
private static final long serialVersionUID = -1858518369668584532L;
private String username;
private String password;
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;
}
}
package it.york.rmi.stub;
import it.york.rmi.bean.Account;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface UserManagerInterface extends Remote {
public String getUserName() throws RemoteException;
public Account getAdminAccount() throws RemoteException;
}
package it.york.rmi;
import it.york.rmi.stub.UserManagerInterface;
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 Entry {//服务器端
public static void main(String[] args) throws AlreadyBoundException,RemoteException{
UserManagerImpl userManage = new UserManagerImpl();
UserManagerInterface userManagerInterface = //这里很重要,服务器端的对象,客户端因此能用
(UserManagerInterface) UnicastRemoteObject.exportObject(userManage,0);
Registry registry = LocateRegistry.createRegistry(2001);
registry.rebind("userManager", userManagerInterface);
System.out.println("server is ready");
}
}
package it.york.rmi;
import java.rmi.RemoteException;
import it.york.rmi.bean.Account;
import it.york.rmi.stub.UserManagerInterface;
public class UserManagerImpl implements UserManagerInterface {
private static final long serialVersionUID = -3111492742628447261L;
public UserManagerImpl() throws RemoteException{
}
public String getUserName() throws RemoteException {
// TODO Auto-generated method stub
return "york";
}
public Account getAdminAccount() throws RemoteException {
// TODO Auto-generated method stub
Account account = new Account();
account.setPassword("520666");
account.setUsername("ke.xiao");
return account;
}
}
客户端:
package it.york.rmi.client;
import it.york.rmi.stub.UserManagerInterface;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Entry {//客户端
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 2001);
UserManagerInterface userManager =
(UserManagerInterface) registry.lookup("userManager");
System.out.println(""+userManager.getAdminAccount().getUsername()
+"\r\n\t" +userManager.getAdminAccount().getPassword());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器端:
package it.york.rmi.bean;
import java.io.Serializable;
public class Account implements Serializable, Cloneable {
private static final long serialVersionUID = -1858518369668584532L;
private String username;
private String password;
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;
}
}
package it.york.rmi.stub;
import it.york.rmi.bean.Account;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface UserManagerInterface extends Remote {
public String getUserName() throws RemoteException;
public Account getAdminAccount() throws RemoteException;
}
package it.york.rmi;
import it.york.rmi.stub.UserManagerInterface;
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 Entry {//服务器端
public static void main(String[] args) throws AlreadyBoundException,RemoteException{
UserManagerImpl userManage = new UserManagerImpl();
UserManagerInterface userManagerInterface = //这里很重要,服务器端的对象,客户端因此能用
(UserManagerInterface) UnicastRemoteObject.exportObject(userManage,0);
Registry registry = LocateRegistry.createRegistry(2001);
registry.rebind("userManager", userManagerInterface);
System.out.println("server is ready");
}
}
package it.york.rmi;
import java.rmi.RemoteException;
import it.york.rmi.bean.Account;
import it.york.rmi.stub.UserManagerInterface;
public class UserManagerImpl implements UserManagerInterface {
private static final long serialVersionUID = -3111492742628447261L;
public UserManagerImpl() throws RemoteException{
}
public String getUserName() throws RemoteException {
// TODO Auto-generated method stub
return "york";
}
public Account getAdminAccount() throws RemoteException {
// TODO Auto-generated method stub
Account account = new Account();
account.setPassword("520666");
account.setUsername("ke.xiao");
return account;
}
}
相关文章推荐
- Java之RMI设计模式基本原理与示例
- 自己实现RMI(一)基本原理
- 学习Hadoop第六课(初识HDFS的基本原理)
- RMI设计模式基本原理与示例
- Java之RMI设计模式基本原理与示例
- Java之RMI设计模式基本原理与示例
- 初识:rmi __完整的example
- RMI基本原理
- Java之RMI设计模式基本原理与示例
- 初识Linux程序
- 初识CAA
- 初识CSS--常用元素总结(五)-表格和列表
- 初识线程
- MAC电脑怎么解决?错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099;
- 初识Android(转抄 What is Android)
- 初识Javascript
- Python--初识
- Android Volley完全解析1:初识Volley的基本用法
- untiy初识之 ios读取本地数据文件
- JDBC初识