rmi-iiop出现以前,只有rmi和corba两种选择来进行分布式程序设计。rmi-iiop综合了rmi和corba的优点,克服了他们的缺点,使得程序员能更方便的编写分布式程序设计,实现分布式计算。
首先,rmi-iiop综合了rmi的简单性和corba的多语言性(兼容性),其次rmi-iiop克服了rmi只能用于java的缺点和corba的复杂性(可以不用掌握idl).
下面给出了一个非常的简单的rmi-iiop程序,该程序是在上一个例子(java2 rmi入门的基础)上修改完成的,可以对比两个程序的区别。
1. 实现远程接口,生成远程对象,存根(stub)和框架(skeleton)
实现远程接口,远程接口告诉jvm:实现了该接口的对象可以远程调用及有哪些方法可以调用。
本例子中定义了sayhello()。由于远程调用会涉及到网络通讯,因此这些方法都要抛出remoteexception.
远程接口和远程对象可以由a开发,并把远程接口(hello)d打包分给client端开发者b。
建立f:
mi_iiop目录,把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_iiop;
import javax.naming.*;
import java.rmi.remoteexception;
import java.rmi.rmisecuritymanager;
//import java.rmi.server.unicastremoteobject;
import javax.rmi.portableremoteobject;
public class helloimpl extends portableremoteobject
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"
//***** old code for rmi
// naming.rebind("helloserver", obj);
//***** new code for rmi-iiop
context initialnamingcontext = new initialcontext();
initialnamingcontext.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
mi_iiop
set classpath=.;%classpath%
javac -d . hello.java
javac -d . helloimpl.java
rmic -iiop -d . jdeveloper.rmi_iiop.helloimpl
这一步将生成<_interface>_stub.class,<_interfaceimpl>_tie.class:
_hello_stub.class和_helloimpl_tie.class
2. 实现client端程序
// helloclient.java
package jdeveloper.rmi_iiop;
import java.rmi.rmisecuritymanager;
import java.rmi.naming;
import java.rmi.remoteexception;
import java.rmi.notboundexception;
import javax.rmi.portableremoteobject;
import javax.naming.*;
public class helloclient {
public static void main(string args[]) throws exception{
system.setsecuritymanager(new rmisecuritymanager());
context initialnamingcontext = new initialcontext();
hello remoteobj = (hello) portableremoteobject.narrow(
initialnamingcontext.lookup("iiop://"+ args[0] +"/helloserver"),
hello.class
);
system.out.println(remoteobj.sayhello());
}
}
把helloclient.java拷贝到目录f:
mi_iiop中。
f:
cd
mi_iiop
javac -d . helloclient.java
3. 运行程序
启动dos窗口
运行 tnameserv
把 server.bat 和 policy 放到f:
mi_iiop
启动新的dos窗口
运行 server
启动新的dos窗口
把 client.bat 放到f:
mi_iiop
运行 client hostname
server.bat
set cp=%classpath%
set classpath=.;%classpath%
java -djava.naming.factory.initial=com.sun.jndi.cosnaming.cnctxfactory
-djava.naming.provider.url=iiop://hjc:900 -djava.security.policy=policy jdeveloper.rmi_iiop.helloimpl
set classpath=%cp%
client.bat
set cp=%classpath%
set classpath=.;%classpath%
java -djava.naming.factory.initial=com.sun.jndi.cosnaming.cnctxfactory
-djava.naming.provider.url=iiop://hjc:900 -djava.security.policy=policy jdeveloper.rmi_iiop.helloclient %1
set classpath=%cp%
policy
grant {
// allow everything for now
permission java.security.allpermission;
闽公网安备 35060202000074号