Java RMI之HelloWorld RMI

里讲述的是基于JDK1.5的RMI程序搭建,更简单的说是一个 HelloWorld RMI。1. 这里是基于JDK1.5的,节省了繁琐的手工编译(生成桩和骨架)。不像1.4之前的RMI。2. 这里是把客户端和服务器端的两个程序,分布在两个独立的程序里面,而不是同一个package下面。是真正的分布式。3. 这里不过多阐述原理,这只是一个Hello World!!好,以下是步骤:1. 在Eclipse里面创建一个server 端的project。然后,创建一个接口,这个接口是你要向client端开放的方法定义。它叫做:UserManagerInterface,而且必须继承Remote接口。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;

} 2. 为了证明RMI中,“面向对象”或者是“无缝传递JAVA Object”是何等简单,我们需要定义一个Account类,该类是一个Bean,必须实现implements Serializable序列化接口。这是一个可以在client和server传输的可序列化对象。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;

}

}3. 此时,需要实现你已经开放的接口:

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;

}

}4. 定义一个主程序入口,注册你已经实现的RMI接口,包括开放端口等。其实很简单:把我们的接口名称,命名为“userManager”,方便client进行调用

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");

}

}5. Server端的代码已经全部写完,但是还要把bean类(Account)和接口类(UserMangerInterface)打包成jar,以便可以在下面导入进client端的项目中。项目–》右键–》导出–》jar–》选择bean和interface–》命名为RmiServerInterface.jar–》finish

6. 开始创建client端的程序。新建一个project。创建完成后,把刚才jar包导入进client的项目中。7. 导入我们的接口jar以后,可以开始编写一个client端的主程序,并调用server端的方法。

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();

}

}

}8. 启动server端的主程序,然后启动client端的主程序。server控制台打印:server is readyclient控制台打印:adminadmin大功告成!!

坦然接受生活给你的馈赠吧,不管是好的还是坏的。

Java RMI之HelloWorld RMI

相关文章:

你感兴趣的文章:

标签云: