rmi从java1.1开始,rmi使得运行于不同jvm(包括不同主机)上的java应用程序可以彼此通话。
即:一个jvm中的java应用程序可以调用另一jvm上的对象(远程对象)所定义的方法。
java rmi有着重要的意义。rmi在java网络编程和高级编程中都有重要的应用,如ejb, jini等。
java2对rmi做了很多增强和改进,如安全性,动态代码下载等。
本文给出了一个最简单的例子,以说明其中的一些基本原理。本文的特点是注重了实际开发和真正的运行
环境相结合,模拟了rmi真正的开发和运行过程。
1. 实现远程接口,生成远程对象,存根(stub)和框架(skeleton)
实现远程接口,远程接口告诉jvm:实现了该接口的对象可以远程调用及有哪些方法可以调用。
本例子中定义了sayhello()。由于远程调用会涉及到网络通讯,因此这些方法都要抛出remoteexception.
远程接口和远程对象可以由a开发,并把远程接口(hello)d打包分给client端开发者b。
建立f:\server目录,把hello.java和helloimpl.java拷贝到该目录中。
// hello.java
package jdeveloper.rmi;
import java.rmi.remote;
import java.rmi.remoteexception;
public interface hello extends remote {
string sayhello() throws remoteexception;
}
生成远程对象.
// helloimpl.java
package jdeveloper.rmi;
import java.rmi.naming;
import java.rmi.remoteexception;
import java.rmi.rmisecuritymanager;
import java.rmi.server.unicastremoteobject;
public class helloimpl extends unicastremoteobject
implements hello {
public helloimpl() throws remoteexception {
super();
}
public string sayhello() {
return "hello world!";
}
public static void main(string args[]) {
// create and install a security manager
if (system.getsecuritymanager() == null) {
system.setsecuritymanager(new rmisecuritymanager());
}
try {
hello obj = new helloimpl();
// bind this object instance to the name "helloserver"
naming.rebind("helloserver", obj);
system.out.println("helloserver bound in registry");
} catch (exception e) {
system.out.println("helloimpl err: " + e.getmessage());
e.printstacktrace();
}
}
}
存根(stub)和框架(skeleton)
f:
cd \server
javac -d . hello.java
javac -d . helloimpl.java
rmic -d . jdeveloper.rmi.helloimpl
jar cvf hello.jar jdeveloper\rmi\hello.class 把hello.jar分发给client端的开发者b。
存根(stub)的存放!
存根(stub)是动态下载的。client通过存根(stub)和远程对象的框架(skeleton)通讯,对client来
讲就象操作本地对象一样。在大多数情况下,可下载的代码放到http服务器的某个目录中。本例子放到http://hjc/rmi下。
hjc:机器名,rmi:http的一个目录。如果只是单机测试则可以放到某个目录下 如f:\serverclasses.
本文将对以上两种方法都给出运行的步骤(policy文件和bat文件)。
2. 实现client端程序
// helloclient.java
package jdeveloper.rmi;
import java.rmi.rmisecuritymanager;
import java.rmi.naming;
import java.rmi.remoteexception;
import java.rmi.notboundexception;
public class helloclient {
public static void main(string args[]) throws exception{
system.setsecuritymanager(new rmisecuritymanager());
hello remoteobj = (hello)naming.lookup("//"+ args[0] +"/helloserver");
system.out.println(remoteobj.sayhello());
}
}
建立f:\client目录,把helloclient.java拷贝到该目录中。
建立f:\clientclasses目录,把hello.jar拷贝到该目录中。
f:
cd \client
javac -classpath %classpath%;f:\clientclasses\hello.jar -d . helloclient.java
3. 运行程序
启动dos窗口
set classpath=
start rmiregistry
a.以单机方式运行
建立f:\serverclasses\jdeveloper\rmi目录
f:
cd f:\serverclasses
copy f:\server\hello.jar .
copy f:\server\jdeveloper\rmi\helloimpl_stub.class f:\serverclasses\jdeveloper\rmi\
jar xvf hello.jar
启动新的dos窗口
把 starthelloserver.bat 和 rmiserver.policy 放到f:\server\
运行 starthelloserver
starthelloserver.bat
set cp=f:\server;f:\serverclasses\hello.jar
echo using classpath: %cp%
java -classpath %cp% -djava.rmi.server.codebase=file:/f:\serverclasses/
-djava.rmi.server.hostname=hjc -djava.security.policy=rmiserver.policy jdeveloper.rmi.helloimpl
rmiserver.policy
grant {
permission java.net.socketpermission "*:1024-65535",
"connect";
};
启动新的dos窗口
把 starthelloclient.bat 和 rmiclient.policy 放到f:\client\
运行 starthelloclient
starthelloclient.bat
@echo off
set cp=f:\client;f:\clientclasses\hello.jar
echo using classpath %cp%
@echo on
java -classpath %cp% -djava.rmi.server.codebase=file:/f:\serverclasses/
-djava.security.policy=rmiclient.policy jdeveloper.rmi.helloclient %1
rmiclient.policy
grant {
permission java.net.socketpermission "*:1024-65535",
"connect";
permission java.io.filepermission
"f:\\serverclasses\\-", "read";
};
b.以网络方式运行
建立 apache_path\htdocs\rmi\jdeveloper\rmi目录
cd apache_path\htdocs\rmi
copy f:\server\hello.jar .
copy f:\server\jdeveloper\rmi\helloimpl_stub.class apache_path\htdocs\rmi\jdeveloper\rmi
jar xvf hello.jar
把 starthellohttpserver.bat 和 rmihttpserver.policy 放到f:\server\
启动新的dos窗口
运行 starthellohttpserver
starthellohttpserver.bat
set cp=f:\server;f:\serverclasses\hello.jar
echo using classpath: %cp%
java -classpath %cp% -djava.rmi.server.codebase=http://hjc/rmi/
-djava.security.policy=rmihttpserver.policy jdeveloper.rmi.helloimpl
rmihttpserver.policy
grant {
permission java.net.socketpermission "*:1099", "accept, connect, listen, resolve";
permission java.net.socketpermission "*:80", "accept, connect, listen, resolve";
};
启动新的dos窗口
把 starthellohttpclient.bat 和 rmihttpclient.policy 放到f:\client\
运行 starthellohttpclient
starthellohttpclient.bat
@echo off
set cp=f:\client;f:\clientclasses\hello.jar
echo using classpath %cp%
@echo on
java -classpath %cp% -djava.rmi.server.codebase=http://hjc/rmi/
-djava.security.policy=rmihttpclient.policy jdeveloper.rmi.helloclient %1
rmihttpclient.policy
grant {
permission java.net.socketpermission "*:80", "connect";
permission java.net.socketpermission "*:1024-65535", "connect";
};
