参考自:http://www.blogjava.net/Even/archive/2007/05/26/120137.html
注:对原文代码进行修改、补充说明,并测试通过
RMI预备知识:http://zhidao.baidu.com/question/10278933.html
现在虽然我们很少用到RMI进行编程,但是做为Java一个最早的分布式解决方案,我们还是有了解的必要。
写在前面:
本示例程序目录结构如下:
e:/rmistudy
build.bat
RmiHello.bat
RmiHelloRemoteIntf.java
RmiHelloRemoteImpl.java
RmiHelloServer.java
RmiHelloClient.java
RmiHelloClient.class
RmiHelloRemoteImpl.class
RmiHelloRemoteImpl_Stub.class
RmiHelloRemoteIntf.class
RmiHelloServer.class
RmiHelloClient.policy
RmiHelloServer.policy
============================
远程接口 RmiHelloRemoteIntf.java
import java.rmi.Remote; import java.rmi.RemoteException; public interface RmiHelloRemoteIntf extends Remote { String helloRemoteObj(String client) throws RemoteException; }
远程接口的实现类RmiHelloRemoteImpl.java
import java.rmi.server.UnicastRemoteObject; import java.rmi.RemoteException; public class RmiHelloRemoteImpl extends UnicastRemoteObject implements RmiHelloRemoteIntf { public RmiHelloRemoteImpl() throws RemoteException { super(); } public String helloRemoteObj(String client) throws RemoteException { return "Hello World " + client; } }
服务器类RmiHelloServer.java
import java.rmi.RMISecurityManager; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; public class RmiHelloServer { public RmiHelloServer() { } public static void main(String[] args) { //创建并安装安全管理器 if(System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try{ //创建远程对象 RmiHelloRemoteImpl remoteObj = new RmiHelloRemoteImpl(); //启动注册表 LocateRegistry.createRegistry(2222); //将名称绑定到对象 Naming.rebind("//localhost:2222/helloObj", remoteObj); System.out.println("RMI服务器正在运行。。。。。。"); } catch(Exception e) { e.printStackTrace(); } } }
【注意】在本例中为了简单,将客户端和服务器置于同一机器上,故将服务对象RmiHelloRemoteImpl
绑定在localhost上
服务器端的权限文件RmiHelloServer.policy
grant codeBase
"file:/e:/rmistudy/"
{
permission java.net.SocketPermission
"*:1000-9999","accept,connect,listen,resolve";
};
现在就看看Client端是如何连接服务器和进行远程方法的调用的吧
import java.rmi.Naming; import java.rmi.RMISecurityManager; public class RmiHelloClient { public RmiHelloClient() { } public static void main(String[] args) { //创建并安装安全管理器 if(System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try{ RmiHelloRemoteIntf c1 = (RmiHelloRemoteIntf) Naming.lookup("rmi://localhost:2222/helloObj"); System.out.println(c1.helloRemoteObj("Everyone")); } catch(Exception e) { e.printStackTrace(); } } }
【注意】lookup的时候,应填写之前注册的信息。这也是服务器端和客户端的一种约定
Client端的权限文件RmiHelloClient.policy
grant codeBase
"file:/e:/rmistudy/"
{
permission java.net.SocketPermission
"*:1000-9999","accept,connect,listen,resolve";
};
至此,一个最简单的RMI例子完成,接下来是编译和运行。一般来说,rmi程序分为客户端和服务器两部分,服务器主要负责对服务对象进行注册并等待客户端进行调用。因此,一般需要由两个进程来完成。
编译批处理:build.bat
javac RmiHelloRemoteIntf.java
javac RmiHelloRemoteImpl.java
rmic RmiHelloRemoteImpl
javac RmiHelloServer.java
javac RmiHelloClient.java
pause
启动服务器和客户端(先启动服务器): RmiHello.bat
@echo 在另一个窗口启动服务器,请稍后。。。
start java -Djava.security.policy=RmiHelloServer.policy RmiHelloServer
echo 运行客户程序。。。
java -Djava.security.policy=RmiHelloClient.policy RmiHelloClient
后记:policy文件用于配置rmi框架权限,本例中为授权其监听端口以及对外发起连接等。