转自:http://6221123.blog.51cto.com/6211123/1112619
- package dataserver.rmi.stub;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- import dataserver.rmi.bean.Account;
- public interface UserManagerInterface extends Remote{
- public String getUserName() throws RemoteException;
- public Account getAdminAccount() throws RemoteException;
- }
- package dataserver.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 dataserver.rmi;
- import java.rmi.RemoteException;
- import dataserver.rmi.bean.Account;
- import dataserver.rmi.stub.UserManagerInterface;
- public class UserManagerImpl implements UserManagerInterface {
- public UserManagerImpl() throws RemoteException {
- //super();
- // TODO Auto-generated constructor stub
- //UnicastRemoteObject.exportObject(this);
- }
- /**
- *
- */
- private static final long serialVersionUID = -3111492742628447261L;
- public String getUserName() throws RemoteException {
- // TODO Auto-generated method stub
- return "Tommy Lee";
- }
- public Account getAdminAccount() throws RemoteException {
- // TODO Auto-generated method stub
- Account account=new Account();
- account.setUsername("admin");
- account.setPassword("admin");
- return account;
- }
- }
- package dataserver.entry;
- import java.rmi.AlreadyBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.rmi.server.UnicastRemoteObject;
- import dataserver.rmi.UserManagerImpl;
- import dataserver.rmi.stub.UserManagerInterface;
- public class Entry {
- public static void main(String []args) throws AlreadyBoundException, RemoteException{
- UserManagerImpl userManager=new UserManagerImpl();
- UserManagerInterface userManagerI=(UserManagerInterface)UnicastRemoteObject.exportObject(userManager,0);
- // Bind the remote object's stub in the registry
- Registry registry = LocateRegistry.createRegistry(2001);
- registry.rebind("userManager", userManagerI);
- System.out.println("server is ready");
- }
- }
- package weiblog.rmi;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import dataserver.rmi.stub.UserManagerInterface;
- public class Entry2 {
- 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()
- +userManager.getAdminAccount().getPassword());
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NotBoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }