您的位置:首页 > 编程语言 > Java开发

用RMI和CORBA开发JAVA分布式程序(二)

2008-04-26 01:46 471 查看

CORBA

CORBA是OMG组织针对企业应用上的分布式程序开发标准。重要的一点是CORBA仅仅是个规范。CORBA应用以ORB而知名。市场上已经出现了几个CORBA产品如VISIBROKE,ORBIX等。JAVAIDL是在JDK1。3及以上版本核心包的另一个应用。CORBA设计的与平台和语言无关(注:与WEBSERVICE类似),因此CORBA能运行在任何平台上,能应用在任何网络里,能用任何支持IDL接口的语言编写。和RMI类似,CORBA对象也需要用接口描述说明。但是CORBA接口需要用一种和C++类似的IDL来描述,需要指出的是IDL不是一种程序语言。

CORBA应用起步

开发CORBA应用程序主要有以下几个步骤:1定义IDL接口2将IDL转换成JAVA3应用接口4开发服务器端5开发客护端6运行名称服务器,服务器和客户程序我们现在一步一步地讲述用CORBA开发文件传输的应用,和RMI相似。我们将用到JAVAIDL,在JDK1。3包里有。

定义接口

定义接口的时候,我们需要考虑服务器支持的操作类型。在文件传输应用里,需要用到一个下载文件的方法。例5有了这个接口FileInterface。客户端将调用这个方法下载文件。IDL里的sequence和数组比较接近,但它不是个固定的长度。注意方法downloadFile的String参数类型有三种传输模式:in(从客户端到服务器),out(从服务器端到客户端),inout(双向)CodeSample5:FileInterface.idl
interfaceFileInterface{
typedefsequence<octet>Data;
DatadownloadFile(instringfileName);
};
接口定义完成后,你可以用JDK1。3带的IDLJ编译器编译生成JAVA。Idlj编译器带有一些参数,如-f参数可以生成你指定的客护端代码或服务端骨干码,或两者。在这个例子里,我们将在两台机器里,所以我们用
-fserver,-fclient
生成服务器端,客户端
JAVA
代码。
下面我们编译接口生成服务器端代码,用下面命令:
prompt>
idlj-fserverFileInterface.idl
这个命令将生成Skeleton,holder,helper和其它类。生成类
_FileInterfaceImplBase
,我们将利用这个类的接口来实现应用。

应用接口

下面我们将实现下载方法。CodeSample6:FileServant.JAVA
importJAVA.io.*;
publicclassFileServantextends_FileInterfaceImplBase{
publicbyte[]downloadFile(StringfileName){
Filefile=newFile(fileName);
bytebuffer[]=newbyte[(int)file.length()];
try{
BufferedInputStreaminput=new
BufferedInputStream(newFileInputStream(fileName));
input.read(buffer,0,buffer.length);
input.close();
}catch(Exceptione){
System.out.println("FileServantError:"+e.getMessage());
e.printStackTrace();
}
return(buffer);
}
}

服务器端开发

下一步我们开发服务器端。包括以下几个步骤:1初始化ORB2创建一个FileServant对象3在CORBA名称服务里注册对象名4打印一个状态信息5等待客户端请求CodeSample7:FileServer.JAVA
importJAVA.io.*;
importorg.omg.CosNaming.*;
importorg.omg.CosNaming.NamingContextPackage.*;
importorg.omg.CORBA.*;
publicclassFileServer{
publicstaticvoidmain(Stringargs[]){
try{
//createandinitializetheORB
ORBorb=ORB.init(args,null);
//createtheservantandregisteritwiththeORB
FileServantfileRef=newFileServant();
orb.connect(fileRef);
//gettherootnamingcontext
org.omg.CORBA.ObjectobjRef=
orb.resolve_initial_references("NameService");
NamingContextncRef=NamingContextHelper.narrow(objRef);
//Bindtheobjectreferenceinnaming
NameComponentnc=newNameComponent("FileTransfer","");
NameComponentpath[]={nc};
ncRef.rebind(path,fileRef);
System.out.println("Serverstarted....");
//Waitforinvocationsfromclients
JAVA.lang.Objectsync=newJAVA.lang.Object();
synchronized(sync){
sync.wait();
}
}catch(Exceptione){
System.err.println("ERROR:"+e.getMessage());
e.printStackTrace(System.out);
}
}
}一旦
FileServer
有了
ORB
(对象请求代理),它就能注册
CORBA
服务。它用
OMG
制定的
COS
命名服务注册。它从命名服务根开始,返回一个生成的
CORBA
对象。为了引用
NamingContext
对象,必须narroweddown一个合适的类型。如下做:
NamingContextncRef=NamingContextHelper.narrow(objRef);
对象
ncRef
objectis现在就是
org.omg.CosNaming.NamingContext
的实例
.你可以用它来注册一个代用绑定方法明明服务的CORBA服务。

开发客户端

下一步是开发一个客户端应用,获得命名服务,访问和查找别的服务。当得到
FileTransfer
服务后,就可以调用下载方法了。
CodeSample8:FileClient
importJAVA.io.*;
importJAVA.util.*;
importorg.omg.CosNaming.*;
importorg.omg.CORBA.*;
publicclassFileClient{
publicstaticvoidmain(Stringargv[]){
try{
//createandinitializetheORB
ORBorb=ORB.init(argv,null);
//gettherootnamingcontext
org.omg.CORBA.ObjectobjRef=
orb.resolve_initial_references("NameService");
NamingContextncRef=NamingContextHelper.narrow(objRef);
NameComponentnc=newNameComponent("FileTransfer","");
//Resolvetheobjectreferenceinnaming
NameComponentpath[]={nc};
FileInterfaceOperationsfileRef=
FileInterfaceHelper.narrow(ncRef.resolve(path));
if(argv.length<1){
System.out.println("Usage:JAVAFileClientfilename");
}
//savethefile
Filefile=newFile(argv[0]);
bytedata[]=fileRef.downloadFile(argv[0]);
BufferedOutputStreamoutput=new
BufferedOutputStream(newFileOutputStream(argv[0]));
output.write(data,0,data.length);
output.flush();
output.close();
}catch(Exceptione){
System.out.println("FileClientError:"+e.getMessage());
e.printStackTrace();
}
}
}

运行应用

最后一步是运行应用。有几个步骤:1运行CORBA命名服务。用命令
tnameserv
.来做。却省端口是900,如果900不能用,你可以用另一端口2500,命令如下:
prompt>
tnameserv-ORBinitialPort2500
2运行服务器当在却省端口时
prompt>
JAVAFileServer
当在其它端口如2500时
prompt>
JAVAFileServer-ORBInitialPort2500
3
生成客户端干码
prompt>
idlj-fclientFileInterface.idl
4运行客户端,假设在2500端口
prompt>
JAVAFileClienthello.txt-ORBInitialPort2500
hello.txt是要下载的文件如果命名服务在4500端口
prompt>
JAVAFileClienthello.txt-ORBInitialHostgosling-ORBInitialPort4500
我们还可以在ORB初始化指定一些参数:如
ORBorb=ORB.init(argv,null);
指定
CORBA
服务器,和命名端口如下:
Propertiesprops=newProperties();
props.put("org.omg.CORBA.ORBInitialHost","gosling");
props.put("orb.omg.CORBA.ORBInitialPort","2500");
ORBorb=ORB.init(args,props);

测试

在文件传输应用中,客户端需要先知道要传输文件的名字,服务器端没有列表显示文件名的方法,你可以增加方法扩展应用。当然,你也可以开发UI客户端代替命令行。当客户端启动后,它调用服务器端的方法显示文件列表,就可以让用户选择文件来下载它。如下图:Figure1:GUI-basedFileTransferClient

CORBA和RMI比较

显而易见,RMI比CORBA简单,开发者不需要熟悉IDL。通常,CORBA与RMI有下列不同:接口定义不同,CORBA用IDL,而RMI用JAVA。CORBA的参数有输入,输出类型。CORBA设计与语言无关。是不同语言开发系统的桥梁。CORBA对象不能垃圾收集。象我们提到的那样,CORBA与语言无关。象C++不支持垃圾收集那样。这是CORBA的缺点。一旦我们创建CORBA对象,它将一直存在,知道你亲自去处它。而RMI是自动收集。

结论

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: