初识RMI基本原理

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;
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值